How to access a Custom Editor components
I'm creating Editors via script using the method
Editor.CreateEditor(Selection.gameObjects);
And I can access the Header displayed on the inspector pretty easy like so:
editor.DrawHeader();
For the components I thought using:
editor.OnInspectorGUI();
And I thought it would do exactly what I wanted, but sadly It does nothing, possible because It's a virtual method and has not been override with anything and there's no way of doing it since I'm creating these editors via script like I mention before.
Also using:
editor.DrawDefaultInspector();
Won't draw the nice/flashy compact inspector we are use to. So I thought I'd seek out help, anything will be much appreciated.
Oh also using the serializedObject from the editor itself has been futile.
Answer by Limbo · Feb 27, 2019 at 01:06 AM
A little breakthrough, I found a way to access the components but I can't edit multiple objects yet, heres the code:
SerializedObject s = editor.serializedObject;
SerializedProperty components = s.FindProperty("m_Component");
SerializedProperty componentProperty = null;
s.Update();
for (int i = 0; i < components.arraySize; i++)
{
componentProperty = components.GetArrayElementAtIndex(i).FindPropertyRelative("component");
Editor comEditor = Editor.CreateEditor(componentProperty.objectReferenceValue);
componentProperty.isExpanded = EditorGUILayout.InspectorTitlebar(componentProperty.isExpanded, comEditor);
if (componentProperty.isExpanded)
{
comEditor.OnInspectorGUI();
}
}
s.ApplyModifiedProperties();
There's has to be a better way, but for now this works:
Object[] targets = editor.targets;
SerializedObject s = null;
SerializedProperty components = null;
SerializedProperty componentProperty = null;
$$anonymous$$eyList<System.Type, Object> keyList = new $$anonymous$$eyList<System.Type, Object>();
Editor e = null;
for (int i = 0; i < targets.Length; i++)
{
s = new SerializedObject(targets[i]);
components = s.Get("m_Component");
for (int j = 0; j < components.arraySize; j++)
{
componentProperty = components.Child(j).Get("component");
keyList.Add(componentProperty.objectReferenceValue.GetType(), componentProperty.objectReferenceValue);
}
}
for (int i = 0; i < keyList.Count; i++)
{
e = Editor.CreateEditor(keyList.Values[i].ToArray());
e.serializedObject.Update();
Title(false, e);
e.OnInspectorGUI();
e.serializedObject.Apply$$anonymous$$odifiedProperties();
}
$$anonymous$$eyList is just a class where keys points to different lists Get() and Child() are just extensions functions for SerializedObject & SerializedProperty
Your answer
Follow this Question
Related Questions
OnPreviewGUI for Canvas and UI Elements 0 Answers
Custom editor: EditorGui.DrawPreviewTexture in SceneView only works in the top right corner 1 Answer
how would I display an array correctly with from an editor script? 0 Answers
Custom Inspector changes not updating target in Edit Mode 1 Answer
How do I draw information, extending the "Project/Assets/" Window? 0 Answers