167 lines
4.9 KiB
C#
167 lines
4.9 KiB
C#
|
|
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
|
|||
|
|
}
|