public NodeManager Clone() { NodeManager clone = ScriptableObject.CreateInstance <NodeManager>(); clone.Init(); clone.nodeID = nodeID; foreach (BehaviourTree.Node node in nodes) { clone.nodes.Add(NodeManager.Clone <BehaviourTree.Node>(node)); } foreach (BehaviourTree.Node node in clone.nodes) { if (null != node.parent) { node.parent = clone.FindNode(node.parent.id); } for (int i = 0; i < node.children.Count; i++) { BehaviourTree.Node child = node.children[i]; node.children[i] = clone.FindNode(child.id); } } return(clone); }
void InitCreator() { #if UNITY_EDITOR creator = new Dictionary <string, CreateNodeDelegate>(); List <Assembly> scriptAssemblies = System.AppDomain.CurrentDomain.GetAssemblies().Where((Assembly assembly) => assembly.FullName.Contains("Assembly")).ToList(); if (!scriptAssemblies.Contains(Assembly.GetExecutingAssembly())) { scriptAssemblies.Add(Assembly.GetExecutingAssembly()); } foreach (Assembly assembly in scriptAssemblies) { foreach (System.Type type in assembly.GetTypes().Where(T => T.IsClass && !T.IsAbstract && T.IsSubclassOf(typeof(BehaviourTree.Node)))) { object[] nodeAttributes = type.GetCustomAttributes(typeof(NodeAttribute), false); if (0 == nodeAttributes.Length) { continue; } NodeAttribute attr = nodeAttributes[0] as NodeAttribute; if (attr != null) { string typeName = type.Name; creator.Add(attr.contextText, () => { BehaviourTree.Node node = ScriptableObject.CreateInstance(typeName) as BehaviourTree.Node; return(node); }); } } } #endif }
void MakeTransition(object obj) { int selectedIndex = GetSelectedIndex(); if (-1 != selectedIndex) { selectedNode = AIEditorWindow.manager.nodes[selectedIndex]; editorState = EditorState.MakeTransition; } }
void DeleteNode(object obj) { int selectedIndex = GetSelectedIndex(); if (-1 != selectedIndex) { selectedNode = AIEditorWindow.manager.nodes[selectedIndex]; if (null != selectedNode.parent) { selectedNode.parent.children.Remove(selectedNode); } AIEditorWindow.manager.nodes.RemoveAt(selectedIndex); } }
void CreateNode(object obj) { string nodeType = obj.ToString(); BehaviourTree.Node node = AIEditorWindow.manager.creator[nodeType](); node.id = AIEditorWindow.manager.nodeID++; node.rect.x = mousePos.x; node.rect.y = mousePos.y; AIEditorWindow.manager.nodes.Add(node); if (null != selectedNode) { selectedNode.children.Add(node); node.parent = selectedNode; selectedNode = null; } }
void OnGUI() { if (null == window) { return; } Event e = Event.current; mousePos = e.mousePosition; bool leftClick = 0 == e.button; bool rightClick = 1 == e.button; bool mouseDown = EventType.MouseDown == e.type; bool mouseUp = EventType.MouseUp == e.type; bool mouseDrag = EventType.MouseDrag == e.type; bool scrollWheel = EventType.ScrollWheel == e.type; if (mouseDown) { int selectedIndex = GetSelectedIndex(); if (-1 != selectedIndex) { UnityEditor.Selection.activeObject = AIEditorWindow.manager.nodes[selectedIndex]; } } if (rightClick && mouseDown && EditorState.MakeTransition != editorState) { GenericMenu menu = new GenericMenu(); int selectedIndex = GetSelectedIndex(); if (null != AIEditorWindow.manager.creator) { foreach (var v in AIEditorWindow.manager.creator) { menu.AddItem(new GUIContent("Add " + v.Key), false, CreateNode, v.Key); } } if (-1 != selectedIndex) { menu.AddSeparator(""); selectedNode = AIEditorWindow.manager.nodes[selectedIndex]; menu.AddItem(new GUIContent("Make Transition"), false, MakeTransition, null); menu.AddItem(new GUIContent("Delete Node"), false, DeleteNode, null); } menu.ShowAsContext(); e.Use(); } else if (leftClick && mouseDown && EditorState.MakeTransition == editorState) { int selectedIndex = GetSelectedIndex(); if (-1 != selectedIndex && !AIEditorWindow.manager.nodes[selectedIndex].Equals(selectedNode)) { BehaviourTree.Node node = AIEditorWindow.manager.nodes[selectedIndex]; node.parent = selectedNode; selectedNode.children.Add(node); } editorState = EditorState.Default; selectedNode = null; e.Use(); } if (mouseDrag) { int selectedIndex = GetSelectedIndex(); if (-1 == selectedIndex) { foreach (BehaviourTree.Node node in AIEditorWindow.manager.nodes) { node.rect.position += e.delta; } Repaint(); } else { BehaviourTree.Node node = AIEditorWindow.manager.nodes [selectedIndex]; BehaviourTree.Node parent = node.parent; if (null != parent) { parent.children.Sort((BehaviourTree.Node lhs, BehaviourTree.Node rhs) => { if (lhs.rect.position.x < rhs.rect.position.x) { return(-1); } return(1); }); } } } if (scrollWheel) { zoom = (float)Math.Round(Math.Min(2.0f, Math.Max(0.6f, zoom + e.delta.y / 15)), 2); Repaint(); } Matrix4x4 oldMatrix = GUI.matrix; Matrix4x4 matTrans = Matrix4x4.TRS(vanishingPoint, Quaternion.identity, Vector3.one); Matrix4x4 matScale = Matrix4x4.Scale(new Vector3(zoom, zoom, 1.0f)); GUI.matrix = matTrans * matScale * matTrans.inverse; if (EditorState.MakeTransition == editorState && null != selectedNode) { Rect mouseRect = new Rect(e.mousePosition.x, e.mousePosition.y, 10, 10); BehaviourTree.Node.DrawConnection(selectedNode.rect, mouseRect); Repaint(); } for (int i = 0; i < AIEditorWindow.manager.nodes.Count; i++) { AIEditorWindow.manager.nodes[i].DrawConnection(); } BeginWindows(); for (int i = 0; i < AIEditorWindow.manager.nodes.Count; i++) { AIEditorWindow.manager.nodes[i].rect = GUI.Window(i, AIEditorWindow.manager.nodes[i].rect, DrawNode, AIEditorWindow.manager.nodes[i].title); } EndWindows(); GUI.matrix = oldMatrix; DrawSideGUI(); }