GUI.Window from child class not reacting
I'm quite new to GUI and Editor scripting, so be aware of silly little mistakes. (Also, this is my first Unity Answers Question ever, yay)
I am making a node based dialogue system in a custom editor window. The nodes are shown as GUI windows. First, I handled the drawing of the node-windows in the "NodeEditor" class, which also handles the EditorWindow. It also had a list of "Node" instances. The "Node" class stored node data (window dimensions, text, etc.). It all kind of worked.
Then I decided to move the drawing of the windows that I did in the OnGui() method of "NodeEditor" to a virtual method in the "Node" class, so I could make child-classes that override it and look different. They should also process input differently.
My problem is: the node's windows are drawn, but they are not clickable. They don't get highlighted when I click them and I cannot drag them.
NodeEditor class (Reduced to important parts):
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class NodeEditor : EditorWindow
{
static List<Node> nodes = new List<Node>();
private static int currentId = 0;
[MenuItem("Kosro/Node Editor")]
static void ShowEditor()
{
NodeEditor editor = GetWindow<NodeEditor>();
editor.Init();
}
void Init()
{
nodes = new List<Node>();
nodes.Add(new Node());
}
void OnGUI()
{
DrawNodes();
}
public static int GiveId()
{
currentId++;
return currentId;
}
public static void DeleteNode(Node node)
{
nodes.Remove(node);
}
void DrawNodes()
{
BeginWindows();
for (int i=0; i<nodes.Count;i++)
{
nodes[i].DrawNode();
}
EndWindows();
}
}
Node class:
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class Node
{
public Vector2 windowPosition;
public Rect window = new Rect(100, 100, 100, 100);
public List<Node> connectedNodes = new List<Node>();
public string windowTitle = "Default Node";
public virtual void DrawNode()
{
window = GUI.Window(NodeEditor.GiveId(), window, DrawWindow, windowTitle);
}
void DrawWindow(int id)
{
GUI.DragWindow();
}
public virtual void ShowContextMenu()
{
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("Delete Node"), false, DeleteNode);
menu.ShowAsContext();
}
void DeleteNode()
{
NodeEditor.DeleteNode(this);
}
}
Is what I'm trying possible? Are there better alternatives? Thanks in advance ^^
Answer by JHobsie · Mar 07, 2017 at 06:04 AM
The problem is how you assign the windowID.
Because you are calling NodeEditor.GiveId() in your DrawNode function, it is assigning a different ID each call to GUI.Window
I would suggest that on c'tor/initialisation of the Node you assign the id there.
I did this with your example and it fixed the issue.
int windowID;
public Node()
{
windowID = NodeEditor.GiveId();
}
public virtual void DrawNode()
{
window = GUI.Window(windowID, window, DrawWindow, windowTitle);
}
Uuugh... stupid little mistake... thank you very much ^^