Added Radial Slider menu options

Minor bugix for Radial Slider when no curve assigned
release
Simon Jackson 2017-07-29 20:49:04 +01:00
parent dd0b3a3cba
commit fc8bd8ba37
2 changed files with 65 additions and 6 deletions

View File

@ -1777,6 +1777,44 @@ namespace UnityEditor.UI
} }
#endregion #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>();
image.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>(kStandardSpritePath);
image.type = Image.Type.Simple;
image.color = s_DefaultSelectableColor;
RectTransform sliderRootRectTransform = sliderRoot.GetComponent<RectTransform>();
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<Image>();
slidrImage.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>(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<RadialSlider>();
slider.StartColor = Color.green;
slider.EndColor = Color.red;
RectTransform sliderRectTransform = SliderControl.GetComponent<RectTransform>();
sliderRectTransform.anchorMin = Vector2.zero;
sliderRectTransform.anchorMax = Vector2.one;
sliderRectTransform.sizeDelta = Vector2.zero;
Selection.activeGameObject = sliderRoot;
}
#endregion
#endregion #endregion

View File

@ -9,12 +9,13 @@ using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
[AddComponentMenu("UI/Extensions/Radial Slider")]
[RequireComponent(typeof(Image))] [RequireComponent(typeof(Image))]
public class RadialSlider : MonoBehaviour, IPointerEnterHandler, IPointerDownHandler, IPointerUpHandler public class RadialSlider : MonoBehaviour, IPointerEnterHandler, IPointerDownHandler, IPointerUpHandler
{ {
private bool isPointerDown, isPointerReleased, lerpInProgress; private bool isPointerDown, isPointerReleased, lerpInProgress;
private Vector2 m_localPos; 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 Camera m_eventCamera;
private Image m_image; private Image m_image;
@ -90,7 +91,7 @@ namespace UnityEngine.UI.Extensions
public AnimationCurve LerpCurve public AnimationCurve LerpCurve
{ {
get { return m_lerpCurve; } get { return m_lerpCurve; }
set { m_lerpCurve = value; } set { m_lerpCurve = value; m_lerpTime = LerpCurve[LerpCurve.length - 1].time; }
} }
public bool LerpInProgress public bool LerpInProgress
@ -112,6 +113,7 @@ namespace UnityEngine.UI.Extensions
m_image = GetComponent<Image>(); m_image = GetComponent<Image>();
m_image.type = Image.Type.Filled; m_image.type = Image.Type.Filled;
m_image.fillMethod = Image.FillMethod.Radial360; m_image.fillMethod = Image.FillMethod.Radial360;
m_image.fillOrigin = 3;
} }
return m_image; return m_image;
} }
@ -128,6 +130,18 @@ namespace UnityEngine.UI.Extensions
set { _onTextValueChanged = value; } 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() private void Update()
{ {
if (isPointerDown) if (isPointerDown)
@ -151,10 +165,17 @@ namespace UnityEngine.UI.Extensions
if (lerpInProgress && Value != m_lerpTargetAngle) if (lerpInProgress && Value != m_lerpTargetAngle)
{ {
m_currentLerpTime += Time.deltaTime; m_currentLerpTime += Time.deltaTime;
float perc = m_currentLerpTime / LerpCurve[LerpCurve.length - 1].time; float perc = m_currentLerpTime / m_lerpTime;
UpdateRadialImage(Mathf.Lerp(m_startAngle, m_lerpTargetAngle, LerpCurve.Evaluate(perc))); 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; lerpInProgress = false;
UpdateRadialImage(m_lerpTargetAngle); UpdateRadialImage(m_lerpTargetAngle);
@ -201,7 +222,7 @@ namespace UnityEngine.UI.Extensions
if (LerpToTarget && LerpCurve.length < 2) if (LerpToTarget && LerpCurve.length < 2)
{ {
LerpToTarget = false; 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 #endif