2026-01-06 14:24:23 +08:00
|
|
|
|
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<string>();
|
|
|
|
|
|
private Dictionary<string, string> _paths = new();
|
|
|
|
|
|
|
|
|
|
|
|
[MenuItem("Evo/剧本编辑器/创建剧本")]
|
|
|
|
|
|
private static void Open()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 打开一个浮动窗口
|
|
|
|
|
|
GetWindow<GraphCreateWindow>( "创建配置" ) ;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
|
{
|
|
|
|
|
|
minSize = maxSize = new Vector2(300, 200);
|
2026-01-08 17:31:26 +08:00
|
|
|
|
|
|
|
|
|
|
// 记录Package地址并返回选项
|
|
|
|
|
|
var packages = GraphEditorTools.GetPackages();
|
|
|
|
|
|
_paths.Clear();
|
|
|
|
|
|
packages.ForEach(path => _paths.Add(Path.GetFileName(path), path));
|
|
|
|
|
|
_options = _paths.Keys.ToArray();
|
2026-01-06 14:24:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnGUI()
|
|
|
|
|
|
{
|
|
|
|
|
|
GUILayout.Space(15);
|
|
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
|
|
|
|
GUILayout.Space(10);
|
|
|
|
|
|
EditorGUILayout.BeginVertical();
|
|
|
|
|
|
|
|
|
|
|
|
// Package选项
|
2026-01-08 17:31:26 +08:00
|
|
|
|
_selectedIndex = GraphEditorTools.Popup("请选择剧本所在的Package:", _selectedIndex, _options);
|
2026-01-06 14:24:23 +08:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
2026-01-08 17:31:26 +08:00
|
|
|
|
CreateScriptGraph(_options[_selectedIndex]);
|
2026-01-06 14:24:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogException(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EditorGUILayout.EndVertical();
|
|
|
|
|
|
GUILayout.Space(10);
|
|
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 创建剧本
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="packageID">包体ID</param>
|
|
|
|
|
|
private void CreateScriptGraph(string packageID)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 检查剧本名称是否有效
|
|
|
|
|
|
if (string.IsNullOrEmpty(_scriptName))
|
|
|
|
|
|
{
|
|
|
|
|
|
_tip = "剧本名称不能为空";
|
|
|
|
|
|
_showTip = true;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (_paths.TryGetValue(packageID, out var path))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 包体目录排空
|
2026-01-08 17:31:26 +08:00
|
|
|
|
var graphDir = Path.Combine(Application.dataPath, "StoryEditor", "Graphs",
|
|
|
|
|
|
$"{(string.IsNullOrEmpty(packageID) ? "default" : packageID)}");
|
2026-01-06 14:24:23 +08:00
|
|
|
|
if (!Directory.Exists(graphDir))
|
2026-01-08 17:31:26 +08:00
|
|
|
|
{
|
2026-01-06 14:24:23 +08:00
|
|
|
|
Directory.CreateDirectory(graphDir);
|
2026-01-08 17:31:26 +08:00
|
|
|
|
}
|
2026-01-06 14:24:23 +08:00
|
|
|
|
|
|
|
|
|
|
// 检查资源存在性
|
2026-01-08 17:31:26 +08:00
|
|
|
|
var graphFilePath = Path.Combine(graphDir.Replace(Application.dataPath,"Assets"), $"{_scriptName}.asset");
|
2026-01-06 14:24:23 +08:00
|
|
|
|
var existAsset = AssetDatabase.LoadAssetAtPath<ScriptGraph>(graphFilePath);
|
|
|
|
|
|
if (existAsset)
|
|
|
|
|
|
{
|
|
|
|
|
|
_tip = "该名称的剧本已存在";
|
|
|
|
|
|
_showTip = true;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建剧本图表
|
2026-01-08 17:31:26 +08:00
|
|
|
|
var graphPath = Path.Combine(path, "Main","Res","Graphs", $"{_scriptName}.sg.json").Replace(Application.dataPath, "").Replace('\\', '/');
|
|
|
|
|
|
graphPath = graphPath[1..];
|
2026-01-06 14:24:23 +08:00
|
|
|
|
var graph = CreateInstance<ScriptGraph>();
|
|
|
|
|
|
graph.packageID = packageID;
|
|
|
|
|
|
graph.graphPath = graphPath;
|
|
|
|
|
|
|
|
|
|
|
|
AssetDatabase.CreateAsset(graph, graphFilePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError($"未记录Package路径:{packageID}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|