- Home /
Draw scriptable object "inspector" in custom window (EditorWindow)
Hi, i am writting something like group of settings for my project. Settings are stored in scriptableObjects. I want to achive custom window with some kind of choose structure (popup. listbox or something). Options in that choose structure will be names of scriptableObjects. After choosing one of them their "inspector" shows up below (just like in normal inspector). I did small research but i can't make them draw inspector with OnInspectorGUI and DrawDefaultInspector methods. So far i got this:
private void OnGUI()
{
var scriptableObjects = Resources.LoadAll<ScriptableObject>("Data/");
var options = createOptions(scriptableObjects); //takes names of found scriptableObjects and add them to options for popup
GUILayout.Label("Found " + scriptableObjects.Length.ToString() + " scriptable objects", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
index = EditorGUILayout.Popup(index, options);
if (EditorGUI.EndChangeCheck())
{
var editor = Editor.CreateEditor(scriptableObjects[index]);
//editor.OnInspectorGUI();
editor.DrawDefaultInspector();
}
}
OnGui not work with EditorWindows. OnGui only work with Object that exist in scene not outside.
Go back to Custom Editor with OnInspectorGUI()
Hmmm... i don't quite understand. You are saying it is impossible to show default inspector of scriptable object in custom window (EditorWindows)? What do you mean about going back to Custom Editor with OnInspectorGUI? Sorry for mental impairment ;p
private void OnGUI() not work with EditorWindow. It show nothing because OnGUI only show when an object with that script was selected in SCENE
Well, that one i understood... So is there any direction i can go to solve it?
@NoDumbQuestion you're wrong. OnGUI works with EditorWindow. You want OnInspectorGUI when writing custom Editors for ScriptableObjects. Please check docs before writing such answers.
https://docs.unity3d.com/ScriptReference/EditorWindow.OnGUI.html
I checked in my project and editor.OnInspectorGUI should work. Are you sure that your list of scriptable objects is not null? I'll try to look it up some more later if you still haven't figured it out.
Hum you are right. I think mistook OnGUI with GUI draw on SceneView made by custom editor.
Well I guess I tend to make simple stuff complicated
Answer by Bunny83 · Jun 11, 2018 at 01:03 PM
This makes no sense:
if (EditorGUI.EndChangeCheck())
{
var editor = Editor.CreateEditor(scriptableObjects[index]);
editor.OnInspectorGUI();
}
As the IMGUI system is an immediate mode GUI system. The EndChangeCheck will only be true when a change has happend. Changes can only happen during input events. That means your editor will never receive any repaint events. You have to move it outside of that if statement.
Next thing is calling DrawDefaultInspector would be kind of pointless if you have an actual custom editor for your scriptableobject.
Finally you should not use CreateEditor that way. A custom editor is also a scriptableobject. When you create one you have to destroy it as well. That's why we have the various cached versions like "Editor.CreateCachedEditor". You should store the current editor in a member variable of your editor window. The CreateCachedEditor method will determine if it's necessary to create a new editor or if it can just use the current one. If a new editor is created the old one will be destroyed.
You should not to heavy unnecessary stuff inside OnGUI of an EditorWindow. It may slow down your whole editor. Things like "Resources.LoadAll" should be done in OnEnable and you may want to provide an Update button to call it on demand. If you don't want the manual update you could use the "OnFocus" callback or "OnProjectChange". Though OnFocus would make more sense. The number of scriptableobjects could only be changed when the window does not have the focus (when the user creates new objects in the project panel). So when the usercomes back to the editor window it will update the list of objects once.
Finally be careful when using Popup. It shows a native context menu. If you have really a lot of objects to choose from it would be better to use other methods for selecting the current object.
Your answer
Follow this Question
Related Questions
CustomEditor for an ScriptableObject asset only works after recompile. 1 Answer
ScriptableObjects lists (from the same script) open simultaneously in custom window 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Serializing a ScriptableObject without creating an asset for it? 2 Answers