using Animancer; using Sirenix.OdinInspector; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityTimer; 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 = "动画资源")] public Dictionary allAnimationSet = new Dictionary(); private Animator animator { get; set; } public AnimancerComponent animancer { get; private set; } private IEnumerable GetAllAnim() { return allAnimationSet.Keys; } private void Awake() { animator = GetComponent(); if (!animator) { animator = gameObject.AddComponent(); } animancer = GetComponent(); if (!animancer) { animancer = gameObject.AddComponent(); animancer.Animator = animator; } } private void OnEnable() { if (isPlayEnable) { PlayAnim(enablePlayAnim); } } public void PlayAnim(string name) { if (allAnimationSet.TryGetValue(name, out AnimationSet animationSet)) { AnimationClip clip = animationSet.GetClip(); PlayAnim(clip); } } public void PlayAnim(string name, Vector2 direction) { if (allAnimationSet.TryGetValue(name, out AnimationSet animationSet)) { AnimationClip clip = animationSet.GetClip(direction); PlayAnim(clip); } } public void PlayAnim(string name, Action endCallback) { if (allAnimationSet.TryGetValue(name, out AnimationSet animationSet)) { AnimationClip clip = animationSet.GetClip(); 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 AnimationSet animationSet)) { AnimationClip clip = animationSet.GetClip(); 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(); } } }