Regarding your previous post, I tried adding a third set called timer myself, and it worked fine:
(In c#)
//Editor/NodeTest.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class NodeTest : EditorWindow {
List<Rect> triggerRect;
List<Rect> nullRect;
List<Rect> timerRect;
[MenuItem( "EDITORS/Node Test" )]
static void Init () {
NodeTest ink = ScriptableObject.CreateInstance<NodeTest>();
ink.Show();
}
void OnEnable () {
triggerRect = new List<Rect>();
nullRect = new List<Rect>();
timerRect = new List<Rect>();
}
void OnGUI () {
Menu();
BeginWindows();
for ( int i = 0; i < triggerRect.Count; i++ ) {
GUI.color = Color.green;
triggerRect[i] = GUI.Window( i, triggerRect[i], TriggerWindow, "Trigger " + i );
}
for ( int i = 0; i < nullRect.Count; i++ ) {
GUI.color = Color.magenta;
nullRect[i] = GUI.Window( i + triggerRect.Count, nullRect[i], NullWindow, "Null " + i );
}
for ( int i = 0; i < timerRect.Count; i++ ) {
GUI.color = Color.blue;
timerRect[i] = GUI.Window( i + nullRect.Count + triggerRect.Count, timerRect[i], TimerWindow, "Timer " + i );
}
EndWindows();
}
void Menu () {
GUILayout.Space( 30 );
if ( GUILayout.Button( "Trigger", GUILayout.Width( 200 ), GUILayout.Height( 20 ) ) ) {
triggerRect.Add( new Rect( 200, 200, 200, 120 ) );
}
if ( GUILayout.Button( "Null", GUILayout.Width( 200 ), GUILayout.Height( 20 ) ) ) {
nullRect.Add( new Rect( 200, 200, 120, 120 ) );
}
if ( GUILayout.Button( "Timer", GUILayout.Width( 200 ), GUILayout.Height( 20 ) ) ) {
timerRect.Add( new Rect( 200, 200, 60, 120 ) );
}
}
void TriggerWindow ( int ID ) {
GUILayout.Space( 80 );
if ( GUILayout.Button( "X", GUILayout.Width( 20 ) ) ) {
triggerRect.RemoveAt( ID );
}
GUI.DragWindow();
}
void NullWindow ( int ID ) {
GUILayout.Space( 80 );
if ( GUILayout.Button( "X", GUILayout.Width( 20 ) ) ) {
nullRect.RemoveAt( ID - triggerRect.Count );
}
GUI.DragWindow();
}
void TimerWindow ( int ID ) {
GUILayout.Space( 80 );
if ( GUILayout.Button( "X", GUILayout.Width( 20 ) ) ) {
timerRect.RemoveAt( ID - triggerRect.Count - nullRect.Count );
}
GUI.DragWindow();
}
}
While this is fine, it would be simpler if, instead of having seperate lists for each type, you made a class with a selectable type, and just maintain a single list, something like:
//Editor/NodeTest.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class NodeTest : EditorWindow {
List<MyNode> nodeList;
NodeType selectedType;
enum NodeType { trigger, nul, timer }; //'null' is not allowed
class MyNode {
public Rect rect;
public NodeType nodeType;
public MyNode ( Rect r, NodeType n ) {
this.rect = r;
this.nodeType = n;
}
}
[MenuItem( "EDITORS/Node Test" )]
static void Init () {
NodeTest ink = ScriptableObject.CreateInstance<NodeTest>();
ink.Show();
}
void OnEnable () {
nodeList = new List<MyNode>();
}
void OnGUI () {
Menu();
BeginWindows();
for ( int i = 0; i < nodeList.Count; i++ ) {
switch ( nodeList[i].nodeType ) {
case NodeType.trigger:
GUI.color = Color.green;
break;
case NodeType.nul:
GUI.color = Color.magenta;
break;
case NodeType.timer:
GUI.color = Color.blue;
break;
}
nodeList[i].rect = GUI.Window( i, nodeList[i].rect, NodeWindow, nodeList[i].nodeType + " " + i );
}
EndWindows();
}
void Menu () {
GUILayout.Space( 30 );
selectedType = (NodeType) EditorGUILayout.EnumPopup( selectedType, GUILayout.Width( 200 ), GUILayout.Height( 20 ) );
if ( GUILayout.Button( "Add node", GUILayout.Width( 200 ), GUILayout.Height( 20 ) ) ) {
nodeList.Add( new MyNode( new Rect( 200, 200, 200, 120 ), selectedType ) );
}
}
void NodeWindow ( int ID ) {
switch ( nodeList[ID].nodeType ) {
case NodeType.trigger:
GUILayout.Space( 80 );
if ( GUILayout.Button( "X", GUILayout.Width( 20 ) ) ) {
nodeList.RemoveAt( ID );
}
break;
case NodeType.nul:
GUILayout.Space( 80 );
if ( GUILayout.Button( "X", GUILayout.Width( 20 ) ) ) {
nodeList.RemoveAt( ID );
}
break;
case NodeType.timer:
GUILayout.Space( 80 );
if ( GUILayout.Button( "X", GUILayout.Width( 20 ) ) ) {
nodeList.RemoveAt( ID );
}
break;
}
GUI.DragWindow();
}
}
Now you don’t have to worry about offsetting the index of each window by the size of each previous collection.
Regarding you last post, editor scripts cannot function as game components.
What you can do is write a custom inspector for your trigger component, that adds a button to the inspector, which opens the node editor. When the button is clicked, it checks the objects hierarchy, and creates nodes from it.