I’m trying to create/draw a Preview Window for a object listed in a custom List Class, e.g:
ExampleLists class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExampleLists : MonoBehaviour {
[System.Serializable]
public class ExampleClass{
public string itemName;
public GameObject itemPrefab;
}
public List<ExampleClass> exampleClassList = new List<ExampleClass>();
}
CustomEditor class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(ExampleLists))]
public class TestEditorPreview : Editor {
ExampleLists exampleLists;
public override void OnInspectorGUI()
{
serializedObject.Update();
exampleLists = (ExampleLists)target;
//base.OnInspectorGUI();
EditorGUILayout.PropertyField(serializedObject.FindProperty("exampleClassList"), new GUIContent("Examples Lists", "List of clasess."), true);
serializedObject.ApplyModifiedProperties();
}
}
I’ve struggled with this myself. I’m sorry, I don’t have the answer, but here are some thoughts: Drawing to the preview panel is trivial, it’s also possible to get an object preview from a helper class and render it there. The problem is getting the currently selected object from the list.
I’ve looked around and found that EditorGUIUtility.keyboardControl is updated when the selection of list elements changes. ControlIDs are only valid during the current GUI call, but they might be enough to identify the selected item. I don’t know if it’s possible to get an object by specifying the id, but potentially there is some way to search through the SerializedObject and request a controlID for every property and sub-property and then compare each value against the keyboardControl. Sounds like bad performance, but should theoretically work. So the theory is: Unity draws all fields and gives them an id. After that, you iterate through everything manually and request an id for each control (if you draw the control yourself, you already know the name/label and can use it as a hint. Finally, since the ids should be recreated deterministically (within one GUI call) you can check which property was the selected one. If you draw the controls yourself, this should work, if not, I’m unsure about retrieving the IDs.
Oh, also you can try my asset Voltage for editor UI. VoltageThumbnail does what you need. Sorry to promote, but it can help and is free. If you decide to try it, i’ll be on the forums answering questions.
I tried what you suggest, it’s working, but in a weird mode …The object is rendered only if it’s in the scene and active and it’s a child of the object with attached script and also referenced in the reorderable list, script:
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
GameObject obj = (target as MyBaseItemDataContainer).MyCustomClass[0].prefabRef!= null ? (target as MyBaseItemDataContainer).MyCustomClass[0].prefabRef.gameObject : (target as MyBaseItemDataContainer).MyCustomClass[0].prefabRef;
if (obj)
{
if (gameObjectEditor == null)
gameObjectEditor = CreateEditor(obj);
gameObjectEditor.OnPreviewGUI(r, background);
}