Use button colors, improve initial render

pull/413/head
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>();
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;

View File

@ -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>();

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 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