Use button colors, improve initial render

release
David Gileadi 2017-09-01 17:39:32 -07:00
parent 825ae0d504
commit f5602a0e14
3 changed files with 23 additions and 27 deletions

View File

@ -1660,14 +1660,18 @@ namespace UnityEditor.UI
SegmentedControl control = go.AddComponent<SegmentedControl>(); SegmentedControl control = go.AddComponent<SegmentedControl>();
Color selectedColor = new Color(0f, 0.455f, 0.894f); Color selectedColor = new Color(0f, 0.455f, 0.894f);
control.selectedColor = selectedColor;
var labels = new string[] { "This", "That", "Other" }; var labels = new string[] { "This", "That", "Other" };
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
var button = AddButtonAsChild(go); var button = AddButtonAsChild(go).GetComponent<Button>();
button.gameObject.AddComponent<Segment>(); button.gameObject.AddComponent<Segment>();
button.name = "Segment " + (i + 1); button.name = "Segment " + (i + 1);
var colors = button.colors;
colors.pressedColor = selectedColor;
button.colors = colors;
var text = button.GetComponentInChildren<Text>(); var text = button.GetComponentInChildren<Text>();
text.text = labels[i]; text.text = labels[i];
text.color = selectedColor; text.color = selectedColor;

View File

@ -41,13 +41,26 @@ namespace UnityEngine.UI.Extensions
get { return GetComponent<Selectable>(); } get { return GetComponent<Selectable>(); }
} }
[SerializeField]
Color textColor;
internal Sprite cutSprite; internal Sprite cutSprite;
protected Segment() 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) public virtual void OnPointerClick(PointerEventData eventData)
{ {
if (eventData.button != PointerEventData.InputButton.Left) if (eventData.button != PointerEventData.InputButton.Left)
@ -126,7 +139,6 @@ namespace UnityEngine.UI.Extensions
} }
segmentedControl.selectedSegment = this.button; segmentedControl.selectedSegment = this.button;
StoreTextColor();
TransitionButton(); TransitionButton();
segmentedControl.onValueChanged.Invoke(index); segmentedControl.onValueChanged.Invoke(index);
} }
@ -159,8 +171,8 @@ namespace UnityEngine.UI.Extensions
internal void TransitionButton(bool instant) internal void TransitionButton(bool instant)
{ {
Color tintColor = selected ? segmentedControl.selectedColor : button.colors.normalColor; Color tintColor = selected ? button.colors.pressedColor : button.colors.normalColor;
Color textColor = selected ? button.colors.normalColor : this.textColor; Color textColor = selected ? button.colors.normalColor : button.colors.pressedColor;
Sprite transitionSprite = selected ? button.spriteState.pressedSprite : cutSprite; Sprite transitionSprite = selected ? button.spriteState.pressedSprite : cutSprite;
string triggerName = selected ? button.animationTriggers.pressedTrigger : button.animationTriggers.normalTrigger; 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); 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) void ChangeTextColor(Color targetColor)
{ {
var text = GetComponentInChildren<Text>(); var text = GetComponentInChildren<Text>();

View File

@ -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 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; } } public bool allowSwitchingOff { get { return m_allowSwitchingOff; } set { m_allowSwitchingOff = value; } }
@ -94,9 +91,6 @@ namespace UnityEngine.UI.Extensions
var segment = selectedSegment.GetComponent<Segment>(); var segment = selectedSegment.GetComponent<Segment>();
if (segment) if (segment)
{ {
#if UNITY_EDITOR
segment.StoreTextColor();
#endif
segment.selected = true; segment.selected = true;
} }
} }
@ -147,11 +141,6 @@ namespace UnityEngine.UI.Extensions
{ {
selectedSegmentIndex = transform.childCount - 1; selectedSegmentIndex = transform.childCount - 1;
} }
if (selectedColor == new Color(0, 0, 0, 0))
{
selectedColor = new Color(0f, 0.455f, 0.894f);
}
} }
#endif #endif