Added Experimental update to UI Knob to enable working in World/SS-Camera. May revert.
Unity seems to calculate the mouse cursor position wrong between frames.release
parent
4a2a4f555d
commit
6a4256738a
File diff suppressed because it is too large
Load Diff
|
@ -15,17 +15,17 @@ using UnityEngine.EventSystems;
|
|||
/// - clampOutput01 - if true the output knobValue will be clamped between 0 and 1 regardless of number of loops.
|
||||
/// - snapToPosition - snap to step. NOTE: max value will override the step.
|
||||
/// - snapStepsPerLoop - how many snap positions are in one knob loop;
|
||||
/// - OnValueChanged - event that is called every frame while rotationg knob, sends <float> argument of knobValue
|
||||
/// - OnValueChanged - event that is called every frame while rotating knob, sends <float> argument of knobValue
|
||||
/// NOTES
|
||||
/// - script works only in images rotation on Z axis;
|
||||
/// - while dragging outside of control, the rotation will be cancelled
|
||||
/// - while dragging outside of control, the rotation will be canceled
|
||||
/// </summary>
|
||||
///
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
[RequireComponent(typeof(Image))]
|
||||
[AddComponentMenu("UI/Extensions/UI_Knob")]
|
||||
public class UI_Knob : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IDragHandler
|
||||
public class UI_Knob : Selectable, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler, IDragHandler, IInitializePotentialDragHandler
|
||||
{
|
||||
public enum Direction { CW, CCW };
|
||||
[Tooltip("Direction of rotation CW - clockwise, CCW - counterClockwise")]
|
||||
|
@ -36,7 +36,7 @@ namespace UnityEngine.UI.Extensions
|
|||
public float maxValue = 0;
|
||||
[Tooltip("How many rotations knob can do, if higher than max value, the latter will limit max value")]
|
||||
public int loops = 1;
|
||||
[Tooltip("Clamp output value between 0 and 1, usefull with loops > 1")]
|
||||
[Tooltip("Clamp output value between 0 and 1, useful with loops > 1")]
|
||||
public bool clampOutput01 = false;
|
||||
[Tooltip("snap to position?")]
|
||||
public bool snapToPosition = false;
|
||||
|
@ -51,43 +51,65 @@ namespace UnityEngine.UI.Extensions
|
|||
private Vector2 _currentVector;
|
||||
private Quaternion _initRotation;
|
||||
private bool _canDrag = false;
|
||||
[SerializeField]
|
||||
private bool experimental = false;
|
||||
|
||||
//ONLY ALLOW ROTATION WITH POINTER OVER THE CONTROL
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
private RectTransform m_HandleRect;
|
||||
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
_canDrag = true;
|
||||
m_HandleRect = GetComponent<RectTransform>();
|
||||
}
|
||||
public void OnPointerUp(PointerEventData eventData)
|
||||
|
||||
public override void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
_canDrag = false;
|
||||
}
|
||||
public void OnPointerEnter(PointerEventData eventData)
|
||||
public override void OnPointerEnter(PointerEventData eventData)
|
||||
{
|
||||
_canDrag = true;
|
||||
}
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
public override void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
_canDrag = false;
|
||||
}
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
SetInitPointerData(eventData);
|
||||
}
|
||||
void SetInitPointerData(PointerEventData eventData)
|
||||
|
||||
|
||||
public override void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
_canDrag = true;
|
||||
|
||||
base.OnPointerDown(eventData);
|
||||
|
||||
_initRotation = transform.rotation;
|
||||
if (experimental)
|
||||
{
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(m_HandleRect, eventData.position, eventData.pressEventCamera, out _currentVector);
|
||||
}
|
||||
else
|
||||
{
|
||||
_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;
|
||||
}
|
||||
|
||||
if (experimental)
|
||||
{
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(m_HandleRect, eventData.position, eventData.pressEventCamera, out _currentVector);
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentVector = eventData.position - (Vector2)transform.position;
|
||||
}
|
||||
_currentAngle = Mathf.Atan2(_currentVector.y, _currentVector.x) * Mathf.Rad2Deg;
|
||||
|
||||
Quaternion addRotation = Quaternion.AngleAxis(_currentAngle - _initAngle, this.transform.forward);
|
||||
|
@ -133,7 +155,6 @@ namespace UnityEngine.UI.Extensions
|
|||
{
|
||||
knobValue = 0;
|
||||
transform.localEulerAngles = Vector3.zero;
|
||||
SetInitPointerData(eventData);
|
||||
InvokeEvents(knobValue + _currentLoops);
|
||||
return;
|
||||
}
|
||||
|
@ -141,7 +162,6 @@ namespace UnityEngine.UI.Extensions
|
|||
{
|
||||
knobValue = 1;
|
||||
transform.localEulerAngles = Vector3.zero;
|
||||
SetInitPointerData(eventData);
|
||||
InvokeEvents(knobValue + _currentLoops);
|
||||
return;
|
||||
}
|
||||
|
@ -156,7 +176,6 @@ namespace UnityEngine.UI.Extensions
|
|||
knobValue = maxValue;
|
||||
float maxAngle = direction == Direction.CW ? 360f - 360f * maxValue : 360f * maxValue;
|
||||
transform.localEulerAngles = new Vector3(0, 0, maxAngle);
|
||||
SetInitPointerData(eventData);
|
||||
InvokeEvents(knobValue);
|
||||
return;
|
||||
}
|
||||
|
@ -179,6 +198,11 @@ namespace UnityEngine.UI.Extensions
|
|||
value /= loops;
|
||||
OnValueChanged.Invoke(value);
|
||||
}
|
||||
|
||||
public virtual void OnInitializePotentialDrag(PointerEventData eventData)
|
||||
{
|
||||
eventData.useDragThreshold = false;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
|
|
Loading…
Reference in New Issue