From f5602a0e1414af42cf03cba6a913983692205e88 Mon Sep 17 00:00:00 2001 From: David Gileadi <gileadis@gmail.com> Date: Fri, 1 Sep 2017 17:39:32 -0700 Subject: [PATCH] Use button colors, improve initial render --- Editor/UIExtensionsMenuOptions.cs | 8 +++++-- Scripts/Controls/Segment.cs | 31 +++++++++++++++------------- Scripts/Controls/SegmentedControl.cs | 11 ---------- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/Editor/UIExtensionsMenuOptions.cs b/Editor/UIExtensionsMenuOptions.cs index 310a0bf..57af6ec 100644 --- a/Editor/UIExtensionsMenuOptions.cs +++ b/Editor/UIExtensionsMenuOptions.cs @@ -1660,14 +1660,18 @@ namespace UnityEditor.UI SegmentedControl control = go.AddComponent<SegmentedControl>(); Color selectedColor = new Color(0f, 0.455f, 0.894f); - control.selectedColor = selectedColor; var labels = new string[] { "This", "That", "Other" }; for (int i = 0; i < 3; i++) { - var button = AddButtonAsChild(go); + var button = AddButtonAsChild(go).GetComponent<Button>(); button.gameObject.AddComponent<Segment>(); button.name = "Segment " + (i + 1); + + var colors = button.colors; + colors.pressedColor = selectedColor; + button.colors = colors; + var text = button.GetComponentInChildren<Text>(); text.text = labels[i]; text.color = selectedColor; diff --git a/Scripts/Controls/Segment.cs b/Scripts/Controls/Segment.cs index 96598ee..691ef56 100644 --- a/Scripts/Controls/Segment.cs +++ b/Scripts/Controls/Segment.cs @@ -41,13 +41,26 @@ namespace UnityEngine.UI.Extensions get { return GetComponent<Selectable>(); } } - [SerializeField] - Color textColor; internal Sprite cutSprite; protected Segment() { } + protected override void Start() + { + StartCoroutine(DelayedInit()); + } + + IEnumerator DelayedInit() + { + yield return null; + yield return null; + + button.image.overrideSprite = cutSprite; + if (selected) + MaintainSelection(); + } + public virtual void OnPointerClick(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) @@ -126,7 +139,6 @@ namespace UnityEngine.UI.Extensions } segmentedControl.selectedSegment = this.button; - StoreTextColor(); TransitionButton(); segmentedControl.onValueChanged.Invoke(index); } @@ -159,8 +171,8 @@ namespace UnityEngine.UI.Extensions internal void TransitionButton(bool instant) { - Color tintColor = selected ? segmentedControl.selectedColor : button.colors.normalColor; - Color textColor = selected ? button.colors.normalColor : this.textColor; + Color tintColor = selected ? button.colors.pressedColor : button.colors.normalColor; + Color textColor = selected ? button.colors.normalColor : button.colors.pressedColor; Sprite transitionSprite = selected ? button.spriteState.pressedSprite : cutSprite; string triggerName = selected ? button.animationTriggers.pressedTrigger : button.animationTriggers.normalTrigger; @@ -191,15 +203,6 @@ namespace UnityEngine.UI.Extensions button.targetGraphic.CrossFadeColor(targetColor, instant ? 0f : button.colors.fadeDuration, true, true); } - internal void StoreTextColor() - { - var text = GetComponentInChildren<Text>(); - if (!text) - return; - - textColor = text.color; - } - void ChangeTextColor(Color targetColor) { var text = GetComponentInChildren<Text>(); diff --git a/Scripts/Controls/SegmentedControl.cs b/Scripts/Controls/SegmentedControl.cs index 6b56cc8..f6e5ceb 100644 --- a/Scripts/Controls/SegmentedControl.cs +++ b/Scripts/Controls/SegmentedControl.cs @@ -61,9 +61,6 @@ namespace UnityEngine.UI.Extensions } } - [SerializeField] - public Color selectedColor; - public Graphic separator { get { return m_separator; } set { m_separator = value; m_separatorWidth = 0; LayoutSegments(); } } public bool allowSwitchingOff { get { return m_allowSwitchingOff; } set { m_allowSwitchingOff = value; } } @@ -94,9 +91,6 @@ namespace UnityEngine.UI.Extensions var segment = selectedSegment.GetComponent<Segment>(); if (segment) { -#if UNITY_EDITOR - segment.StoreTextColor(); -#endif segment.selected = true; } } @@ -147,11 +141,6 @@ namespace UnityEngine.UI.Extensions { selectedSegmentIndex = transform.childCount - 1; } - - if (selectedColor == new Color(0, 0, 0, 0)) - { - selectedColor = new Color(0f, 0.455f, 0.894f); - } } #endif