I am creating a basic node connector (pretty much like the animator). And when you click a particular node the inspector displays that node options. The Node is a class called Node with some data. Lets say name, ID and color.
public class Node {
//Constructor
string name;
string ID;
Color color;
}
All of these nodes are saved in a scriptableobject, in a list, and i access it through GUI Textures, but in the end i save my node (refernced) in a selectedNode variable. Apart from that i have a custom editor for nodes.
[CustomEditor (typeof(Node))]
public class NodeEditor :
//I'm not sure how to set this node either scince it is not target, it is not being selected
Node node;
public override void OnInspectorGUI ()
{
Debug.Log (node.name);
}
}
Now, i need whenever this variable is different from null, the inspector to display that particular node editor.
I hope it is clear enough.
I found a quite complex workaround… It seems that it is imposible to show something in the inspector if it isn’t through Selection.activeObject
. Actually i discovered that this is what the Animator wondow does. That’s why when you click a State it takes you (in the project window) where the controller is… It makes a Selection.activeObject = StateObject
.
So in order to achieve this effect i had to create a scriptableObject:
public class NodeInspector : ScriptableObject {
public Node selectedNode; //public so that it can be set through the main window
}
Just with the selected Node. Then, the editor script of this scriptableObject simply displays the options you want for each node. However, in order to access that specific scriptableObject (since it can only be created once), it must be in a Resources folder. I couln’t find another way…
Editor script:
[CustomEditor (typeof (NodeInspector))]
public class NodeInspectorEditor : Editor {
Node node; //The one whose options will be displayed
NodeInspector selection;
public void OnEnable () {
selection = Resources.Load<NodeInspector> ("Node Inspector");
}
public override void OnInspectorGUI ()
{
node = selection.selectedNode;
//This is on the GUI methods so that if the selected Node changes, this will change too
if (node != null) {
//GUI Things
}
}
}
And finally, so as to make this object the one you want the inspector to inspect you simply do nodeInspector = Resources.Load<NodeInspector> ("Node Inspector");
and Selection.activeObject = nodeInspector
. And it finally works…
However, every time you chose a node it will make the project window go to this scrpitableObject’s asset. And i didn’t like it a thing. So, to avoid this i did set the selection, locked the inspector and set the selection to null. if the value changed it would simply unlock it.
void Update () {
if (checkSelectionChange) {
if (Selection.activeObject != null) {
ActiveEditorTracker.sharedTracker.isLocked = false;
checkSelectionChange = false;
}
}
if (selectedGUINode != null) {
nodeInspector.selectedNode = selectedNode;
Selection.activeObject = nodeInspector;
ActiveEditorTracker.sharedTracker.isLocked = true;
Selection.activeObject = null;
checkSelectionChange = true;
}
}
And that’s it