/// <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);
        }
Example #3
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);
        }