IndieGame/client/Assets/Ether/Scripts/Module/Animation/AnimController.cs
2024-10-30 17:58:20 +08:00

247 lines
7.3 KiB
C#

using Animancer;
using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityTimer;
using Object = UnityEngine.Object;
namespace Ether
{
[RequireComponent(typeof(Animator), typeof(AnimancerComponent))]
public class AnimController : SerializedMonoBehaviour
{
[Title("动画控制")]
[LabelText("是否在激活时播放")]
[PropertyOrder(0)]
public bool isPlayEnable;
[LabelText("激活时播放动画")]
[ShowIf("isPlayEnable", true)]
[ValueDropdown("GetAllAnim")]
[PropertyOrder(1)]
public string enablePlayAnim;
[LabelText("是否在隐藏时播放")]
[PropertyOrder(2)]
public bool isPlayDisable;
[LabelText("隐藏时播放动画")]
[ShowIf("isPlayDisable", true)]
[ValueDropdown("GetAllAnim")]
[PropertyOrder(3)]
public string disablePlayAnim;
[LabelText("动画列表")]
[PropertySpace(10)]
[ShowInInspector]
[OnValueChanged("OnValueChanged")]
[PropertyOrder(10)]
public List<Object> allAnimation = new List<Object>();
private Dictionary<string, Object> allAnimationSet;
// [PropertySpace(20)]
// [LabelText("预览动画")]
// [ValueDropdown("GetAllAnim")]
// [PropertyOrder(4)]
// public string previewAnim;
//
// [LabelText("预览进度")]
// [Range(0, 1)]
// [OnValueChanged("OnPreview")]
// [PropertyOrder(5)]
// public float previewProgress;
//
// [PropertySpace(10)]
// [Button("预览动画", ButtonSizes.Large)]
// [PropertyOrder(6)]
// public void PreviewAnim()
// {
// OnPreview();
// }
//
// private void OnPreview()
// {
// Awake();
// if (string.IsNullOrEmpty(previewAnim))
// {
// return;
// }
// if (allAnimationSet.TryGetValue(previewAnim, out Object animation))
// {
// AnimationClip clip = animation switch
// {
// AnimationSet animationSet => animationSet.GetClip(),
// AnimationClip animationClip => animationClip,
// _ => null
// };
//
// if (clip == null) return;
// float normalizedTime = previewProgress;
// var currentTime = clip.length * normalizedTime;
// clip.SampleAnimation(gameObject, currentTime);
// }
// }
//
private void OnValueChanged()
{
// 这里可以添加逻辑来处理值变化,例如清理无效条目
for (int i = allAnimation.Count - 1; i >= 0; i--)
{
if (!IsAllowedType(allAnimation[i]))
{
allAnimation.Remove(allAnimation[i]);
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 allAnimation.Select(value => value.name);
}
private void Awake()
{
animator = GetComponent<Animator>();
if (!animator)
{
animator = gameObject.AddComponent<Animator>();
}
animancer = GetComponent<AnimancerComponent>();
if (!animancer)
{
animancer = gameObject.AddComponent<AnimancerComponent>();
animancer.Animator = animator;
}
allAnimationSet = allAnimation.ToDictionary(item => item.name, item => item);
}
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();
}
}
}