From a2bb233141692cca30c6eb02e192ea3d5b2cf93e Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Tue, 25 Jul 2017 18:54:00 +0100 Subject: [PATCH] Merged Stepper control PR #11 --- Editor/UIExtensionsMenuOptions.cs | 23 +++ Scripts/Controls/Stepper.cs | 266 ++++++++++++++++++++++++++++++ Scripts/Controls/Stepper.cs.meta | 12 ++ 3 files changed, 301 insertions(+) create mode 100644 Scripts/Controls/Stepper.cs create mode 100644 Scripts/Controls/Stepper.cs.meta diff --git a/Editor/UIExtensionsMenuOptions.cs b/Editor/UIExtensionsMenuOptions.cs index 8e2e97f..da6dd93 100644 --- a/Editor/UIExtensionsMenuOptions.cs +++ b/Editor/UIExtensionsMenuOptions.cs @@ -1677,6 +1677,29 @@ namespace UnityEditor.UI Selection.activeGameObject = go; } #endregion + + #region Stepper + [MenuItem("GameObject/UI/Extensions/Stepper", false)] + static public void AddStepper(MenuCommand menuCommand) + { + GameObject go = CreateUIElementRoot("Stepper", menuCommand, new Vector2(42, kThinHeight)); + Stepper control = go.AddComponent(); + + var labels = new string[] { "−", "+" }; + for (int i = 0; i < 2; i++) + { + var button = AddButtonAsChild(go); + button.gameObject.AddComponent(); + button.name = i == 0 ? "Minus" : "Plus"; + var text = button.GetComponentInChildren(); + text.text = labels[i]; + } + + control.LayoutSides(); + + Selection.activeGameObject = go; + } + #endregion #region UI Knob [MenuItem("GameObject/UI/Extensions/UI Knob", false)] diff --git a/Scripts/Controls/Stepper.cs b/Scripts/Controls/Stepper.cs new file mode 100644 index 0000000..e191f6c --- /dev/null +++ b/Scripts/Controls/Stepper.cs @@ -0,0 +1,266 @@ +/// Credit David Gileadi +/// Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/pull-requests/11 + +using System; +using UnityEngine.Events; +using UnityEngine.EventSystems; + +namespace UnityEngine.UI.Extensions +{ + // Stepper control + [AddComponentMenu("UI/Extensions/Stepper")] + [RequireComponent(typeof(RectTransform))] + public class Stepper : UIBehaviour + { + public Button[] sides + { + get + { + if (_sides == null || _sides.Length == 0) + { + _sides = GetSides(); + } + return _sides; + } + } + private Button[] _sides; + + [SerializeField] + private int _value = 0; + public int value { get { return _value; } set { _value = value; } } + + [SerializeField] + private int _minimum = 0; + public int minimum { get { return _minimum; } set { _minimum = value; } } + + [SerializeField] + private int _maximum = 100; + public int maximum { get { return _maximum; } set { _maximum = value; } } + + [SerializeField] + private int _step = 1; + public int step { get { return _step; } set { _step = value; } } + + [SerializeField] + private bool _wrap = false; + public bool wrap { get { return _wrap; } set { _wrap = value; } } + + [SerializeField] + private Graphic _separator; + public Graphic separator { get { return _separator; } set { _separator = value; _separatorWidth = 0; LayoutSides(sides); } } + + private float _separatorWidth = 0; + private float separatorWidth + { + get + { + if (_separatorWidth == 0 && separator) + { + _separatorWidth = separator.rectTransform.rect.width; + var image = separator.GetComponent(); + if (image) + _separatorWidth /= image.pixelsPerUnit; + } + return _separatorWidth; + } + } + + [Serializable] + public class StepperValueChangedEvent : UnityEvent { } + + // Event delegates triggered on click. + [SerializeField] + private StepperValueChangedEvent _onValueChanged = new StepperValueChangedEvent(); + public StepperValueChangedEvent onValueChanged + { + get { return _onValueChanged; } + set { _onValueChanged = value; } + } + + protected Stepper() + { } + +#if UNITY_EDITOR + protected override void OnValidate() + { + base.OnValidate(); + + if (separator) + LayoutSides(); + + if (!wrap) + { + DisableAtExtremes(sides); + } + } +#endif + + private Button[] GetSides() + { + var buttons = GetComponentsInChildren