74 lines
2.1 KiB
C#
74 lines
2.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using AibisDream.Framework;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class ActorAnima : AbsActor
|
|
{
|
|
private const string HashPrefix = "#hash.";
|
|
|
|
private Animator _animator;
|
|
private Dictionary<int, string> _stateNameDict;
|
|
|
|
protected override void Init()
|
|
{
|
|
_animator = GetComponent<Animator>();
|
|
// 设置基本信息
|
|
base.Init();
|
|
// 获取Controller
|
|
actorData.actorType = ActorType.Anima;
|
|
var controller = ResourceKit.LoadAssetSync<RuntimeAnimatorController>($"Animation/{ActorName}");
|
|
_animator.runtimeAnimatorController = controller;
|
|
|
|
// 保存事件
|
|
actorData.onSave.AddListener(() =>
|
|
{
|
|
actorData.stateName =
|
|
HashPrefix + _animator.GetCurrentAnimatorStateInfo(0).shortNameHash;
|
|
});
|
|
|
|
// 加载初始状态
|
|
if (!string.IsNullOrWhiteSpace(actorData.stateName))
|
|
{
|
|
ChangeState(actorData.stateName);
|
|
}
|
|
}
|
|
|
|
public override void ChangeState(string animaName)
|
|
{
|
|
if (animaName.StartsWith(HashPrefix))
|
|
{
|
|
var hash = int.Parse(animaName[HashPrefix.Length..]);
|
|
_animator.Play(hash);
|
|
}
|
|
else
|
|
{
|
|
_animator.Play(animaName);
|
|
}
|
|
}
|
|
|
|
public override IEnumerator ChangeStateAsync(string animaName)
|
|
{
|
|
_animator.Play(animaName);
|
|
|
|
// 等待一帧确保动画状态切换完成
|
|
yield return null;
|
|
|
|
// 等待直到动画状态名称匹配
|
|
while (!_animator.GetCurrentAnimatorStateInfo(0).IsName(animaName))
|
|
{
|
|
yield return null;
|
|
}
|
|
|
|
var info = _animator.GetCurrentAnimatorStateInfo(0);
|
|
yield return new WaitForSeconds(info.length);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
actorData.onSave.RemoveAllListeners();
|
|
}
|
|
}
|
|
} |