Hi,
I am trying to create a simple custom editor script with a drop down list that I can use to quickly change the color of the gameobject. The basic functionality is “working” as it will change the color of the object when I access the drop down menu. It will even save it when I close/open the scene, however when I select the object it resets it back to the first value in the list. Thanks for any help you guys might have!
using UnityEngine;
using UnityEditor;
using System.Collections;
[System.Serializable]
[CustomEditor( typeof(CustomObject))]
public class CustomObjectEditor : Editor
{
public enum COLORS{Red,Blue,Green}
public COLORS colorChoice;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
var spriteObject = target as CustomObject;
var spriteRenderer = spriteObject.GetComponent<SpriteRenderer>();
//Use Enum dropdown menu to set the color of the object
colorChoice = (COLORS)EditorGUILayout.EnumPopup("Colors", colorChoice);
switch( colorChoice )
{
case COLORS.Red:
spriteRenderer.color = Color.red;
break;
case COLORS.Blue:
spriteRenderer.color = Color.blue;
break;
case COLORS.Green:
spriteRenderer.color = Color.green;
break;
}
if(GUI.changed)
EditorUtility.SetDirty(target);
}
}
Alright well I was able to get this to work, I just moved my enum over into my game object script and then got a reference to it in the editor script. Perhaps thats the right way to use these custom editor extension scripts and pull your values into them (would be nice for someone to clarify as the documentation was confusing for a noob). Here is what I ended up with that seemed to be working for me (maybe its still wrong):
using UnityEngine;
using UnityEditor;
using System.Collections;
[System.Serializable]
[CustomEditor( typeof(CustomObject))]
public class CustomObjectEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
var spriteObject = target as CustomObject;
var spriteRenderer = spriteObject.GetComponent<SpriteRenderer>();
var COLORS = spriteObject.GetComponent<CustomObject>().currentColor;
//Use Enum dropdown menu to set the color of the object
switch( COLORS )
{
case CustomObject.COLORS.Red:
spriteRenderer.color = Color.red;
break;
case CustomObject.COLORS.Blue:
spriteRenderer.color = Color.blue;
break;
case CustomObject.COLORS.Green:
spriteRenderer.color = Color.green;
break;
}
if(GUI.changed)
EditorUtility.SetDirty(spriteObject);
}
}
Well, an “Editor” instance is only created for editing an object but doesn’t save anything on it’s own. Once you deselect or select another object the old editor is destroyed and a new one created. You don’t “read” the current state of your actual object when you select an object. You should do something like this:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
[CustomEditor( typeof(CustomObject))]
public class CustomObjectEditor : Editor
{
public enum COLORS
{
None=-1, Red, Blue, Green
}
private static List<Color> m_Colors = new List<Color>()
{
Color.red, Color.blue, Color.green
};
public COLORS colorChoice;
private CustomObject m_Target;
private SpriteRenderer m_Renderer;
private void OnEnable()
{
m_Target = target as CustomObject;
m_Renderer = m_Target.GetComponent<SpriteRenderer>();
colorChoice = FindColor(m_Renderer.color);
}
private COLORS FindColor(Color aCol)
{
return (COLORS)m_Colors.IndexOf(aCol);
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
colorChoice = (COLORS)EditorGUILayout.EnumPopup("Colors", colorChoice);
if(GUI.changed && (int)colorChoice >= 0)
{
m_Renderer.color = m_Colors[(int)colorChoice];
EditorUtility.SetDirty(target)
}
}
}
If it should just be a quick color-setter you should replace the FindColor-line in OnEnable with:
colorChoice = COLORS.None;
That way when you select an object it will keep it’s current value.