using System.Collections; using System.Collections.Generic; using System.Linq; using AibisDream.Framework; using UnityEngine; namespace AibisDream.Test { /// /// ActorAnimaEx 功能测试脚本 /// 使用键盘按键触发各项测试功能 /// public class ActorAnimaExTest : MonoBehaviour { [Header("测试配置")] [SerializeField] private string testActorName = "调酒妹测试"; [SerializeField] private string testSlotName = "测试"; [Header("调试信息")] [SerializeField] private bool showHelp = true; private ActorAnimaEx _testActor; private List _blendTreeInfos; private List _stateNames; private Dictionary> _faceNamesByState; private int _currentStateIndex = 0; private Dictionary _currentFaceIndexByState; private bool _isTalking = false; private void Start() { _currentFaceIndexByState = new Dictionary(); _faceNamesByState = new Dictionary>(); 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); } /// /// 打印帮助信息到控制台 /// private void PrintHelp() { Debug.Log("=== ActorAnimaEx 测试控制 ===\n" + "1 - 初始化测试角色\n" + "2 - 切换状态 (ChangeState)\n" + "3 - 切换表情 (SetFaceState)\n" + "4 - 切换说话状态 (SetTalkingState)\n" + "5 - 运行组合测试\n" + "R - 重置测试\n" + "H - 显示/隐藏帮助"); } /// /// 测试 1: 初始化测试角色 /// 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}"); } } /// /// 加载混合树配置 /// private void LoadBlendTreeConfig() { var configText = ResourceKit.LoadAssetSync($"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 {"站立", "俯身"}; // 为每个状态提取表情名称列表 _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}"); } } } /// /// 测试 2: 切换状态 /// 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}"); } /// /// 测试 3: 切换表情 /// 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; } /// /// 测试 4: 切换说话状态 /// private void TestSetTalkingState() { if (_testActor == null) { Debug.LogWarning("请先初始化测试角色 (按 1)"); return; } _isTalking = !_isTalking; Debug.Log($"=== 测试 4: 切换说话状态到 '{(_isTalking ? "开启" : "关闭")}' ==="); _testActor.SetTalkingState(_isTalking); Debug.Log($"✓ 说话状态已切换: {(_isTalking ? "开启" : "关闭")}"); } /// /// 测试 5: 组合测试 /// 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("=== 组合测试完成 ==="); } /// /// 重置测试 /// 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)"); } } }