【m】10. StoryEditor [1.1.0] - 2026-01-08

### Added
- 新增剧本设置窗口,在窗口中打开和导出剧本
### Fixed
- 修改Graph创建时的默认位置,防止热更分包导致脚本丢失报错
- 修改Graph导出时对加载器的加载逻辑,解决跨包无法加载的问题
This commit is contained in:
mzh
2026-01-08 17:31:26 +08:00
parent f3a4165c1e
commit f5a79aabf8
8 changed files with 264 additions and 104 deletions

View File

@@ -28,7 +28,12 @@ namespace Stary.Evo.StoryEditor.Editor
private void OnEnable()
{
minSize = maxSize = new Vector2(300, 200);
_options = GetPackages();
// 记录Package地址并返回选项
var packages = GraphEditorTools.GetPackages();
_paths.Clear();
packages.ForEach(path => _paths.Add(Path.GetFileName(path), path));
_options = _paths.Keys.ToArray();
}
private void OnGUI()
@@ -39,9 +44,7 @@ namespace Stary.Evo.StoryEditor.Editor
EditorGUILayout.BeginVertical();
// Package选项
EditorGUILayout.LabelField( "请选择剧本所在的Package" , EditorStyles.boldLabel ) ;
var newIndex = EditorGUILayout.Popup( _selectedIndex , _options ) ;
_selectedIndex = newIndex;
_selectedIndex = GraphEditorTools.Popup("请选择剧本所在的Package", _selectedIndex, _options);
GUILayout.Space(15);
@@ -69,7 +72,7 @@ namespace Stary.Evo.StoryEditor.Editor
{
try
{
CreateScriptGraph(_options[newIndex]);
CreateScriptGraph(_options[_selectedIndex]);
}
catch (Exception e)
{
@@ -82,68 +85,6 @@ namespace Stary.Evo.StoryEditor.Editor
EditorGUILayout.EndHorizontal();
}
/// <summary>
/// 获取所有Module Package
/// </summary>
private string[] GetPackages()
{
// 查找Modules目录
List<string> modules = new();
GetDirectoryPaths(Application.dataPath, modules, "Modules");
if (modules.Count == 0)
{
Debug.LogError("未找到任何Modules目录");
return Array.Empty<string>();
}
// 查找package
List<string> packages = new();
GetDirectoryPaths(modules[0], packages, "com.");
if (packages.Count == 0)
{
Debug.LogError("未找到任何Package");
return Array.Empty<string>();
}
// 记录Package地址并返回选项
_paths.Clear();
packages.ForEach(path => _paths.Add(Path.GetFileName(path), path));
return _paths.Keys.ToArray();
}
/// <summary>
/// 获取符合条件的目录
/// </summary>
/// <param name="root">查找起始目录</param>
/// <param name="result">查找结果</param>
/// <param name="title">筛选条件title</param>
/// <param name="tail">筛选条件tail</param>
private static void GetDirectoryPaths(string root, List<string> result, string title = null, string tail = null)
{
foreach (var dir in Directory.GetDirectories(root))
{
// ReSharper disable once ReplaceWithSingleAssignment.True
var check = true;
// 匹配文件头
if (!string.IsNullOrEmpty(title) && !Path.GetFileName(dir).StartsWith(title))
{
check = false;
}
// 匹配文件尾
if (!string.IsNullOrEmpty(tail) && !Path.GetFileName(dir).EndsWith(tail))
{
check = false;
}
if(check)
result.Add(dir.Replace('\\', '/'));
// 继续往下找
GetDirectoryPaths(dir, result, title);
}
}
/// <summary>
/// 创建剧本
/// </summary>
@@ -161,14 +102,15 @@ namespace Stary.Evo.StoryEditor.Editor
if (_paths.TryGetValue(packageID, out var path))
{
// 包体目录排空
var graphDir = Path.Combine(path, "Main","Res","Graphs");
var graphDir = Path.Combine(Application.dataPath, "StoryEditor", "Graphs",
$"{(string.IsNullOrEmpty(packageID) ? "default" : packageID)}");
if (!Directory.Exists(graphDir))
{
Directory.CreateDirectory(graphDir);
}
// 检查资源存在性
var graphPath = Path.Combine(graphDir, $"{_scriptName}.asset").Replace(Application.dataPath, "").Replace('\\', '/');
graphPath = graphPath[1..];
var graphFilePath = Path.Combine("Assets", graphPath);
var graphFilePath = Path.Combine(graphDir.Replace(Application.dataPath,"Assets"), $"{_scriptName}.asset");
var existAsset = AssetDatabase.LoadAssetAtPath<ScriptGraph>(graphFilePath);
if (existAsset)
{
@@ -178,6 +120,8 @@ namespace Stary.Evo.StoryEditor.Editor
}
// 创建剧本图表
var graphPath = Path.Combine(path, "Main","Res","Graphs", $"{_scriptName}.sg.json").Replace(Application.dataPath, "").Replace('\\', '/');
graphPath = graphPath[1..];
var graph = CreateInstance<ScriptGraph>();
graph.packageID = packageID;
graph.graphPath = graphPath;