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.
|
/// - 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.
|
/// - snapToPosition - snap to step. NOTE: max value will override the step.
|
||||||
/// - snapStepsPerLoop - how many snap positions are in one knob loop;
|
/// - 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
|
/// NOTES
|
||||||
/// - script works only in images rotation on Z axis;
|
/// - 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>
|
/// </summary>
|
||||||
///
|
///
|
||||||
namespace UnityEngine.UI.Extensions
|
namespace UnityEngine.UI.Extensions
|
||||||
{
|
{
|
||||||
[RequireComponent(typeof(Image))]
|
[RequireComponent(typeof(Image))]
|
||||||
[AddComponentMenu("UI/Extensions/UI_Knob")]
|
[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 };
|
public enum Direction { CW, CCW };
|
||||||
[Tooltip("Direction of rotation CW - clockwise, CCW - counterClockwise")]
|
[Tooltip("Direction of rotation CW - clockwise, CCW - counterClockwise")]
|
||||||
|
@ -36,7 +36,7 @@ namespace UnityEngine.UI.Extensions
|
||||||
public float maxValue = 0;
|
public float maxValue = 0;
|
||||||
[Tooltip("How many rotations knob can do, if higher than max value, the latter will limit max value")]
|
[Tooltip("How many rotations knob can do, if higher than max value, the latter will limit max value")]
|
||||||
public int loops = 1;
|
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;
|
public bool clampOutput01 = false;
|
||||||
[Tooltip("snap to position?")]
|
[Tooltip("snap to position?")]
|
||||||
public bool snapToPosition = false;
|
public bool snapToPosition = false;
|
||||||
|
@ -51,43 +51,65 @@ namespace UnityEngine.UI.Extensions
|
||||||
private Vector2 _currentVector;
|
private Vector2 _currentVector;
|
||||||
private Quaternion _initRotation;
|
private Quaternion _initRotation;
|
||||||
private bool _canDrag = false;
|
private bool _canDrag = false;
|
||||||
|
[SerializeField]
|
||||||
|
private bool experimental = false;
|
||||||
|
|
||||||
//ONLY ALLOW ROTATION WITH POINTER OVER THE CONTROL
|
private RectTransform m_HandleRect;
|
||||||
public void OnPointerDown(PointerEventData eventData)
|
|
||||||
|
|
||||||
|
protected override void Awake()
|
||||||
{
|
{
|
||||||
_canDrag = true;
|
m_HandleRect = GetComponent<RectTransform>();
|
||||||
}
|
}
|
||||||
public void OnPointerUp(PointerEventData eventData)
|
|
||||||
|
public override void OnPointerUp(PointerEventData eventData)
|
||||||
{
|
{
|
||||||
_canDrag = false;
|
_canDrag = false;
|
||||||
}
|
}
|
||||||
public void OnPointerEnter(PointerEventData eventData)
|
public override void OnPointerEnter(PointerEventData eventData)
|
||||||
{
|
{
|
||||||
_canDrag = true;
|
_canDrag = true;
|
||||||
}
|
}
|
||||||
public void OnPointerExit(PointerEventData eventData)
|
public override void OnPointerExit(PointerEventData eventData)
|
||||||
{
|
{
|
||||||
_canDrag = false;
|
_canDrag = false;
|
||||||
}
|
}
|
||||||
public void OnBeginDrag(PointerEventData eventData)
|
|
||||||
{
|
|
||||||
SetInitPointerData(eventData);
|
public override void OnPointerDown(PointerEventData eventData)
|
||||||
}
|
|
||||||
void SetInitPointerData(PointerEventData eventData)
|
|
||||||
{
|
{
|
||||||
|
_canDrag = true;
|
||||||
|
|
||||||
|
base.OnPointerDown(eventData);
|
||||||
|
|
||||||
_initRotation = transform.rotation;
|
_initRotation = transform.rotation;
|
||||||
_currentVector = eventData.position - (Vector2)transform.position;
|
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;
|
_initAngle = Mathf.Atan2(_currentVector.y, _currentVector.x) * Mathf.Rad2Deg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnDrag(PointerEventData eventData)
|
public void OnDrag(PointerEventData eventData)
|
||||||
{
|
{
|
||||||
//CHECK IF CAN DRAG
|
//CHECK IF CAN DRAG
|
||||||
if (!_canDrag)
|
if (!_canDrag)
|
||||||
{
|
{
|
||||||
SetInitPointerData(eventData);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_currentVector = eventData.position - (Vector2)transform.position;
|
|
||||||
|
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;
|
_currentAngle = Mathf.Atan2(_currentVector.y, _currentVector.x) * Mathf.Rad2Deg;
|
||||||
|
|
||||||
Quaternion addRotation = Quaternion.AngleAxis(_currentAngle - _initAngle, this.transform.forward);
|
Quaternion addRotation = Quaternion.AngleAxis(_currentAngle - _initAngle, this.transform.forward);
|
||||||
|
@ -133,7 +155,6 @@ namespace UnityEngine.UI.Extensions
|
||||||
{
|
{
|
||||||
knobValue = 0;
|
knobValue = 0;
|
||||||
transform.localEulerAngles = Vector3.zero;
|
transform.localEulerAngles = Vector3.zero;
|
||||||
SetInitPointerData(eventData);
|
|
||||||
InvokeEvents(knobValue + _currentLoops);
|
InvokeEvents(knobValue + _currentLoops);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -141,7 +162,6 @@ namespace UnityEngine.UI.Extensions
|
||||||
{
|
{
|
||||||
knobValue = 1;
|
knobValue = 1;
|
||||||
transform.localEulerAngles = Vector3.zero;
|
transform.localEulerAngles = Vector3.zero;
|
||||||
SetInitPointerData(eventData);
|
|
||||||
InvokeEvents(knobValue + _currentLoops);
|
InvokeEvents(knobValue + _currentLoops);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -156,7 +176,6 @@ namespace UnityEngine.UI.Extensions
|
||||||
knobValue = maxValue;
|
knobValue = maxValue;
|
||||||
float maxAngle = direction == Direction.CW ? 360f - 360f * maxValue : 360f * maxValue;
|
float maxAngle = direction == Direction.CW ? 360f - 360f * maxValue : 360f * maxValue;
|
||||||
transform.localEulerAngles = new Vector3(0, 0, maxAngle);
|
transform.localEulerAngles = new Vector3(0, 0, maxAngle);
|
||||||
SetInitPointerData(eventData);
|
|
||||||
InvokeEvents(knobValue);
|
InvokeEvents(knobValue);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -179,6 +198,11 @@ namespace UnityEngine.UI.Extensions
|
||||||
value /= loops;
|
value /= loops;
|
||||||
OnValueChanged.Invoke(value);
|
OnValueChanged.Invoke(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual void OnInitializePotentialDrag(PointerEventData eventData)
|
||||||
|
{
|
||||||
|
eventData.useDragThreshold = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
|
|
Loading…
Reference in New Issue