【a】可视化剧本编辑器 10.StoryEditor
This commit is contained in:
20
Assets/10.StoryEditor/Sample~/Script/ResourceLoader.cs
Normal file
20
Assets/10.StoryEditor/Sample~/Script/ResourceLoader.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using rokid.armaz.module;
|
||||
using Stary.Evo.StoryEditor;
|
||||
using UnityEngine;
|
||||
|
||||
public class ResourceLoader : IResource
|
||||
{
|
||||
public UniTask<T> Load<T>(ResourcePathData pathData) where T : Object
|
||||
{
|
||||
return AMP.ResourceLoader.LoadAssetAsync<T>(pathData.packageID, pathData.path);
|
||||
}
|
||||
|
||||
public UniTask<ResourcePathData> Save<T>(T asset, string packageID) where T : Object
|
||||
{
|
||||
ResourcePathData pathData = new();
|
||||
pathData.packageID = packageID;
|
||||
pathData.AddPath(asset.name);
|
||||
return UniTask.FromResult(pathData);
|
||||
}
|
||||
}
|
||||
11
Assets/10.StoryEditor/Sample~/Script/ResourceLoader.cs.meta
Normal file
11
Assets/10.StoryEditor/Sample~/Script/ResourceLoader.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 43ce2e09f0a37f64e8dac4036d588160
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
166
Assets/10.StoryEditor/Sample~/Script/TestScriptPlayer.cs
Normal file
166
Assets/10.StoryEditor/Sample~/Script/TestScriptPlayer.cs
Normal file
@@ -0,0 +1,166 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 543f5e9055e16f045806981c5725e910
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user