예제 #1
0
        public static void OpenEditor(BehaviourTreeAsset asset)
        {
            BehaviourTreeDesignerWindow window = GetWindow <BehaviourTreeDesignerWindow>();

            window.InitWith(asset);
            window.Show();
        }
예제 #2
0
        public void InitWith(BehaviourTreeAsset asset)
        {
            if (EditorApplication.isPlaying)
            {
                BindBehaviourTreeEvent(false);
            }
            ContextMenu.Hide();
            Runner         = null;
            BehaviourAsset = asset;
            TreeCanvas.ClearElements();
            CommentCanvas.ClearElements();
            TreeGraph.Clear();

            mRootGUI = new BehaviourRootGUI(this);
            mRootGUI.UpdateLocalData();
            TreeCanvas.AddElement(mRootGUI);
            TreeGraph.AddNode(mRootGUI);

            if (BehaviourAsset != null)
            {
                ImportTreeData();
            }
            ResetIdCounter();
            RebuildExecutionOrder();
            UpdateStateInfo();
        }
        public bool SetSelectedAsset()
        {
            BehaviourTreeRunner tmprunner = null;
            BehaviourTreeAsset  tmpasset  = null;

            if (Selection.activeGameObject != null)
            {
                tmprunner = Selection.activeGameObject.GetComponent <BehaviourTreeRunner>();
            }
            else if (Selection.activeObject != null)
            {
                tmpasset = Selection.activeObject as BehaviourTreeAsset;
            }
            if (tmprunner != null)
            {
                SetBehaviourTreeRunner(tmprunner);
                return(true);
            }
            if (tmpasset == null && BehaviourTreeEditor.ActiveBTEditor != null)
            {
                var his = BehaviourTreeEditor.ActiveBTEditor.LastAssetInHistory;
                if (!string.IsNullOrEmpty(his))
                {
                    tmpasset = AssetDatabase.LoadAssetAtPath <BehaviourTreeAsset>(his);
                }
            }
            if (tmpasset != null)
            {
                SetBehaviourTreeAsset(tmpasset);
                return(true);
            }
            return(false);
        }
        public void SetBehaviourTreeAsset(BehaviourTreeAsset asset)
        {
            if (asset == tree)
            {
                return;
            }
            if (tmpAsset != null)
            {
                BehaviourTreeAsset.DestroyAsset(tmpAsset, true);
                tmpAsset = null;
            }
            tree = asset;
            var bind = runner == null || asset == null ? null : runner.GetBinder(asset);

            if (bind != null)
            {
                runner = bind.Runner;
            }
            runtimeBinder = bind;
            if (tree != null)
            {
                tmpAsset = tree.Clone();
            }
            if (updateTreeAsset != null)
            {
                updateTreeAsset();
            }
        }
        private void OnSelectionChange()
        {
            BehaviourTreeAsset btAsset = Selection.activeObject as BehaviourTreeAsset;

            if (btAsset)
            {
                _view.SetupView(btAsset);
            }
        }
        private static void OpenWindow(BehaviourTreeAsset btAsset = null)
        {
            BehaviourTreeEditor wnd = GetWindow <BehaviourTreeEditor>();

            wnd.titleContent = new GUIContent("BehaviourTreeEditor");

            if (btAsset)
            {
                wnd._view.SetupView(btAsset);
            }
        }
 public void Dispose()
 {
     if (tmpAsset != null)
     {
         BehaviourTreeAsset.DestroyAsset(tmpAsset, true);
         tmpAsset = null;
     }
     runner = null;
     tree   = null;
     EditorApplication.playModeStateChanged -= OnPlayModeChanged;
 }
        public static bool OnOpenAsset(int instanceId, int line)
        {
            BehaviourTreeAsset btAsset = EditorUtility.InstanceIDToObject(instanceId) as BehaviourTreeAsset;

            if (btAsset != null)
            {
                OpenWindow(btAsset);
                return(true);
            }

            return(false);
        }
예제 #9
0
        public static void OpenBTEditor(BehaviourTreeAsset asset)
        {
            var window = ActiveBTEditor;

            if (window == null)
            {
                window = GetWindow <BehaviourTreeEditor>(typeof(SceneView));
                window.Show();
            }
            window.AddDelayTask(ACT_OPEN_ASSET, () =>
            {
                window.mAssetBinder.SetBehaviourTreeAsset(asset);
            });
        }
        public void SaveAsset()
        {
            if (tmpAsset == null)
            {
                return;
            }
            List <BTNode> children = new List <BTNode>();
            var           path     = tree == null ? null : AssetDatabase.GetAssetPath(tree);

            tmpAsset.EditorResort();
            if (string.IsNullOrEmpty(path))
            {
                path = EditorUtility.SaveFilePanelInProject("Save Behaviour Tree", AssetName, "asset", "Save behaviour asset.");
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }
                if (File.Exists(path))
                {
                    AssetDatabase.DeleteAsset(path);
                }
                tree = tmpAsset.Clone();
                var name = Path.GetFileName(path);
                if (name.EndsWith(".asset"))
                {
                    name = name.Substring(0, name.Length - 6);
                }
                tree.name = name;
                AssetDatabase.CreateAsset(tree, path);
                tree.GetAllNodes(children);
                foreach (var t in children)
                {
                    AssetDatabase.AddObjectToAsset(t.Asset, path);
                }
                AssetDatabase.ImportAsset(path);
            }
            else
            {
                tmpAsset.EditorMergeTo(path, tree);
                tree.GetAllNodes(children);
                foreach (var t in children)
                {
                    AssetDatabase.AddObjectToAsset(t.Asset, path);
                }
                AssetDatabase.ImportAsset(path);
            }
            //AssetDatabase.SaveAssets();
        }
