Hi.
So i have buttons that look like this:
I want to synchronize the color of the text with the colors of the button, both in editor and runtime.
Can’t find a proper way to do so.
I’ve tried to make a button script based on the existing one, with extra field for TextMeshProUGUI element. but since there is a custom inspector, I’ve tried simply to serialize this field within override method of the button(which doesn’t work, the method is not running at all):
public class ButtonTMP : Button
{
private TextMeshProUGUI text;
public override void OnSubmit(BaseEventData eventData)
{
Debug.Log("Button Submitted!");
if (!text) text = GetComponentInChildren<TextMeshProUGUI>();
if (!IsInteractable())
text.color = colors.disabledColor;
else text.color = targetGraphic.color;
base.OnSubmit(eventData);
}
}
If anyone has suggestions or working piece of code to show, I’ll appreciate.
Meanwhile I’ll keep experimenting with the code by my self, and will post a working solution as i find one.
Ok, it was easier than i thought.
I’ve simply copied the original button script, and the editor script.
In the new button script added:
[SerializeField]
private TextMeshProUGUI m_Text;
And few overrides:
protected override void DoStateTransition(SelectionState state, bool instant)
{
base.DoStateTransition(state, instant);
Color tintColor;
m_Text.color = Color.white;
switch (state)
{
case SelectionState.Normal:
tintColor = colors.normalColor;
break;
case SelectionState.Highlighted:
tintColor = colors.highlightedColor;
break;
case SelectionState.Pressed:
tintColor = colors.pressedColor;
break;
case SelectionState.Selected:
tintColor = colors.selectedColor;
break;
case SelectionState.Disabled:
tintColor = colors.disabledColor;
break;
default:
tintColor = Color.black;
break;
}
if (transition == Transition.ColorTint)
m_Text.CrossFadeColor(tintColor * colors.colorMultiplier * 1.3f, instant ? 0f : colors.fadeDuration, true, true);
}
protected override void OnValidate()
{
base.OnValidate();
m_Text.color = Color.white;
if (interactable)
m_Text.color = colors.normalColor * colors.colorMultiplier * 1.3f;
else m_Text.color = colors.disabledColor;
}
protected override void Reset()
{
base.Reset();
if (!m_Text) m_Text = GetComponentInChildren<TextMeshProUGUI>();
}
and the same with the editor script, added property field for text:
using UnityEngine.UI;
namespace UnityEditor.UI
{
[CustomEditor(typeof(ButtonWithText), true)]
[CanEditMultipleObjects]
/// <summary>
/// Custom Editor for the Button Component.
/// Extend this class to write a custom editor for a component derived from Button.
/// </summary>
public class ButtonWithTextEditor : SelectableEditor
{
SerializedProperty m_OnClickProperty;
SerializedProperty m_TextProperty;
protected override void OnEnable()
{
base.OnEnable();
m_OnClickProperty = serializedObject.FindProperty("m_OnClick");
m_TextProperty = serializedObject.FindProperty("m_Text");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.Space();
serializedObject.Update();
EditorGUILayout.PropertyField(m_TextProperty);
EditorGUILayout.PropertyField(m_OnClickProperty);
serializedObject.ApplyModifiedProperties();
}
}
}
Everything works just fine.