diff --git a/Editor/UIExtensionsMenuOptions.cs b/Editor/UIExtensionsMenuOptions.cs index da6dd93..0a5ad98 100644 --- a/Editor/UIExtensionsMenuOptions.cs +++ b/Editor/UIExtensionsMenuOptions.cs @@ -1777,6 +1777,44 @@ namespace UnityEditor.UI } #endregion + #region Radial Slider + [MenuItem("GameObject/UI/Extensions/RadialSlider", false)] + static public void AddRadialSlider(MenuCommand menuCommand) + { + GameObject sliderRoot = CreateUIElementRoot("Radial Slider", menuCommand, s_ThickGUIElementSize); + GameObject SliderControl = CreateUIObject("Slider", sliderRoot); + + Image image = sliderRoot.AddComponent(); + image.sprite = AssetDatabase.GetBuiltinExtraResource(kStandardSpritePath); + image.type = Image.Type.Simple; + image.color = s_DefaultSelectableColor; + + RectTransform sliderRootRectTransform = sliderRoot.GetComponent(); + sliderRootRectTransform.anchorMin = new Vector2(0.5f, 0.5f); + sliderRootRectTransform.anchorMax = new Vector2(0.5f, 0.5f); + sliderRootRectTransform.anchoredPosition = Vector2.zero; + sliderRootRectTransform.sizeDelta = new Vector2(250f, 250f); + + Image slidrImage = SliderControl.AddComponent(); + slidrImage.sprite = AssetDatabase.GetBuiltinExtraResource(kStandardSpritePath); + slidrImage.type = Image.Type.Filled; + slidrImage.fillMethod = Image.FillMethod.Radial360; + slidrImage.fillOrigin = 3; + slidrImage.color = Color.red; + slidrImage.fillAmount = 0; + RadialSlider slider = SliderControl.AddComponent(); + slider.StartColor = Color.green; + slider.EndColor = Color.red; + + RectTransform sliderRectTransform = SliderControl.GetComponent(); + sliderRectTransform.anchorMin = Vector2.zero; + sliderRectTransform.anchorMax = Vector2.one; + sliderRectTransform.sizeDelta = Vector2.zero; + + Selection.activeGameObject = sliderRoot; + } + #endregion + #endregion diff --git a/Scripts/Controls/RadialSlider.cs b/Scripts/Controls/RadialSlider.cs index 77f6b96..c2094c2 100644 --- a/Scripts/Controls/RadialSlider.cs +++ b/Scripts/Controls/RadialSlider.cs @@ -9,12 +9,13 @@ using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions { + [AddComponentMenu("UI/Extensions/Radial Slider")] [RequireComponent(typeof(Image))] public class RadialSlider : MonoBehaviour, IPointerEnterHandler, IPointerDownHandler, IPointerUpHandler { private bool isPointerDown, isPointerReleased, lerpInProgress; private Vector2 m_localPos; - private float m_targetAngle, m_lerpTargetAngle, m_startAngle, m_currentLerpTime; + private float m_targetAngle, m_lerpTargetAngle, m_startAngle, m_currentLerpTime, m_lerpTime; private Camera m_eventCamera; private Image m_image; @@ -90,7 +91,7 @@ namespace UnityEngine.UI.Extensions public AnimationCurve LerpCurve { get { return m_lerpCurve; } - set { m_lerpCurve = value; } + set { m_lerpCurve = value; m_lerpTime = LerpCurve[LerpCurve.length - 1].time; } } public bool LerpInProgress @@ -112,6 +113,7 @@ namespace UnityEngine.UI.Extensions m_image = GetComponent(); m_image.type = Image.Type.Filled; m_image.fillMethod = Image.FillMethod.Radial360; + m_image.fillOrigin = 3; } return m_image; } @@ -128,6 +130,18 @@ namespace UnityEngine.UI.Extensions set { _onTextValueChanged = value; } } + private void Awake() + { + if (LerpCurve != null && LerpCurve.length > 0) + { + m_lerpTime = LerpCurve[LerpCurve.length - 1].time; + } + else + { + m_lerpTime = 1; + } + } + private void Update() { if (isPointerDown) @@ -151,10 +165,17 @@ namespace UnityEngine.UI.Extensions if (lerpInProgress && Value != m_lerpTargetAngle) { m_currentLerpTime += Time.deltaTime; - float perc = m_currentLerpTime / LerpCurve[LerpCurve.length - 1].time; - UpdateRadialImage(Mathf.Lerp(m_startAngle, m_lerpTargetAngle, LerpCurve.Evaluate(perc))); + float perc = m_currentLerpTime / m_lerpTime; + if (LerpCurve != null && LerpCurve.length > 0) + { + UpdateRadialImage(Mathf.Lerp(m_startAngle, m_lerpTargetAngle, LerpCurve.Evaluate(perc))); + } + else + { + UpdateRadialImage(Mathf.Lerp(m_startAngle, m_lerpTargetAngle, perc)); + } } - if (m_currentLerpTime >= LerpCurve[LerpCurve.length - 1].time || Value == m_lerpTargetAngle) + if (m_currentLerpTime >= m_lerpTime || Value == m_lerpTargetAngle) { lerpInProgress = false; UpdateRadialImage(m_lerpTargetAngle); @@ -201,7 +222,7 @@ namespace UnityEngine.UI.Extensions if (LerpToTarget && LerpCurve.length < 2) { LerpToTarget = false; - Debug.LogError("Need to define a Lerp Curve to enable 'Lerp To Target'"); + Debug.LogError("You need to define a Lerp Curve to enable 'Lerp To Target'"); } } #endif