예제 #11
0
        internal void SetupView(BehaviourTreeAsset tree)
        {
            _btAsset = tree;

            graphViewChanged -= OnGraphViewChanged;
            // Clear previous tree
            DeleteElements(graphElements);
            graphViewChanged += OnGraphViewChanged;

            _btAsset.CreateRoot();

            // Create Nodes
            _btAsset.InspectorNodes.ForEach(InstantiateNodeView);

            //Create Edges
            _btAsset.InspectorNodes.ForEach(CreateEdges);
        }
예제 #12
0
 void GetServices()
 {
     mAsset = editor.TargetTree;
     mServiceNodes.Clear();
     mServiceNames.Clear();
     mServiceModules.Clear();
     if (mAsset != null)
     {
         mAsset.GetAllNodes(mServiceNodes, (x) => x.isService);
     }
     for (int i = 0; i < mServiceNodes.Count; i++)
     {
         var mod = AIModules.Get(mServiceNodes[i]);
         mServiceModules.Add(mod);
         mServiceNames.Add(GetServiceName(mServiceNodes[i], mod));
     }
 }
        public void SetBehaviourTreeRunner(BehaviourTreeRunner runner)
        {
            if (runner == null)
            {
                this.runner        = null;
                this.runtimeBinder = null;
                return;
            }
            if (this.runner == null)
            {
                this.runner = null;
            }
            var asset = runner == null ? tree : runner.SourceAsset;

            if (asset == null)
            {
                asset = tree;
            }
            var bind = runner == null ? null : runner.GetBinder(asset);

            if (asset == tree && this.runner == runner && runtimeBinder == bind)
            {
                return;
            }
            this.runner   = runner;
            this.tree     = asset;
            runtimeBinder = bind;
            if (tmpAsset != null)
            {
                BehaviourTreeAsset.DestroyAsset(tmpAsset, true);
                tmpAsset = null;
            }
            if (tree != null)
            {
                if (tree != null)
                {
                    tmpAsset = tree.Clone();
                }
                if (updateTreeAsset != null)
                {
                    updateTreeAsset();
                }
            }
        }
예제 #14
0
        void AddHistory(BehaviourTreeAsset asset)
        {
            if (asset == null)
            {
                return;
            }
            var path = AssetDatabase.GetAssetPath(asset);

            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            mHistory.Remove(path);
            while (mHistory.Count >= HISTORY_SIZE)
            {
                mHistory.RemoveAt(mHistory.Count - 1);
            }
            mHistory.Insert(0, path);
        }
 private void OnPlayModeChanged(PlayModeStateChange stat)
 {
     if (stat == PlayModeStateChange.EnteredPlayMode || stat == PlayModeStateChange.EnteredEditMode)
     {
         if (runner == null)
         {
             runner = null;
         }
         if (tree == null)
         {
             tree = null;
         }
         if (tmpAsset != null)
         {
             BehaviourTreeAsset.DestroyAsset(tmpAsset, true);
             tmpAsset = null;
         }
         if (tree == null)
         {
             if (SetSelectedAsset())
             {
                 return;
             }
         }
         else
         {
             var go    = Selection.activeGameObject;
             var tmpru = go == null ? null : go.GetComponent <BehaviourTreeRunner>();
             if (tmpru != null && tmpru.SourceAsset == tree)
             {
                 runner = tmpru;
             }
         }
         runtimeBinder = runner == null ? null : runner.ActiveBinder;
         if (updateTreeAsset != null)
         {
             updateTreeAsset();
         }
     }
 }
 public void SetBehaviourBinder(AssetBinder binder)
 {
     if (binder == null || runtimeBinder == binder)
     {
         return;
     }
     if (tmpAsset != null)
     {
         BehaviourTreeAsset.DestroyAsset(tmpAsset, true);
         tmpAsset = null;
     }
     runner        = binder.Runner;
     tree          = binder.Source;
     runtimeBinder = binder;
     if (tree != null)
     {
         tmpAsset = tree.Clone();
     }
     if (updateTreeAsset != null)
     {
         updateTreeAsset();
     }
 }
 public void Reset()
 {
     if (runner != null && tree == null)
     {
         tree = runner.SourceAsset;
     }
     if (tree != null)
     {
         runtimeBinder = runner == null ? null : runner.GetBinder(tree);
         if (tmpAsset == null)
         {
             tmpAsset = tree.Clone();
         }
         else
         {
             tree.EditorMergeTo(null, tmpAsset);
         }
         if (updateTreeAsset != null)
         {
             updateTreeAsset();
         }
     }
 }