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(); caption.color = Color.clear; var audioClip = GetComponentInChildren(); 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 _paths = new(); /// /// 获取所有Module Package /// private string[] GetPackages() { // 查找Modules目录 List modules = new(); GetDirectoryPaths(Application.dataPath, modules, "Modules"); if (modules.Count == 0) { Debug.LogError("未找到任何Modules目录"); return Array.Empty(); } // 查找package List packages = new(); GetDirectoryPaths(modules[0], packages, "com."); if (packages.Count == 0) { return Array.Empty(); } // 记录Package地址并返回选项 _paths.Clear(); packages.ForEach(path => _paths.Add(Path.GetFileName(path), path)); return _paths.Keys.ToArray(); } /// /// 获取Package中所有Script /// private string[] GetScripts() { // 未选Package if (string.IsNullOrEmpty(packageID) || !_paths.ContainsKey(packageID)) return Array.Empty(); List scripts = new(); GetFilePaths(_paths[packageID], scripts, tail: ".sg.json"); return scripts.Count == 0 ? Array.Empty() : scripts.Select(path => Path.GetFileName(path).Replace(".json", "")).ToArray(); } /// /// 获取符合条件的目录 /// /// 查找起始目录 /// 查找结果 /// 筛选条件(title) /// 筛选条件(tail) private static void GetDirectoryPaths(string root, List 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); } } /// /// 获取符合条件的文件路径 /// /// 查找起始目录 /// 查找结果 /// 筛选条件(title) /// 筛选条件(tail) private static void GetFilePaths(string root, List 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 }