- Home /
Custom Inspector requires looking at to function.
Hey guys,
I have a custom Inspector GUI running for a script, and it functions correctly; however, for it to do so, I must select the object with the inspector in the scene hierarchy while in play mode. If I do select it, even if I immediately deselect it, it functions normally for the rest of the time the project is in play mode. If I do not look at the item in the hierarchy, scripts that I need to interact with the inspected script do nothing. Running the script in the debug inspector setting, it acts the same as if I do not look at the object in the hierarchy, which is to say, no function.
I assume there is some sort of "wake" call or something that needs to be made to the custom inspector script (as that it what appears to happen when I click on it in the hierarchy); however, my research has turned nothing up. I know that the attached scripts function correctly, but only if the object is looked at.
The script I am trying to access involves a list (which I am trying to update), and my research has turned up that there are some redraw issues with custom inspectors, however, the list is not only not being redrawn, it is not being updated altogether, as the script that needs to talk to it doesn't seem to connect through unless I view the panel in the inspector. I Imagine this is something to do with the first subroutine of the custom Inspector panel being OnInspectorGUI(), so it needs to be viewed to be "On" however, I need it to be on regardless.
I am running Unity 2018.1.0f2 (if that matters).
Thanks in advance.
It would be easier if you added code for your custom inspector.
Usually custom inspector scripts should be placed in "Editor" folder in assets to work correctly.
The script is in an editor folder, otherwise it would not work at all. The script works, it's the need to "look" at it (i.e. have it in an active and visible inspector tab) that is perplexing me.
This is the core OnInspectorGUI() subroutine, everything works FINE aside from the need to be looked at:
public override void OnInspectorGUI()
{
$$anonymous$$usicPlayer player = target as $$anonymous$$usicPlayer;
Undo.RecordObject(player, "Edit playlist");
EditorGUILayout.Separator();
SettingsGUI(player);
EditorGUILayout.Separator();
PlaylistArrayGUI(player.Playlists);
EditorGUILayout.Separator();
AddPlaylistButton(player.Playlists);
EditorGUILayout.Separator();
EventsGUI();
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
}
All of the called routines just fill out sections of the Custom Inspector, there's not really any active code going on in there, which is largely why I am so confused as to why it is functioning the way it is.
It seems pretty obvious, as it functions correctly when looked at, that the error is something needing to be initialized probably somewhere else, not in the custom inspector, I'm just not sure where or what that should be. The Custom Inspector code looks pretty standard so I'm pretty perplexed as to what would be causing it.
Answer by gvergidis · Aug 23, 2018 at 07:16 AM
Hello. I think that your problem is that you set the script dirty only on GUI.Changed. Try moving that code of line out of the if block and tell me if behaviour has changed.
Answer by FuzzBeast · Aug 23, 2018 at 12:25 AM
Just going to add this as a reply, so it's not hidden under the fold:
This was my response to @Casiell's comment:
The script is in an editor folder, otherwise it would not work at all. The script works, it's the need to "look" at it (i.e. have it in an active and visible inspector tab) that is perplexing me.
This is the core OnInspectorGUI() subroutine, everything works FINE aside from the need to be looked at:
public override void OnInspectorGUI()
{
MusicPlayer player = target as MusicPlayer;
Undo.RecordObject(player, "Edit playlist");
EditorGUILayout.Separator();
SettingsGUI(player);
EditorGUILayout.Separator();
PlaylistArrayGUI(player.Playlists);
EditorGUILayout.Separator();
AddPlaylistButton(player.Playlists);
EditorGUILayout.Separator();
EventsGUI();
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
}
All of the called routines just fill out sections of the Custom Inspector, there's not really any active code going on in there, which is largely why I am so confused as to why it is functioning the way it is.
It seems pretty obvious, as it functions correctly when looked at, that the error is something needing to be initialized probably somewhere else, not in the custom inspector, I'm just not sure where or what that should be. The Custom Inspector code looks pretty standard so I'm pretty perplexed as to what would be causing it.
Answer by Casiell · Aug 23, 2018 at 07:15 AM
Ok, I couldn't find anything in documentation (weird), but I think I know the issue.
OnInspectorGUI is only called when Unity actually draws the inspector. Usually it's not a problem, because OnInspectorGUI is ment to be used for DRAWING inspector and updating it based on the user input.
If you want some data to generate automatically, you should do this in your actual script. There is [ExecuteInEditMode] tag that can help you, if you want for example your Start method to be called in Edit mode.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Custom Editor DragAndDrop for List 1 Answer
A node in a childnode? 1 Answer
How do I make child classes in a list editable from the Inspector? 1 Answer
Multiple Cars not working 1 Answer