- Home /
OnInteractivePreviewGUI multiple selection. Works but spams errors.
DragAndDropItemMeta
is a ScriptableObject
with a GameObject
field containing a prefab. I want to show the default preview of that prefab when the scriptableobject is selected. This code does that. But it also spams the log with this:
The serializedObject should not be used inside OnSceneGUI or OnPreviewGUI. Use the target property directly instead.
UnityEditor.Editor:CreateEditor(Object)
Win3D.Trees.DragAndDropItemPreview:OnInteractivePreviewGUI(Rect, GUIStyle) (at Assets/Win3D/Trees/DragAndDropItemPreview.cs:24)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at C:/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:179)
And:
The targets array should not be used inside OnSceneGUI or OnPreviewGUI. Use the single target property instead.
UnityEditor.Editor:CreateEditor(Object)
Win3D.Trees.DragAndDropItemPreview:OnInteractivePreviewGUI(Rect, GUIStyle) (at Assets/Win3D/Trees/DragAndDropItemPreview.cs:24)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at C:/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:179)
[CustomPreview(typeof(DragAndDropItemMeta))]
public class DragAndDropItemPreview : ObjectPreview
{
private Dictionary<GameObject, Editor> _gameObjectEditors;
public override bool HasPreviewGUI()
{
return ((DragAndDropItemMeta)target).GameObject;
}
public override void OnInteractivePreviewGUI(Rect r, GUIStyle background)
{
_gameObjectEditors = _gameObjectEditors ?? new Dictionary<GameObject, Editor>();
var meta = (DragAndDropItemMeta) target;
if(!_gameObjectEditors.TryGetValue(meta.GameObject, out var editor))
{
editor = Editor.CreateEditor(meta.GameObject);
_gameObjectEditors[meta.GameObject] = editor;
}
editor.OnInteractivePreviewGUI(r, null);
}
}
This is unity 2019.2. If I don't cache the editors I get spam about creating too many preview scenes instead.
What am I doing wrong?