- Home /
Why UnityEngine.UI.Text color is not changed right away in the Editor?
I implemented the CustomText (it only extends UnityEngine.UI.Text) and CustomTextEditor which changes text color when button is pressed (script below):
using UnityEditor;
using UnityEditor.UI;
using UnityEngine.UI;
[CustomEditor(typeof(CustomText))]
public class CustomTextEditor : TextEditor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
Text component = (CustomText)target;
if(UnityEngine.GUILayout.Button("Change color"))
{
if (component.color == UnityEngine.Color.green)
component.color = UnityEngine.Color.red;
else
component.color = UnityEngine.Color.green;
}
}
}
When I press a "Change color" button in the Inspector I can see that color changes but it doesn't apply in the scene. As it is shown in the image, the "color" variable is green now but the text is actually red:
If I do any action in the Editor (e.g. click on the "color" field, disable/enable any game object in the scene) a color of the text will change. It changes right away also when game is in the playing mode. What could be the cause of this behavior? Is there any way to change text color when I press the button?
Thank you for any help,
Damian
Answer by Jessespike · Oct 23, 2015 at 08:22 PM
Mark the component as dirty.
EditorUtility.SetDirty(component);
http://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html