/// <summary> /// Saves a scriptable object behavior tree and sets the active asset back to the behavior manager /// </summary> /// <param name="behaviorManager"></param> /// <param name="filePath"></param> /// <param name="asset"></param> public static void SaveBehaviorAsset(this BehaviorManager behaviorManager, string filePath, BehaviorTreeManagerAsset asset, Merge root = null) { if (asset == null) { asset = ScriptableObject.CreateInstance <BehaviorTreeManagerAsset>(); } var runnerElementList = new List <BehaviorTreeElement>(); Debug.Log("Attempting save at path: " + filePath); int indexS = filePath.LastIndexOf("/") + 1; int indexD = filePath.LastIndexOf(".") - indexS; asset.name = filePath.Substring(indexS, indexD); var json = asset.RunnerElementsJSON; if (behaviorManager != null) { behaviorManager.Reinitialize(); asset.MilliSecondsBetweenTicks = behaviorManager.MilliSecondsBetweenTicks; asset.TimesToTick = behaviorManager.TimesToTick; TreeElementUtility.TreeToList(behaviorManager.Runner, runnerElementList); } if (root != null) { TreeElementUtility.TreeToList(root, runnerElementList); } if (json == "" || runnerElementList.Count == 0) { var runner = new Merge("Extension Root", -1, -1); runnerElementList.Add(runner); json = JsonConvert.SerializeObject(runnerElementList, Formatting.Indented); } json = JsonConvert.SerializeObject(runnerElementList, Formatting.Indented); asset.RunnerElementsJSON = json; Debug.Log("JSON Saved: " + asset.RunnerElementsJSON); var curPath = AssetDatabase.GetAssetPath(asset); if (curPath == null || curPath == "") { Debug.Log("Creating asset: " + filePath); AssetDatabase.CreateAsset(asset, filePath); } //AssetDatabase.Refresh(); EditorUtility.SetDirty(asset); AssetDatabase.SaveAssets(); }