125 lines
4.1 KiB
C#
125 lines
4.1 KiB
C#
using AibisDream.Framework;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class ActorAnimaEx : AbsActor
|
|
{
|
|
private Animator _animator;
|
|
private string _currentStateName;
|
|
private Dictionary<string, BlendTreeInfo> _blendTreeDict;
|
|
|
|
protected override void Init()
|
|
{
|
|
_animator = GetComponent<Animator>();
|
|
// 设置基本信息
|
|
base.Init();
|
|
actorData.actorType = ActorType.AnimaEx;
|
|
var controller = ResourceKit.LoadAssetSync<RuntimeAnimatorController>($"Animation/{ActorName}");
|
|
_animator.runtimeAnimatorController = controller;
|
|
|
|
ParseBlendTreeInfos();
|
|
|
|
// 保存事件
|
|
actorData.onSave.AddListener(() =>
|
|
{
|
|
actorData.stateName = _currentStateName;
|
|
actorData.faceParam = _animator.GetFloat("Face Param");
|
|
});
|
|
|
|
// 加载初始状态
|
|
if (!string.IsNullOrWhiteSpace(actorData.stateName))
|
|
{
|
|
ChangeState(actorData.stateName);
|
|
}
|
|
if (actorData.faceParam >= 0)
|
|
{
|
|
_animator.SetFloat("Face Param", actorData.faceParam);
|
|
}
|
|
}
|
|
|
|
private void ParseBlendTreeInfos()
|
|
{
|
|
_blendTreeDict = new Dictionary<string, BlendTreeInfo>();
|
|
// 解析混合树
|
|
var configText = ResourceKit.LoadAssetSync<TextAsset>($"AnimationConfig/{ActorName}");
|
|
if (configText != null)
|
|
{
|
|
var blendTreeConfig = AnimatorKit.ParseBlendTreeConfigFromJson(configText.text);
|
|
foreach (var info in blendTreeConfig.blendTrees)
|
|
{
|
|
_blendTreeDict[info.name] = info;
|
|
}
|
|
|
|
_currentStateName = blendTreeConfig.defaultStateName;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换站姿
|
|
/// </summary>
|
|
/// <param name="stateName"></param>
|
|
public override void ChangeState(string stateName)
|
|
{
|
|
StartCoroutine(ChangeStateAsync(stateName));
|
|
}
|
|
|
|
public override IEnumerator ChangeStateAsync(string stateName)
|
|
{
|
|
// 记录当前状态
|
|
_currentStateName = stateName;
|
|
|
|
// 切换站姿
|
|
_animator.SetTrigger(stateName);
|
|
// 等待动作切换完成
|
|
while (!_animator.GetCurrentAnimatorStateInfo(0).IsName(stateName))
|
|
{
|
|
yield return null;
|
|
}
|
|
|
|
// var info = _animator.GetCurrentAnimatorStateInfo(0);
|
|
// yield return new WaitForSeconds(info.length);
|
|
|
|
_animator.ResetTrigger(stateName);
|
|
|
|
Debug.Log($"当前状态{_currentStateName}");
|
|
}
|
|
|
|
public override void SetTalkingState(bool isTalking)
|
|
{
|
|
_animator.SetFloat("Is Talking", isTalking ? 1 : 0);
|
|
}
|
|
|
|
public override void SetFaceState(string faceName)
|
|
{
|
|
// 先读取当前混合树信息
|
|
if (_blendTreeDict.TryGetValue(_currentStateName, out var blendTreeInfo))
|
|
{
|
|
Debug.Log($"当前状态{_currentStateName}的混合树信息存在");
|
|
if (blendTreeInfo.type == "1D")
|
|
{
|
|
var clipInfo = blendTreeInfo.GetClipInfo(faceName);
|
|
if (clipInfo != null)
|
|
{
|
|
_animator.SetFloat(blendTreeInfo.blendParameter, clipInfo.threshold);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var clipInfo = blendTreeInfo.GetClipInfo(faceName);
|
|
if (clipInfo != null)
|
|
{
|
|
_animator.SetFloat(blendTreeInfo.blendParameter, clipInfo.position.x);
|
|
// 不设置Y轴参数
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"当前状态{_currentStateName}的混合树信息不存在");
|
|
}
|
|
}
|
|
}
|
|
} |