示例#1
0
        /// <summary>
        /// Assumes that the path is already valid.
        /// </summary>
        /// <param name="path"></param>
        private void loadTree(string path)
        {
            int assetIndex = path.IndexOf("/Assets/");

            path = path.Substring(assetIndex + 1);

            var tree = AssetDatabase.LoadAssetAtPath <Core.BehaviourTree>(path);

            _window.SetTree(tree);
        }
        /// <summary>
        /// Opens the tree in a Bonsai Window.
        /// </summary>
        /// <param name="tree">The tree to open</param>
        /// <returns>
        /// The window that opens the tree. Null if already opened.
        /// </returns>
        public static BonsaiWindow OpenTree(BehaviourTree tree, BonsaiEditor.Mode mode = BonsaiEditor.Mode.Edit)
        {
            if (!tree)
            {
                return(null);
            }

            // Try to find an editor window without a canvas...
            var windows = Resources.FindObjectsOfTypeAll <BonsaiWindow>();

            bool isAlreadyOpened = windows.Any(w => w.Tree == tree);

            if (isAlreadyOpened)
            {
                return(null);
            }

            // Find a window without any tree.
            BonsaiWindow window = windows.FirstOrDefault(w => w.Tree == null);

            // No windows available, make a new one.
            if (!window)
            {
                window = CreateInstance <BonsaiWindow>();
                window.Show();
            }

            window.SetTree(tree, mode);
            return(window);
        }
        private static bool OpenCanvasAsset(int instanceID, int line)
        {
            var treeSelected = EditorUtility.InstanceIDToObject(instanceID) as BehaviourTree;

            if (treeSelected != null)
            {
                BonsaiWindow windowToUse = null;

                // Try to find an editor window without a canvas...
                var bonsaiWindows = Resources.FindObjectsOfTypeAll <BonsaiWindow>();
                foreach (var w in bonsaiWindows)
                {
                    // The canvas is already opened
                    if (w.Tree == treeSelected)
                    {
                        return(false);
                    }

                    // Found a window with no active canvas.
                    if (w.Tree == null)
                    {
                        windowToUse = w;
                        break;
                    }
                }

                // No windows available...just make a new one.
                if (!windowToUse)
                {
                    windowToUse = CreateInstance <BonsaiWindow>();
                    windowToUse.Show();
                }

                // If a tree asset was created but has no blackboard, add one upon opening.
                // This is for convenience.
                BonsaiSaver.AddBlackboardIfMissing(treeSelected);

                windowToUse.SetTree(treeSelected);
                windowToUse.Repaint();
                return(true);
            }

            return(false);
        }
示例#4
0
        private static bool OpenCanvasAsset(int instanceID, int line)
        {
            var treeSelected = EditorUtility.InstanceIDToObject(instanceID) as Core.BehaviourTree;

            if (treeSelected != null)
            {
                BonsaiWindow windowToUse = null;

                // Try to find an editor window without a canvas...
                var bonsaiWindows = Resources.FindObjectsOfTypeAll <BonsaiWindow>();
                foreach (var w in bonsaiWindows)
                {
                    // The canvas is already opened
                    if (w.tree == treeSelected)
                    {
                        return(false);
                    }

                    // Found a window with no active canvas.
                    if (w.tree == null)
                    {
                        windowToUse = w;
                        break;
                    }
                }

                // No windows available...just make a new one.
                if (!windowToUse)
                {
                    windowToUse = EditorWindow.CreateInstance <BonsaiWindow>();
                    windowToUse.titleContent = new GUIContent("Bonsai");
                    windowToUse.Show();
                }

                windowToUse.SetTree(treeSelected);
                windowToUse.saveManager.InitState();
                windowToUse.Repaint();

                return(true);
            }

            return(false);
        }
        private void SwitchToViewModeIfRequired()
        {
            // Cannot go to view mode.
            if (!EditorApplication.isPlaying || !Selection.activeGameObject)
            {
                return;
            }

            var           btc        = Selection.activeGameObject.GetComponent <BonsaiTreeComponent>();
            BehaviourTree treeToView = btc ? btc.Tree : null;

            // There must be a non-null tree to view,
            // it must be a different tree than the active tree for this window,
            // and must not be opened somewhere else.
            if (treeToView && Tree != treeToView)
            {
                var windows = Resources.FindObjectsOfTypeAll <BonsaiWindow>();

                bool alreadyInView = windows.Any(w => w.Tree == treeToView);

                if (alreadyInView)
                {
                    return;
                }

                BonsaiWindow window = windows.FirstOrDefault(w => !w.Tree);

                // Have the window without a set tree to view the tree selected.
                if (window)
                {
                    window.SetTree(treeToView, BonsaiEditor.Mode.View);
                }
                else
                {
                    // View tree in this window.
                    SetTree(treeToView, BonsaiEditor.Mode.View);
                }
            }
        }