2017-04-07 183 views
-1

我試圖設置UI.Extensions UI_Knob 初始值見鏈接到UI.Extensions回購UI.Extensions bitbutcket如何設置UI_Knob的初始值?

通過一些搜索我也許已經發現,設置初始值的最佳方法是使用模擬pointerEventData和通它使用ExecuteEvents傳遞給腳本,因爲UI_Knob腳本使用鼠標位置來設置它的值。

--BELOW對於UI_Knob--

///信用托馬斯Schelenz ///將代碼從採購 - https://bitbucket.org/ddreaper/unity-ui-

//ONLY ALLOW ROTATION WITH POINTER OVER THE CONTROL 
    public void OnPointerDown(PointerEventData eventData) 
    { 
     _canDrag = true; 
    } 
    public void OnPointerUp(PointerEventData eventData) 
    { 
     _canDrag = false; 
    } 
    public void OnPointerEnter(PointerEventData eventData) 
    { 
     _canDrag = true; 
    } 
    public void OnPointerExit(PointerEventData eventData) 
    { 
     _canDrag = true;  // Now you can drag with pointer OFF control. 
    } 
    public void OnBeginDrag(PointerEventData eventData) 
    { 
     SetInitPointerData(eventData); 
    } 
    void SetInitPointerData(PointerEventData eventData) 
    { 
     _initRotation = transform.rotation; 
     _currentVector = eventData.position - (Vector2)transform.position; 
     _initAngle = Mathf.Atan2(_currentVector.y, _currentVector.x) * Mathf.Rad2Deg; 
    } 
    public void OnDrag(PointerEventData eventData) 
    { 
     //CHECK IF CAN DRAG 
     if (!_canDrag) 
     { 
      SetInitPointerData(eventData); 
      return; 
     } 
     _currentVector = eventData.position - (Vector2)transform.position; 
     _currentAngle = Mathf.Atan2(_currentVector.y, _currentVector.x) * Mathf.Rad2Deg; 

     Quaternion addRotation = Quaternion.AngleAxis(_currentAngle - _initAngle, this.transform.forward); 
     addRotation.eulerAngles = new Vector3(0, 0, addRotation.eulerAngles.z); 

     Quaternion finalRotation = _initRotation * addRotation; 

     if (direction == Direction.CW) 
     { 
      knobValue = 1 - (finalRotation.eulerAngles.z/360f); 

      if (snapToPosition) 
      { 
       SnapToPosition(ref knobValue); 
       finalRotation.eulerAngles = new Vector3(0, 0, 360 - 360 * knobValue); 
      } 
     } 
     else 
     { 
      knobValue = (finalRotation.eulerAngles.z/360f); 

      if (snapToPosition) 
      { 
       SnapToPosition(ref knobValue); 
       finalRotation.eulerAngles = new Vector3(0, 0, 360 * knobValue); 
      } 
     } 

     //PREVENT OVERROTATION 
     if (Mathf.Abs(knobValue - _previousValue) > 0.5f) 
     { 
      if (knobValue < 0.5f && loops > 1 && _currentLoops < loops - 1) 
      { 
       _currentLoops++; 
      } 
      else if (knobValue > 0.5f && _currentLoops >= 1) 
      { 
       _currentLoops--; 
      } 
      else 
      { 
       if (knobValue > 0.5f && _currentLoops == 0) 
       { 
        knobValue = 0; 
        transform.localEulerAngles = Vector3.zero; 
        SetInitPointerData(eventData); 
        InvokeEvents(knobValue + _currentLoops); 
        return; 
       } 
       else if (knobValue < 0.5f && _currentLoops == loops - 1) 
       { 
        knobValue = 1; 
        transform.localEulerAngles = Vector3.zero; 
        SetInitPointerData(eventData); 
        InvokeEvents(knobValue + _currentLoops); 
        return; 
       } 
      } 
     } 

     //CHECK MAX VALUE 
     if (maxValue > 0) 
     { 
      if (knobValue + _currentLoops > maxValue) 
      { 
       knobValue = maxValue; 
       float maxAngle = direction == Direction.CW ? 360f - 360f * maxValue : 360f * maxValue; 
       transform.localEulerAngles = new Vector3(0, 0, maxAngle); 
       SetInitPointerData(eventData); 
       InvokeEvents(knobValue); 
       return; 
      } 
     } 

     transform.rotation = finalRotation; 
     InvokeEvents(knobValue + _currentLoops); 

     _previousValue = knobValue; 
    } 
    private void SnapToPosition(ref float knobValue) 
    { 
     float snapStep = 1/(float)snapStepsPerLoop; 
     float newValue = Mathf.Round(knobValue/snapStep) * snapStep; 
     knobValue = newValue; 
    } 
    private void InvokeEvents(float value) 
    { 
     if (clampOutput01) 
      value /= loops; 
     OnValueChanged.Invoke(value); 
    } 



} 

[System.Serializable] 
public class KnobFloatValueEvent : UnityEvent<float> { } 

}

+0

對不起,但我不明白這個問題。你能澄清嗎? – Hristo

回答

0

我回答我自己的問題而不使用模擬的鼠標數據。相反,我用MyStartingAngle()方法添加了一個Start()方法。見下文。我砍掉了UI_Knob的旋轉/設置值的方法,基本上只是注入了我自己的角度。如果有人願意,我仍然想知道如何用模擬鼠標數據執行此操作。我很感謝我的解決方案的任何輸入。謝謝閱讀!!

// ADD THIS INTO UI_Knob script below initialization. 
// I added this here to allow for setting an initial rotation/value. 

    void Start(){ 
     float myFirstAngle = 180f; 
     MyStartingAngle (myFirstAngle); 
    } 

    void MyStartingAngle(float angle){ 

     _initRotation = transform.rotation; 

     Quaternion addRotation = Quaternion.AngleAxis(angle, this.transform.forward); 
     addRotation.eulerAngles = new Vector3(0, 0, addRotation.eulerAngles.z); 

     Quaternion finalRotation = _initRotation * addRotation; 


     knobValue = 1 - (finalRotation.eulerAngles.z/360f); 

     transform.rotation = finalRotation; 
     InvokeEvents(knobValue + _currentLoops); 

     _previousValue = knobValue; 

    }