I admit I have not done a lot of Custom Editors…I have a hard time passing data back and forth from the Editor script and its Monobavoir counterpart.
I have an array of shaders I can show them in a custom Editor like this…
var serializedObject = new SerializedObject(target);
var shadersArray = serializedObject.FindProperty("shaders");
serializedObject.Update();
EditorGUILayout.PropertyField(shadersArray, true);
serializedObject.ApplyModifiedProperties();
I want to use it in the Editor script. But when I try to cast it from a serializedObject to an array of Shader[ ]…it fails.
That’s not really necessary. The targets array or the target property of the Editor is essentially the legacy way how to write editor scripts. When you use SerializedObject and SerializedProperties almost everything is handled by those. Unfortunately arrays or lists can be a bit cumbersome. I’ll refer to your first post.
So “shadersArray” does represent the array as a SerializedProperty. You can get the element count of the array or List by using
int count = shadersArray.arraySize;
Now you can iterate over the elements by using “GetArrayElementAtIndex” with a specific element index
for(int i = 0; i < count; i++)
{
SerializedProperty shaderProperty = shadersArray.GetArrayElementAtIndex(i);
// [ ... ]
}
Now the serialized property of the array element represents a UnityEngine.Object reference since it’s a reference to a “Shader” instance. For any serialized UnityEngine.Object reference you can get that object through the “objectReferenceValue” of that SerializedProperty:
Note that one of the advantages of the SerializedObject and the SerializedProperties is that they can represent multiple objects at the same time and you can actually use PropertyDrawers to draw a complete property.
There’s usually not really a need for custom editors anymore since you can do most things through PropertyDrawers. You rarely need to access individual objects manually. Though of course it depends on what you want to achieve. Note that CustomEditors, Editors and PropertyDrawers are mainly used by the inspector. While it’s possible to integrate some more advanced editor functionality into a custom editor (like Unity’s terrain editor), if you want to implement a more complex editor feature it may be worth looking into EditorWindows or custom Tools.
Though I don’t want to hold you back implementing your own custom editor. Just think about the usability and if it makes sense to do whatever you do in the inspector.
Thanks for the great in-depth explanation. This really explained it a way that finally makes sense to me. I’m a solo dev, so I create tools only for myself and on an ad-hoc basis over the years, so I never got a full grasp as to how Editor scripts work.
The reason why I needed a custom editor script is because I’m working on porting my game from PCVR to Oculus quest and I’m automating the process of creating duplicate materials that use standard shader for hundreds of gameobjects and creating new ones that use a mobile diffuse shader. I have the shader array so that I can control what materials can be converted to use particular shaders. So far its working out pretty good as I did not want to create them manually.