I’ve been trying to fix this for hours! I made my custom editor to set value and sprite color for particular square. Value is shown by TextMesh Pro text. When I hit play everything is OK, but when I exit play mode every text field is set to 0 and I have to focus every single square to change it back. Sprites are working correctly. It will be very annoying when I will be creating some levels. Any suggestions?
(on screen is stage before exit)
[CustomEditor(typeof(NormalSquare))]
public class NormalSquareEditor : Editor {
NormalSquare mNormalSquare;
SerializedObject sNormalSquare;
SerializedProperty value;
SerializedProperty type;
TMP_Text tmp;
public void OnEnable()
{
mNormalSquare = (NormalSquare)target;
sNormalSquare = new SerializedObject(mNormalSquare);
value = sNormalSquare.FindProperty("value");
type = sNormalSquare.FindProperty("Type");
tmp = mNormalSquare.gameObject.GetComponentInChildren(typeof(TMP_Text)) as TMP_Text;
}
public override void OnInspectorGUI()
{
sNormalSquare.Update();
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(value);
EditorGUILayout.PropertyField(type);
tmp.text = mNormalSquare.value.ToString();
SpriteRenderer sprite = mNormalSquare.gameObject.GetComponent(typeof(SpriteRenderer)) as SpriteRenderer;
Sprite[] squares = Resources.LoadAll<Sprite>(@"Sprites/squares");
switch (mNormalSquare.Type)
{
case SquareType.Adding:
sprite.sprite = squares.Single(s => s.name == "green");
break;
case SquareType.Subtracting:
sprite.sprite = squares.Single(s => s.name == "red");
break;
case SquareType.Multiplicating:
sprite.sprite = squares.Single(s => s.name == "blue");
break;
case SquareType.Dividing:
sprite.sprite = squares.Single(s => s.name == "yellow");
break;
}
if (EditorGUI.EndChangeCheck())
{
sNormalSquare.ApplyModifiedProperties();
}
}
}