using System; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEngine; // ReSharper disable Unity.PerformanceCriticalCodeInvocation namespace Stary.Evo.StoryEditor.Editor { public class GraphCreateWindow : EditorWindow { private int _selectedIndex; private string _scriptName = "graph"; private bool _showTip; private string _tip; private string[] _options = Array.Empty(); private Dictionary _paths = new(); [MenuItem("Evo/剧本编辑器/创建剧本")] private static void Open() { // 打开一个浮动窗口 GetWindow( "创建配置" ) ; } private void OnEnable() { minSize = maxSize = new Vector2(300, 200); // 记录Package地址并返回选项 var packages = GraphEditorTools.GetPackages(); _paths.Clear(); packages.ForEach(path => _paths.Add(Path.GetFileName(path), path)); _options = _paths.Keys.ToArray(); } private void OnGUI() { GUILayout.Space(15); EditorGUILayout.BeginHorizontal(); GUILayout.Space(10); EditorGUILayout.BeginVertical(); // Package选项 _selectedIndex = GraphEditorTools.Popup("请选择剧本所在的Package:", _selectedIndex, _options); GUILayout.Space(15); EditorGUILayout.LabelField( "请输入剧本名称" , EditorStyles.boldLabel ) ; var newName = EditorGUILayout.TextField(_scriptName); if (_scriptName != newName) { _scriptName = newName; _showTip = false; } if (_showTip) { GUIStyle redBold = new GUIStyle(EditorStyles.boldLabel) { normal = { textColor = Color.red } }; EditorGUILayout.LabelField(_tip, redBold) ; } GUILayout.Space(15); // 创建按钮 if (GUILayout.Button("创建剧本")) { try { CreateScriptGraph(_options[_selectedIndex]); } catch (Exception e) { Debug.LogException(e); } } EditorGUILayout.EndVertical(); GUILayout.Space(10); EditorGUILayout.EndHorizontal(); } /// /// 创建剧本 /// /// 包体ID private void CreateScriptGraph(string packageID) { // 检查剧本名称是否有效 if (string.IsNullOrEmpty(_scriptName)) { _tip = "剧本名称不能为空"; _showTip = true; return; } if (_paths.TryGetValue(packageID, out var path)) { // 包体目录排空 var graphDir = Path.Combine(Application.dataPath, "StoryEditor", "Graphs", $"{(string.IsNullOrEmpty(packageID) ? "default" : packageID)}"); if (!Directory.Exists(graphDir)) { Directory.CreateDirectory(graphDir); } // 检查资源存在性 var graphFilePath = Path.Combine(graphDir.Replace(Application.dataPath,"Assets"), $"{_scriptName}.asset"); var existAsset = AssetDatabase.LoadAssetAtPath(graphFilePath); if (existAsset) { _tip = "该名称的剧本已存在"; _showTip = true; return; } // 创建剧本图表 var graphPath = Path.Combine(path, "Main","Res","Graphs", $"{_scriptName}.sg.json").Replace(Application.dataPath, "").Replace('\\', '/'); graphPath = graphPath[1..]; var graph = CreateInstance(); graph.packageID = packageID; graph.graphPath = graphPath; AssetDatabase.CreateAsset(graph, graphFilePath); } else { Debug.LogError($"未记录Package路径:{packageID}"); } } } }