Files
aibis-dream/Assets/Scripts/Test/ActorAnimaExTest.cs
T
2026-01-06 18:51:59 +08:00

394 lines
13 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AibisDream.Framework;
using UnityEngine;
namespace AibisDream.Test
{
/// <summary>
/// ActorAnimaEx 功能测试脚本
/// 使用键盘按键触发各项测试功能
/// </summary>
public class ActorAnimaExTest : MonoBehaviour
{
[Header("测试配置")]
[SerializeField] private string testActorName = "调酒妹测试";
[SerializeField] private string testSlotName = "测试";
[Header("调试信息")]
[SerializeField] private bool showHelp = true;
private ActorAnimaEx _testActor;
private List<BlendTreeInfo> _blendTreeInfos;
private List<string> _stateNames;
private Dictionary<string, List<string>> _faceNamesByState;
private int _currentStateIndex = 0;
private Dictionary<string, int> _currentFaceIndexByState;
private bool _isTalking = false;
private void Start()
{
_currentFaceIndexByState = new Dictionary<string, int>();
_faceNamesByState = new Dictionary<string, List<string>>();
if (showHelp)
{
PrintHelp();
}
}
private void Update()
{
// 检测键盘输入
if (Input.GetKeyDown(KeyCode.Alpha1))
{
TestInitActor();
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
TestChangeState();
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
TestSetFaceState();
}
else if (Input.GetKeyDown(KeyCode.Alpha4))
{
TestSetTalkingState();
}
else if (Input.GetKeyDown(KeyCode.Alpha5))
{
StartCoroutine(TestCombination());
}
else if (Input.GetKeyDown(KeyCode.R))
{
TestReset();
}
else if (Input.GetKeyDown(KeyCode.H))
{
showHelp = !showHelp;
if (showHelp)
{
PrintHelp();
}
}
}
private void OnGUI()
{
if (!showHelp) return;
// 显示帮助信息
GUIStyle style = new GUIStyle(GUI.skin.box);
style.fontSize = 14;
style.normal.textColor = Color.white;
string helpText = "=== ActorAnimaEx 测试控制 ===\n" +
"1 - 初始化测试角色\n" +
"2 - 切换状态 (ChangeState)\n" +
"3 - 切换表情 (SetFaceState)\n" +
"4 - 切换说话状态 (SetTalkingState)\n" +
"5 - 运行组合测试\n" +
"R - 重置测试\n" +
"H - 显示/隐藏帮助\n\n";
if (_testActor != null)
{
helpText += $"当前角色: {_testActor.ActorName}\n";
if (_stateNames != null && _stateNames.Count > 0 && _currentStateIndex < _stateNames.Count)
{
helpText += $"当前状态: {_stateNames[_currentStateIndex]}\n";
}
helpText += $"说话状态: {(_isTalking ? "开启" : "关闭")}\n";
}
GUI.Box(new Rect(10, 10, 300, helpText.Split('\n').Length * 20 + 20), helpText, style);
}
/// <summary>
/// 打印帮助信息到控制台
/// </summary>
private void PrintHelp()
{
Debug.Log("=== ActorAnimaEx 测试控制 ===\n" +
"1 - 初始化测试角色\n" +
"2 - 切换状态 (ChangeState)\n" +
"3 - 切换表情 (SetFaceState)\n" +
"4 - 切换说话状态 (SetTalkingState)\n" +
"5 - 运行组合测试\n" +
"R - 重置测试\n" +
"H - 显示/隐藏帮助");
}
/// <summary>
/// 测试 1: 初始化测试角色
/// </summary>
private void TestInitActor()
{
Debug.Log("=== 测试 1: 初始化测试角色 ===");
try
{
// 检查 ActorManager 是否已初始化
if (ActorManager.Instance == null)
{
Debug.LogError("ActorManager 未初始化!");
return;
}
// 销毁已存在的测试角色
if (_testActor != null)
{
Destroy(_testActor.gameObject);
_testActor = null;
}
// 创建测试角色
_testActor = ActorManager.Instance.InitActor(testActorName, testSlotName, ActorType.AnimaEx) as ActorAnimaEx;
if (_testActor == null)
{
Debug.LogError($"创建角色失败: {testActorName}");
return;
}
Debug.Log($"✓ 角色创建成功: {_testActor.ActorName}");
// 显示角色
_testActor.Show();
// 加载混合树配置
LoadBlendTreeConfig();
Debug.Log("✓ 初始化测试完成");
}
catch (System.Exception e)
{
Debug.LogError($"初始化测试失败: {e.Message}\n{e.StackTrace}");
}
}
/// <summary>
/// 加载混合树配置
/// </summary>
private void LoadBlendTreeConfig()
{
var configText = ResourceKit.LoadAssetSync<TextAsset>($"AnimationConfig/{testActorName}");
if (configText == null)
{
Debug.LogWarning($"未找到配置文件: AnimationConfig/{testActorName}");
return;
}
_blendTreeInfos = AnimatorKit.ParseBlendTreeConfigFromJson(configText.text).blendTrees;
if (_blendTreeInfos == null || _blendTreeInfos.Count == 0)
{
Debug.LogWarning("混合树配置为空或解析失败");
return;
}
// 提取状态名称列表
// _stateNames = _blendTreeInfos.Select(info => info.name).ToList();
_stateNames = new List<string> {"站立", "俯身"};
// 为每个状态提取表情名称列表
_faceNamesByState.Clear();
_currentFaceIndexByState.Clear();
foreach (var info in _blendTreeInfos)
{
if (info.clips != null && info.clips.Count > 0)
{
var faceNames = info.clips.Select(clip => clip.clipName).ToList();
_faceNamesByState[info.name] = faceNames;
_currentFaceIndexByState[info.name] = 0;
}
}
Debug.Log($"✓ 加载混合树配置成功: {_blendTreeInfos.Count} 个状态");
foreach (var stateName in _stateNames)
{
Debug.Log($" - 状态: {stateName}");
if (_faceNamesByState.TryGetValue(stateName, out var faces) && faces.Count > 0)
{
Debug.Log($" 表情数量: {faces.Count}");
}
}
}
/// <summary>
/// 测试 2: 切换状态
/// </summary>
private void TestChangeState()
{
if (_testActor == null)
{
Debug.LogWarning("请先初始化测试角色 (按 1)");
return;
}
if (_stateNames == null || _stateNames.Count == 0)
{
Debug.LogWarning("没有可用的状态");
return;
}
// 更新索引
_currentStateIndex = (_currentStateIndex + 1) % _stateNames.Count;
// 循环切换到下一个状态
string nextState = _stateNames[_currentStateIndex];
Debug.Log($"=== 测试 2: 切换状态到 '{nextState}' ===");
_testActor.ChangeState(nextState);
Debug.Log($"✓ 状态已切换到: {nextState}");
}
/// <summary>
/// 测试 3: 切换表情
/// </summary>
private void TestSetFaceState()
{
if (_testActor == null)
{
Debug.LogWarning("请先初始化测试角色 (按 1)");
return;
}
if (_stateNames == null || _stateNames.Count == 0 || _currentStateIndex >= _stateNames.Count)
{
Debug.LogWarning("没有可用的状态");
return;
}
string currentState = _stateNames[_currentStateIndex];
if (!_faceNamesByState.TryGetValue(currentState, out var faceNames) || faceNames.Count == 0)
{
Debug.LogWarning($"当前状态 '{currentState}' 没有可用的表情");
return;
}
// 获取当前状态的表情索引
if (!_currentFaceIndexByState.TryGetValue(currentState, out int faceIndex))
{
faceIndex = 0;
_currentFaceIndexByState[currentState] = 0;
}
// 循环切换到下一个表情
string nextFace = faceNames[faceIndex];
Debug.Log($"=== 测试 3: 切换表情到 '{nextFace}' (状态: {currentState}) ===");
_testActor.SetFaceState(nextFace);
Debug.Log($"✓ 表情已切换到: {nextFace}");
// 更新索引
faceIndex = (faceIndex + 1) % faceNames.Count;
_currentFaceIndexByState[currentState] = faceIndex;
}
/// <summary>
/// 测试 4: 切换说话状态
/// </summary>
private void TestSetTalkingState()
{
if (_testActor == null)
{
Debug.LogWarning("请先初始化测试角色 (按 1)");
return;
}
_isTalking = !_isTalking;
Debug.Log($"=== 测试 4: 切换说话状态到 '{(_isTalking ? "开启" : "关闭")}' ===");
_testActor.SetTalkingState(_isTalking);
Debug.Log($"✓ 说话状态已切换: {(_isTalking ? "开启" : "关闭")}");
}
/// <summary>
/// 测试 5: 组合测试
/// </summary>
private IEnumerator TestCombination()
{
if (_testActor == null)
{
Debug.LogWarning("请先初始化测试角色 (按 1)");
yield break;
}
if (_stateNames == null || _stateNames.Count == 0)
{
Debug.LogWarning("没有可用的状态");
yield break;
}
Debug.Log("=== 测试 5: 组合测试开始 ===");
// 测试在不同状态下切换表情
foreach (var stateName in _stateNames)
{
Debug.Log($"\n--- 切换到状态: {stateName} ---");
_testActor.ChangeState(stateName);
// 等待状态切换完成
yield return new WaitForSeconds(0.5f);
// 如果该状态有表情,测试切换表情
if (_faceNamesByState.TryGetValue(stateName, out var faceNames) && faceNames.Count > 0)
{
foreach (var faceName in faceNames)
{
Debug.Log($" 设置表情: {faceName}");
_testActor.SetFaceState(faceName);
yield return new WaitForSeconds(0.3f);
}
}
yield return new WaitForSeconds(0.5f);
}
// 测试说话状态
Debug.Log("\n--- 测试说话状态 ---");
_testActor.SetTalkingState(true);
yield return new WaitForSeconds(1f);
_testActor.SetTalkingState(false);
yield return new WaitForSeconds(0.5f);
Debug.Log("=== 组合测试完成 ===");
}
/// <summary>
/// 重置测试
/// </summary>
private void TestReset()
{
Debug.Log("=== 重置测试 ===");
// 销毁当前角色
if (_testActor != null)
{
Destroy(_testActor.gameObject);
_testActor = null;
}
// 重置状态
_currentStateIndex = 0;
_currentFaceIndexByState.Clear();
_isTalking = false;
_blendTreeInfos = null;
_stateNames = null;
_faceNamesByState.Clear();
Debug.Log("✓ 测试已重置,可以重新初始化 (按 1)");
}
}
}