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