/// <summary>
        /// Because it's not possible to store editor objects in non-editor objects
        /// this rigamoral is needed to find the BehaviorTree blueprint.
        /// </summary>
        /// <param name="ohBehaveAI"></param>
        /// <returns></returns>
        private OhBehaveTreeBlueprint GetBlueprintFor(OhBehaveAI ohBehaveAI)
        {
            if (string.IsNullOrEmpty(ohBehaveAI.jsonFilepath))
            {
                return(null);
            }

            if (!File.Exists(Application.streamingAssetsPath + ohBehaveAI.jsonFilepath))
            {
                return(null);
            }

            StreamReader reader = new StreamReader(
                Application.streamingAssetsPath + ohBehaveAI.jsonFilepath);
            string fileString = reader.ReadToEnd();

            reader.Close();

            JsonBehaviourTree tree = JsonUtility.FromJson <JsonBehaviourTree>(fileString);

            if (string.IsNullOrEmpty(tree.blueprintGUID))
            {
                Debug.LogError("No blueprints GUID");
                return(null);
            }

            var blueprint = AssetDatabase.LoadAssetAtPath <OhBehaveTreeBlueprint>(
                AssetDatabase.GUIDToAssetPath(tree.blueprintGUID));

            return(blueprint);
        }
        /// <summary>
        /// Only called when first constructed.
        /// </summary>
        /// <param name="behaviourAI"></param>
        /// <param name="newJsonFilepath"></param>
        public void Initialize(OhBehaveAI behaviourAI, string jsonFilepath)
        {
            if (!AssetDatabase.IsValidFolder(blueprintsPath))
            {
                string guid = AssetDatabase.CreateFolder(
                    Path.GetDirectoryName(blueprintsPath),
                    Path.GetFileName(blueprintsPath));
                blueprintsPath = AssetDatabase.GUIDToAssetPath(guid);
            }


            AssetDatabase.CreateAsset(this,
                                      blueprintsPath + "/" + blueprintsPrefix
                                      + Path.GetFileNameWithoutExtension(jsonFilepath)
                                      + GetInstanceID() + ".asset");


            string blueprintGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(this));


            ohBehaveAI      = behaviourAI;
            behaviourSource = ohBehaveAI.GetComponent <OhBehaveActions>();


            savedNodes = new List <NodeEditorObject>();


            jsonTreeData               = new JsonBehaviourTree();
            jsonTreeData.name          = Path.GetFileNameWithoutExtension(jsonFilepath);
            jsonTreeData.blueprintGUID = blueprintGUID;
            string jsonString = JsonUtility.ToJson(jsonTreeData, true);

            StreamWriter writer = new StreamWriter(jsonFilepath);

            writer.WriteLine(jsonString);
            writer.Close();
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            string relativePath = jsonFilepath.Replace("Assets/StreamingAssets", "");

            ohBehaveAI.jsonFilepath = relativePath;
            jsonGUID = AssetDatabase.AssetPathToGUID(jsonFilepath);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.SetDirty(this);
        }
        public bool Open(OhBehaveAI ohBehaveAI)
        {
            currentAIBehaviour = ohBehaveAI;

            treeBlueprint = GetBlueprintFor(ohBehaveAI);
            if (treeBlueprint == null)
            {
                Repaint();
                return(false);
            }

            treeBlueprint.ohBehaveAI = ohBehaveAI;
            treeBlueprint.ConstructNodes();

            if (zoomer != null)
            {
                zoomer.Reset(treeBlueprint.zoomerSettings);
            }
            window.Show();
            Repaint();
            return(true);
        }
        private void Save(bool isValidTree)
        {
            if (ohBehaveAI == null)
            {
                if (behaviourSource != null)
                {
                    ohBehaveAI = behaviourSource.bsm;
                }
                else
                {
                    Debug.LogError("ohBehaveAI could not be found - Behaviour source invalid");
                }

                if (ohBehaveAI == null)
                {
                    Debug.LogError("ohBehaveAI could not be found");
                    return;
                }
            }

            //if (isValidTree)
            {
                List <JsonNodeData> tree = new List <JsonNodeData>();
                jsonTreeData.actionSource = behaviourSource;
                jsonTreeData.rootNode     = AddNodeToTreeWithChildren(
                    GetNodeObjectByIndex(ROOT_INDEX), ROOT_NODE_PARENT_INDEX, ref tree);

                jsonTreeData.tree = tree.ToArray();
                StreamWriter writer = new StreamWriter(Application.streamingAssetsPath + ohBehaveAI.jsonFilepath);
                writer.WriteLine(JsonUtility.ToJson(jsonTreeData, true));
                writer.Close();
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.SetDirty(this);
        }
 public void OpenSaveFilePanel(OhBehaveAI instance)
 {
     openSaveFileChooser      = true;
     aiRequestingNewBlueprint = instance;
 }
        void Update()
        {
            if (Selection.activeGameObject != null)
            {
                OhBehaveAI ohBehaveSM = Selection.activeGameObject.GetComponent <OhBehaveAI>();
                if (ohBehaveSM != null && ohBehaveSM != currentAIBehaviour)
                {                 // switch to the currently selected gameobjects behavior tree
                    Open(ohBehaveSM);
                    return;
                }
            }

            if (openFileChooser)
            {
                openFileChooser = false;
                string path = EditorUtility.OpenFilePanelWithFilters(
                    "Choose new OhBehave file",
                    "Assets/StreamingAssets/" + userNodeFolder,
                    new string[] { "OhBehaveTree Json file", "OhJson" });

                if (!string.IsNullOrEmpty(path))
                {
                    string relativePath = path.Replace(Application.streamingAssetsPath, "");
                    chooseNewJsonFor.jsonFilepath = relativePath;
                    Open(chooseNewJsonFor);
                }

                chooseNewJsonFor = null;
            }

            if (treeBlueprint == null)
            {
                if (Selection.activeObject != null)
                {
                    OhBehaveTreeBlueprint testIfTree =
                        Selection.activeObject as OhBehaveTreeBlueprint;
                    if (testIfTree != null)
                    {
                        treeBlueprint = testIfTree;
                        Repaint();
                    }
                }
            }
            else
            {
                treeBlueprint.PendingDeletes();
                Repaint();
            }

            if (openSaveFileChooser)
            {
                if (!AssetDatabase.IsValidFolder("Assets/StreamingAssets/" + userNodeFolder))
                {
                    if (!AssetDatabase.IsValidFolder("Assets/StreamingAssets/"))
                    {
                        AssetDatabase.CreateFolder("Assets", "StreamingAssets");
                    }
                    if (!AssetDatabase.IsValidFolder("Assets/StreamingAssets/" + DefaultNodeFolder))
                    {
                        AssetDatabase.CreateFolder("Assets/StreamingAssets", DefaultNodeFolder);
                    }
                    userNodeFolder = DefaultNodeFolder;
                    EditorPrefs.SetString(UserNodeFolderKey, userNodeFolder);
                }

                string nodename = "NewOhBehaveTree";
                int    num      = AssetDatabase.FindAssets(nodename,
                                                           new string[] { "Assets/StreamingAssets/" + userNodeFolder }).Length;
                if (num != 0)
                {
                    nodename += " (" + num + ")";
                }

                var path = EditorUtility.SaveFilePanelInProject(
                    "Create New Json Behavior State Machine", nodename, "OhJson",
                    "Where to save json file?", "Assets/StreamingAssets/" + userNodeFolder);
                if (path.Length != 0)
                {
                    // check if user is using a folder that isn't the default
                    if (Path.GetFileName(Path.GetDirectoryName(path)) != userNodeFolder)
                    {
                        userNodeFolder = Path.GetFileName(Path.GetDirectoryName(path));
                        EditorPrefs.SetString(UserNodeFolderKey, userNodeFolder);
                    }

                    var machineBlueprint = CreateInstance <OhBehaveTreeBlueprint>();
                    machineBlueprint.Initialize(aiRequestingNewBlueprint, path);

                    Open(aiRequestingNewBlueprint);
                }

                aiRequestingNewBlueprint = null;
                openSaveFileChooser      = false;
            }
        }
 public void OpenFileChooser(OhBehaveAI ohBehave)
 {
     openFileChooser  = true;
     chooseNewJsonFor = ohBehave;
 }
Пример #8
0
 public void OnEnable()
 {
     instance = (OhBehaveAI)target;
 }