-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> { }
}
對不起,但我不明白這個問題。你能澄清嗎? – Hristo