Added "SetKnobValue" function which allows the setting of Value and loops

pull/413/head
Simon (darkside) Jackson 2020-10-20 23:06:07 +01:00
parent dbd3bcbaa7
commit 948d4fe62b
1 changed files with 36 additions and 1 deletions

View File

@ -1,6 +1,7 @@
/// Credit Tomasz Schelenz /// Credit Tomasz Schelenz
/// Sourced from - https://bitbucket.org/SimonDarksideJ/unity-ui-extensions/issues/46/feature-uiknob#comment-29243988 /// Sourced from - https://bitbucket.org/SimonDarksideJ/unity-ui-extensions/issues/46/feature-uiknob#comment-29243988
using System;
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
@ -134,6 +135,16 @@ namespace UnityEngine.UI.Extensions
} }
} }
UpdateKnobValue();
transform.rotation = finalRotation;
InvokeEvents(knobValue + _currentLoops);
_previousValue = knobValue;
}
private void UpdateKnobValue()
{
//PREVENT OVERROTATION //PREVENT OVERROTATION
if (Mathf.Abs(knobValue - _previousValue) > 0.5f) if (Mathf.Abs(knobValue - _previousValue) > 0.5f)
{ {
@ -176,12 +187,36 @@ namespace UnityEngine.UI.Extensions
return; return;
} }
} }
}
transform.rotation = finalRotation; public void SetKnobValue(float value, int loops = 0)
{
Quaternion newRoation = Quaternion.identity;
knobValue = value;
_currentLoops = loops;
if (snapToPosition)
{
SnapToPosition(ref knobValue);
}
if (direction == Direction.CW)
{
newRoation.eulerAngles = new Vector3(0, 0, 360 - 360 * knobValue);
}
else
{
newRoation.eulerAngles = new Vector3(0, 0, 360 * knobValue);
}
UpdateKnobValue();
transform.rotation = newRoation;
InvokeEvents(knobValue + _currentLoops); InvokeEvents(knobValue + _currentLoops);
_previousValue = knobValue; _previousValue = knobValue;
} }
private void SnapToPosition(ref float knobValue) private void SnapToPosition(ref float knobValue)
{ {
float snapStep = 1 / (float)snapStepsPerLoop; float snapStep = 1 / (float)snapStepsPerLoop;