IndieGame/client/Assets/Ether/Scripts/Module/Animation/AnimController.cs
2024-10-29 15:14:02 +08:00

199 lines
5.7 KiB
C#

using Animancer;
using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityTimer;
using Object = UnityEngine.Object;
namespace Ether
{
public class AnimController : SerializedMonoBehaviour
{
[Header("动画控制")]
[LabelText("是否在激活时播放")]
public bool isPlayEnable;
[LabelText("激活时播放动画")]
[ShowIf("isPlayEnable", true)]
[ValueDropdown("GetAllAnim")]
public string enablePlayAnim;
[LabelText("是否在隐藏时播放")]
public bool isPlayDisable;
[LabelText("隐藏时播放动画")]
[ShowIf("isPlayDisable", true)]
[ValueDropdown("GetAllAnim")]
public string disablePlayAnim;
[LabelText("动画列表")]
[PropertySpace(10)]
[ShowInInspector]
[DictionaryDrawerSettings(KeyLabel = "名称", ValueLabel = "动画资源")]
[OnValueChanged("OnValueChanged")]
public Dictionary<string, Object> allAnimationSet = new Dictionary<string, Object>();
private void OnValueChanged()
{
// 这里可以添加逻辑来处理值变化,例如清理无效条目
List<string> keysToRemove = new List<string>();
foreach (var pair in allAnimationSet)
{
if (!IsAllowedType(pair.Value))
{
keysToRemove.Add(pair.Key);
}
}
foreach (var key in keysToRemove)
{
allAnimationSet.Remove(key);
Debug.LogError("添加的类型只能是AnimationClip或者AnimationSet");
}
}
private bool IsAllowedType(object value)
{
if (value is AnimationClip or AnimationSet)
{
return true;
}
return false;
}
private Animator animator { get; set; }
public AnimancerComponent animancer { get; private set; }
private IEnumerable<string> GetAllAnim()
{
return allAnimationSet.Keys;
}
private void Awake()
{
animator = GetComponent<Animator>();
if (!animator)
{
animator = gameObject.AddComponent<Animator>();
}
animancer = GetComponent<AnimancerComponent>();
if (!animancer)
{
animancer = gameObject.AddComponent<AnimancerComponent>();
animancer.Animator = animator;
}
}
private void OnEnable()
{
if (isPlayEnable)
{
PlayAnim(enablePlayAnim);
}
}
public void PlayAnim(string name)
{
if (allAnimationSet.TryGetValue(name, out Object animation))
{
AnimationClip clip = animation switch
{
AnimationSet animationSet => animationSet.GetClip(),
AnimationClip animationClip => animationClip,
_ => null
};
PlayAnim(clip);
}
}
public void PlayAnim(string name, Vector2 direction)
{
if (allAnimationSet.TryGetValue(name, out Object animation))
{
AnimationClip clip = animation switch
{
AnimationSet animationSet => animationSet.GetClip(direction),
AnimationClip animationClip => animationClip,
_ => null
};
PlayAnim(clip);
}
}
public void PlayAnim(string name, Action endCallback)
{
if (allAnimationSet.TryGetValue(name, out Object animation))
{
AnimationClip clip = animation switch
{
AnimationSet animationSet => animationSet.GetClip(),
AnimationClip animationClip => animationClip,
_ => null
};
PlayAnim(clip);
}
}
private void PlayAnim(AnimationClip clip)
{
if (animancer && clip)
{
animancer.Play(clip);
}
}
Timer disableAnimTimer;
public void PlayDisableAnim(Action endCallback)
{
if (isPlayDisable)
{
if (allAnimationSet.TryGetValue(disablePlayAnim, out Object animation))
{
AnimationClip clip = animation switch
{
AnimationSet animationSet => animationSet.GetClip(),
AnimationClip animationClip => animationClip,
_ => null
};
if (clip)
{
PlayAnim(clip);
disableAnimTimer?.Cancel();
float clipLength = clip.length;
disableAnimTimer = Timer.Register(clipLength, () =>
{
endCallback?.Invoke();
}, autoDestroyOwner: this);
return;
}
}
}
endCallback?.Invoke();
}
public void PlayEffect(string effectName)
{
if (AudioManager.Inst.IsPlayingEffectAudio(effectName))
{
AudioManager.Inst.StopEffectAudio(effectName);
}
AudioManager.Inst.PlayEffectAudio(effectName);
}
private void OnDisable()
{
animancer.Stop();
disableAnimTimer?.Cancel();
}
}
}