- Home /
Edit an object in UnityEditor EditorWindow
Hi All
If I have a class I have created with public members and I have my own EditorWindow. Can I create some GUI to edit the members on that class in the EditorWindow in a similar way to the GUI the editor generates for scripts attached to the selected GameObject automatically, or do I need to create the GUI manually for each property.
I can see PropertyField and ObjectField GUI elements, but it's not entirely clear how these should work.
Thanks H
Answer by Statement · Aug 11, 2011 at 03:52 PM
Yes you can. Check out the docs for the SerializedProperty which has an example. I am sure you can use this in EditorWindow.OnGUI as well for your window.
Basically you create a SerializedObject from any object, then you can get the SerializedProperty you wish to obtain and use them in EditorGUI.PropertyField or EditorGUILayout.PropertyField.
Thanks, I'll check it out - I've been looking at these but not quite got my head around them. At least I know am broadly looking in the right area. Do these classes need to operate on current selection?
I've been using this this evening, but it seems I have to create a PropertyField for each property in my class. I was hoping this framework would create GUI for all public members, if it should, what am I missing?
Well, check out http://unity3d.com/support/documentation/ScriptReference/SerializedObject.GetIterator.html - from what I gather of it, it would be used in a foreach, where you get a SerializedProperty for every propery on the object.
Ahh that's pretty useful. I have another snag in that one of my properties is a List of serialisable objects that are subclasses of ScriptableObject but while the List shows up and I can expand or contract it, none of the items in the list show up?
Do I need to create a custom editor for each object? It seems odd, because any ScriptableObject I've exposed on a script would just get UI built for it... ?
Well, you could always roll your own code with reflection. something like yourObject.GetType().GetProperties(). Google for ".net Reflection" or something if you want to go this route. This isn't anything unity specific, it's included in .net (and mono)
Your answer
Follow this Question
Related Questions
How do I get a reference to the default editor windows (Hierarchy, Console, and Inspector)? 1 Answer
Editor GUI Foldout header style customization 0 Answers
Issue with custom editor window zooming feature 0 Answers
Is it possible to store and display EditorGUILayout.Toggles? 0 Answers
How do I make a progress bar in the editor lock the background? 0 Answers