How to publish colour change to all objects that have script attached in the editor without pressing play?
Hello all,
I'm working on a 2D game in which you play one day of each season. At the end of the year the cycle repeats. There is a unique colour palette for each season.
I would like to create a tool for the artist I'm working with so they can choose a colour for each sprite in each of the seasons. They should be able to select a season from a drop down menu, and all other sprites with this script attached should reflect that change by showing the appropriate colour for the season so they can work in context.
I feel like I'm close, but I've been struggling with that last part for ages now. Currently I have a script set up called ColourPaletteTool with a custom editor that is multi-object editing enabled using scriptableObjects. When I select multiple objects and change a colour, that change is reflected for all objects selected. Likewise when I change the season, all selected objects change to the appropriate colour. However, no other object update until I click on them and reveal the script in the inspector.
I've tried using events, but had no luck, though I feel that this is the answer. I'm using OnInspectorGUI() at the minute which gets called far too often (and doesn't achieve the desired effect), and I only need to publish this change once when a new season is selected.
Here is what I have so far. Any pointers would be greatly appreciated.
// Custom editor
[CustomEditor(typeof(_ColourPaletteTool)), CanEditMultipleObjects]
public class _ColourPaletteToolEditor : Editor
{
// Set up the serialized properties
SerializedProperty winter;
SerializedProperty spring;
SerializedProperty summer;
SerializedProperty autumn;
SerializedProperty currentSeason;
void OnEnable()
{
winter = serializedObject.FindProperty("colourPalette.Array.data[0]");
spring = serializedObject.FindProperty("colourPalette.Array.data[1]");
summer = serializedObject.FindProperty("colourPalette.Array.data[2]");
autumn = serializedObject.FindProperty("colourPalette.Array.data[3]");
currentSeason = serializedObject.FindProperty("currentSeason");
}
public override void OnInspectorGUI()
{
// Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
serializedObject.Update();
// Show the custom GUI controls.
SeasonManager.SelectSeason((Season)EditorGUILayout.EnumPopup("Season", SeasonManager.CurrentSeason));
currentSeason.intValue = SeasonManager.CurrentSeason.GetHashCode();
winter.colorValue = EditorGUILayout.ColorField("Winter", winter.colorValue);
spring.colorValue = EditorGUILayout.ColorField("Spring", spring.colorValue);
summer.colorValue = EditorGUILayout.ColorField("Summer", summer.colorValue);
autumn.colorValue = EditorGUILayout.ColorField("Autumn", autumn.colorValue);
// Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
serializedObject.ApplyModifiedProperties();
}
}
// Season manage (one instance of this class)
[ExecuteInEditMode]
public class SeasonManager : MonoBehaviour
{
public static Season CurrentSeason = Season.Winter;
public static _ColourPaletteTool[] tools;
public static void SelectSeason(Season season)
{
CurrentSeason = season;
tools = (_ColourPaletteTool[])FindObjectsOfType(typeof(_ColourPaletteTool));
foreach (var tool in tools)
{
tool.UpdateColour();
}
}
}
// Tool script (many instances of this class)
[ExecuteInEditMode]
public class _ColourPaletteTool : MonoBehaviour
{
[SerializeField]
private Color[] colourPalette = new Color[4];
[SerializeField]
private int currentSeason = SeasonManager.CurrentSeason.GetHashCode();
void OnValidate()
{
UpdateColour();
}
public void UpdateColour()
{
GetComponent<SpriteRenderer>().color = colourPalette[currentSeason];
}
}
Your answer
Follow this Question
Related Questions
ReorderableList (or similar) for UIElements 0 Answers
Undo Redo Custom Editor Error ("Assertion failed on expression: '!GetUndoManager().IsProcessing()'") 0 Answers
Undo Problem With Curve Fields In Custom Editor Window 1 Answer
[Simple question] Adding a new SerializedProperty to a SerializedObject 1 Answer