Ejemplo n.º 1
0
        /// <summary>
        /// Shows the node generic menu.
        /// </summary>
        void ShowNodeContextMenu()
        {
            var menu = new GenericMenu();

            menu.AddItem(new GUIContent("Reset"), false, delegate { StateUtility.ResetNode(target); m_SerializedNode = new SerializedNode(target); m_SerializedNode.Update(); });
            menu.AddSeparator(string.Empty);
            menu.AddItem(new GUIContent("Copy"), false, delegate { BehaviourTreeUtility.nodeToPaste = target; });
            menu.AddSeparator(string.Empty);
            if (target != null)
            {
                menu.AddItem(new GUIContent("Find Script"), false, delegate {
                    MonoScript script = BehaviourTreeUtility.GetNodeScript(target.GetType());
                    if (script != null)
                    {
                        EditorGUIUtility.PingObject(script);
                    }
                });
                menu.AddItem(new GUIContent("Edit Script"), false, delegate {
                    MonoScript script = BehaviourTreeUtility.GetNodeScript(target.GetType());
                    if (script != null)
                    {
                        AssetDatabase.OpenAsset(script);
                    }
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Find Script"));
                menu.AddDisabledItem(new GUIContent("Edit Script"));
            }

            menu.ShowAsContext();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Callback to add a new node.
        /// </summary>
        private void OnAddNode(ReorderableList list)
        {
            // Get all node scripts
            var nodeTypes = new List <System.Type>();

            foreach (System.Type type in BehaviourTreeUtility.GetNodeTypes())
            {
                if (!type.IsSubclassOf(typeof(BranchNode)))
                {
                    nodeTypes.Add(type);
                }
            }

            // Create the menu
            var menu = new UnityEditor.GenericMenu();

            // Add node types to the menu
            for (int i = 0; i < nodeTypes.Count; i++)
            {
                var nodeType = nodeTypes[i];
                var nodeInfo = AttributeUtility.GetAttribute <NodeInfoAttribute>(nodeType, false) ?? new NodeInfoAttribute();
                menu.AddItem(new GUIContent(nodeInfo.category + nodeType.Name), false, delegate() { ActionStateUtility.AddNode(m_ActionState, nodeType); RegisterEditorOnGUI(); });
            }

            // Show the context menu
            menu.ShowAsContext();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Refresh items.
        /// </summary>
        void Refresh()
        {
            m_Root            = new Category(null, string.Empty);
            m_CurrentCategory = m_Root;

            // Stores all scripts that inherites from ActionNode as child of m_Root
            foreach (var type in BehaviourTreeUtility.GetNodeTypes())
            {
                var nodeInfo = AttributeUtility.GetAttribute <NodeInfoAttribute>(type, false) ?? new NodeInfoAttribute();
                m_Root.AddChild(nodeInfo.category, type, nodeInfo);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Returns the script for the supplied node type.
 /// <param name= "type">The type of the node to search for the script.</param>
 /// <returns>A MonoScript of the supplied node type.</returns>
 /// </summary>
 public static MonoScript GetNodeScript(System.Type type)
 {
     if (type != null && type.IsSubclassOf(typeof(ActionNode)))
     {
         foreach (MonoScript script in BehaviourTreeUtility.GetNodeScripts())
         {
             if (script.GetClass() == type)
             {
                 return(script);
             }
         }
     }
     return(null);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Draws the script details in the gui.
        /// <param name="script">The script to be drawn.</param>
        /// </summary>
        void DrawScriptDetail(Script script)
        {
            GUILayout.BeginVertical(s_Styles.scriptDetailBox);

            // Draw disabled inspector
            if (m_NodeEditor != null && m_SelectedNodeSample != null)
            {
                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                EditorGUIUtility.LookLikeInspector();
                #endif

                // Store gui enabled
                var guiEnabled = GUI.enabled;
                GUI.enabled = false;

                GUILayout.Space(-1f);
                m_NodeEditor.DrawNode(m_SelectedNodeSample);
                GUILayout.Space(8f);

                // Restore gui enabled
                GUI.enabled = guiEnabled;

                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                EditorGUIUtility.LookLikeControls();
                #endif
            }

            // Draws description?
            if (script.description != string.Empty)
            {
                EditorGUILayout.LabelField(string.Empty, "Description: " + script.description, s_Styles.description);
            }

            // Draw parent
            // Update active node
            var activeNode = BehaviourWindow.activeNode;
            UpdateActiveNode(activeNode);

            // Add to a tree
            InternalBehaviourTree activeTree = BehaviourWindow.activeTree;

            if (activeTree != null)
            {
                GUILayout.BeginHorizontal();
                EditorGUILayout.PrefixLabel("Parent");
                if (activeNode != null && m_ActiveNodeType != null)
                {
                    EditorGUILayout.LabelField(new GUIContent(activeNode.name + m_ActiveNodeTypeName, m_ActiveNodeIcon, m_ActiveNodeInfo.description), s_Styles.titleText);
                }
                else
                {
                    EditorGUILayout.LabelField("null", s_Styles.errorLabel);
                }
                GUILayout.EndHorizontal();

                var guiEnabled2  = GUI.enabled;
                var activeBranch = BehaviourWindow.activeNode as BranchNode;
                GUI.enabled = activeTree != null;
                if (GUILayout.Button(activeBranch != null ? "Add as Child of " + activeBranch.name : "Add as Root", GUILayout.ExpandWidth(false)))
                {
                    ActionNode newNode = null;
                    if (activeBranch != null)
                    {
                        newNode = BehaviourTreeUtility.AddNode(activeBranch, script.type);
                    }
                    else
                    {
                        newNode = BehaviourTreeUtility.AddNode(activeTree, script.type);
                    }

                    // Select the new node
                    if (newNode != null)
                    {
                        BehaviourWindow.activeNodeID = newNode.instanceID;
                    }
                }
                GUI.enabled = guiEnabled2;
            }
            else
            {
                // Add to an ActionState
                var actionState = BehaviourWindow.activeState as InternalActionState;

                if (actionState != null && GUILayout.Button("Add Node", GUILayout.ExpandWidth(false)))
                {
                    ActionNode newNode = ActionStateUtility.AddNode(actionState, script.type);
                    // Select the new node
                    if (newNode != null)
                    {
                        BehaviourWindow.activeNodeID = newNode.instanceID;
                    }
                }
            }

            GUILayout.EndVertical();

            GUILayout.Space(18f);

            // Workaround to avoid to close the GUIPropertyField on click
            if (Event.current.type == EventType.Repaint)
            {
                m_LastRect = GUILayoutUtility.GetLastRect();
            }
            if (Event.current.type == EventType.MouseDown && m_LastRect.Contains(Event.current.mousePosition))
            {
                Event.current.Use();
            }
        }