- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using UnityEngine.UIElements;
- using System.IO;
- using UnityEditor.UIElements;
- public class AIGraphWindow : EditorWindow
- {
- public FSMSave saveFile;
- public string rootDirectory;
- FSMGraphView graphView;
- Button saveButton;
- [MenuItem("Window/FSM Graph")]
- static void Init()
- {
- AIGraphWindow window = (AIGraphWindow)GetWindow<AIGraphWindow>("FSM Graph");
- window.Show();
- }
- private void OnEnable()
- {
- // AddGraphView();
- AddGraphView();
- }
- private void AddGraphView()
- {
- graphView = new FSMGraphView();
- graphView.instance = this;
- graphView.StretchToParentSize();
-
- rootVisualElement.Add(graphView);
- AddToolbar();
- }
- private void OnGUI()
- {
- // graphView.OnGUI();
- /* if (saveFile == null)
- {
- if (GUILayout.Button("Create FSM"))
- {
- string path = EditorUtility.OpenFolderPanel("Choose save folder", Application.dataPath, string.Empty);
- string localPath = Path.Combine("Assets", Path.GetRelativePath(Application.dataPath, path));
- rootDirectory = localPath;
- if (path != string.Empty)
- {
- if (!Directory.Exists(Path.Combine(localPath, "States")))
- {
- AssetDatabase.CreateFolder(localPath, "States");
- }
- if (!Directory.Exists(Path.Combine(localPath, "Transitions")))
- {
- AssetDatabase.CreateFolder(localPath, "Transitions");
- }
- if (!File.Exists(Path.Combine(localPath, "Nodes.asset")))
- {
- FSMSave sv = ScriptableObject.CreateInstance<FSMSave>();
- if (sv.states == null)
- {
- sv.states = new List<FSMSave.SavedElement>();
- }
- AssetDatabase.CreateAsset(sv, Path.Combine(localPath, "Nodes.asset"));
- }
- }
- }
- if (GUILayout.Button("Load FSM"))
- {
- string path = EditorUtility.OpenFilePanel("Choose save file", Application.dataPath, string.Empty);
- if (path != string.Empty)
- {
- string localPath = Path.Combine("Assets", Path.GetRelativePath(Application.dataPath, path));
- rootDirectory = Path.GetDirectoryName(localPath);
- saveFile = AssetDatabase.LoadAssetAtPath<FSMSave>(localPath);
- AddGraphView();
- AddToolbar();
- graphView.Load(saveFile);
- }
- }
- }
- */
- if (saveButton != null)
- {
- if (saveFile == null)
- {
- saveButton.visible = false;
- } else
- {
- saveButton.visible = true;
- }
- }
- }
- public void CreateFSM()
- {
- string path = EditorUtility.OpenFolderPanel("Choose save folder", Application.dataPath, string.Empty);
- if (path == string.Empty) return;
- string localPath = Path.Combine("Assets", Path.GetRelativePath(Application.dataPath, path));
- rootDirectory = localPath;
- if (path != string.Empty)
- {
- if (!Directory.Exists(Path.Combine(localPath, "States")))
- {
- AssetDatabase.CreateFolder(localPath, "States");
- }
- if (!Directory.Exists(Path.Combine(localPath, "Transitions")))
- {
- AssetDatabase.CreateFolder(localPath, "Transitions");
- }
- if (!File.Exists(Path.Combine(localPath, "Nodes.asset")))
- {
- FSMSave sv = ScriptableObject.CreateInstance<FSMSave>();
- if (sv.states == null)
- {
- sv.states = new List<FSMSave.SavedElement>();
- }
- AssetDatabase.CreateAsset(sv, Path.Combine(localPath, "Nodes.asset"));
- saveFile = sv;
- graphView.Load(sv);
- }
-
- }
- }
- public void LoadFSM()
- {
- string path = EditorUtility.OpenFilePanel("Choose save file", Application.dataPath, string.Empty);
- if (path != string.Empty)
- {
- string localPath = Path.Combine("Assets", Path.GetRelativePath(Application.dataPath, path));
- rootDirectory = Path.GetDirectoryName(localPath);
- saveFile = AssetDatabase.LoadAssetAtPath<FSMSave>(localPath);
-
- graphView.Load(saveFile);
- }
- }
- void AddToolbar()
- {
- Toolbar toolbar = new Toolbar();
- Label t = new Label(rootDirectory);
-
- saveButton = new Button(() =>
- {
- graphView.Save();
- t.text = rootDirectory;
- });
- saveButton.text = "Save";
- Button newFSMButton = new Button(() =>
- {
- CreateFSM();
- t.text = rootDirectory;
- });
- newFSMButton.text = "New";
- Button loadButton = new Button(() =>
- {
- LoadFSM();
- t.text = rootDirectory;
- });
- loadButton.text = "Load";
- Button editTransitionButton = new Button(()=> {
- TransitionEditorWindow.Init(graphView.OpenTransitions());
- });
- editTransitionButton.text = "Edit Selected Transition";
- toolbar.Add(t);
- toolbar.Add(saveButton);
- toolbar.Add(loadButton);
- toolbar.Add(newFSMButton);
- toolbar.Add(editTransitionButton);
- rootVisualElement.Add(toolbar);
- }
- }