Setting color with a public enum and displaying the change in editor?

Is it possible/sane to write a editor script that will change a game object’s material via a public enum and to then display it with that material/color in the editor?

So if I write like this I can get a drop down menu in the editor for colors.

public class PowerUpEditorScript : MonoBehaviour {
    public enum ColorEnum{Red, Blue, Green};
    public ColorEnum Colors;

void Update() {
}

That’s fine and all, I get it to show.
But I was thinking about making the colors for the material to change in editor. And I guess this would be possible with the usage of

[ExecuteInEditMode]
public class PowerUpEditorScript : MonoBehaviour {
    ...

Before the class declaration. But where would you put the code for the color change? Do you do it update or is this crazy?

void Update () {
if (Colors == Green) {
    MyGameObject.GetComponent<Renderer>().material = MyGreenMaterial; //or something similar
    }
}

My project doesn’t hang and fall on this, put I’m just curious how ExecuteInEditMode could be used for making stuff more more intuitiv. Or if it’s best to stay clear.

Objects with different materials will have different functions. But I’m thinking their functions will be contained in a different script file so nothing weird happens while executing in editor.

You could indeed do something like that.

However if the script will only be used in the editor, it would be better to create a custom editor window. (You’ll need to inherit from EditorWindow instead of MonoBehaviour. Your script will need to be placed in an ‘Editor’ folder. You’ll need to include UnityEditor namespace.)

You can then for example change the material color to what you want when e.g. a button is clicked. You’ll need to dig into the Unity Editor docs if you’ve never worked with EditorScripts before.

More reading here