Files
plugin-library/Assets/10.StoryEditor/Sample~/ARMazProSample/Script/TestScriptPlayer.cs
mzh cc3543519d 【m】[1.0.5] - 2026-01-06
### Changed
- 添加更新日志
- 修改加载器选择范围,允许指定程序集检查
### Fixed
- 处理Sample在UnityPackage窗口不显示的问题
2026-01-08 11:07:40 +08:00

167 lines
4.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Array = System.Array;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Stary.Evo.StoryEditor;
using Sirenix.OdinInspector;
using UnityEngine;
public class TestScriptPlayer : MonoBehaviour
{
[LabelText("指定包体ID"), SerializeField, ValueDropdown(nameof(GetPackages))]
private string packageID;
[LabelText("指定剧本名称"), SerializeField, ValueDropdown(nameof(GetScripts))]
private string scriptName;
private void Start()
{
var caption = GetComponentInChildren<SpriteRenderer>();
caption.color = Color.clear;
var audioClip = GetComponentInChildren<AudioSource>();
audioClip.volume = 0;
ScriptPlayer.Init(new ResourceLoader(), audioClip, caption);
}
private void OnDestroy()
{
ScriptPlayer.Release();
}
[Button]
public void Play()
{
_ = ScriptPlayer.Play(packageID, scriptName);
}
[Button]
public void Stop()
{
_ = ScriptPlayer.Stop();
}
#region
private Dictionary<string, string> _paths = new();
/// <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)
{
return Array.Empty<string>();
}
// 记录Package地址并返回选项
_paths.Clear();
packages.ForEach(path => _paths.Add(Path.GetFileName(path), path));
return _paths.Keys.ToArray();
}
/// <summary>
/// 获取Package中所有Script
/// </summary>
private string[] GetScripts()
{
// 未选Package
if (string.IsNullOrEmpty(packageID) || !_paths.ContainsKey(packageID))
return Array.Empty<string>();
List<string> scripts = new();
GetFilePaths(_paths[packageID], scripts, tail: ".sg.json");
return scripts.Count == 0 ? Array.Empty<string>() : scripts.Select(path => Path.GetFileName(path).Replace(".json", "")).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>
/// <param name="root">查找起始目录</param>
/// <param name="result">查找结果</param>
/// <param name="title">筛选条件title</param>
/// <param name="tail">筛选条件tail</param>
private static void GetFilePaths(string root, List<string> result, string title = null, string tail = null)
{
foreach (var file in Directory.GetFiles(root))
{
// ReSharper disable once ReplaceWithSingleAssignment.True
var check = true;
// 匹配文件头
if (!string.IsNullOrEmpty(title))
{
if(!Path.GetFileName(file).StartsWith(title))
check = false;
}
// 匹配文件尾
if (!string.IsNullOrEmpty(tail))
{
if (!Path.GetFileName(file).EndsWith(tail))
{
check = false;
}
}
if (check)
{
result.Add(file.Replace('\\', '/'));
}
}
// 继续往下找
foreach (var dir in Directory.GetDirectories(root))
{
GetFilePaths(dir, result, title, tail);
}
}
#endregion
}