IndieGame/client/Assets/Ether/Scripts/Module/Animation/AnimController.cs

247 lines
7.3 KiB
C#
Raw Permalink Normal View History

2024-10-11 10:12:15 +08:00

using Animancer;
using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
2024-10-30 17:58:20 +08:00
using System.Linq;
2024-10-11 10:12:15 +08:00
using UnityEngine;
using UnityTimer;
2024-10-29 15:14:02 +08:00
using Object = UnityEngine.Object;
2024-10-11 10:12:15 +08:00
namespace Ether
{
2024-10-30 17:58:20 +08:00
[RequireComponent(typeof(Animator), typeof(AnimancerComponent))]
2024-10-11 10:12:15 +08:00
public class AnimController : SerializedMonoBehaviour
{
2024-10-29 18:53:20 +08:00
[Title("动画控制")]
2024-10-11 10:12:15 +08:00
[LabelText("是否在激活时播放")]
2024-10-29 18:53:20 +08:00
[PropertyOrder(0)]
2024-10-11 10:12:15 +08:00
public bool isPlayEnable;
[LabelText("激活时播放动画")]
[ShowIf("isPlayEnable", true)]
[ValueDropdown("GetAllAnim")]
2024-10-29 18:53:20 +08:00
[PropertyOrder(1)]
2024-10-11 10:12:15 +08:00
public string enablePlayAnim;
[LabelText("是否在隐藏时播放")]
2024-10-29 18:53:20 +08:00
[PropertyOrder(2)]
2024-10-11 10:12:15 +08:00
public bool isPlayDisable;
[LabelText("隐藏时播放动画")]
[ShowIf("isPlayDisable", true)]
[ValueDropdown("GetAllAnim")]
2024-10-29 18:53:20 +08:00
[PropertyOrder(3)]
2024-10-11 10:12:15 +08:00
public string disablePlayAnim;
[LabelText("动画列表")]
[PropertySpace(10)]
[ShowInInspector]
2024-10-29 15:14:02 +08:00
[OnValueChanged("OnValueChanged")]
2024-10-29 18:53:20 +08:00
[PropertyOrder(10)]
2024-10-30 17:58:20 +08:00
public List<Object> allAnimation = new List<Object>();
private Dictionary<string, Object> allAnimationSet;
2024-10-29 18:53:20 +08:00
// [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);
// }
// }
//
2024-10-29 15:14:02 +08:00
private void OnValueChanged()
{
// 这里可以添加逻辑来处理值变化,例如清理无效条目
2024-10-30 17:58:20 +08:00
for (int i = allAnimation.Count - 1; i >= 0; i--)
2024-10-29 15:14:02 +08:00
{
2024-10-30 17:58:20 +08:00
if (!IsAllowedType(allAnimation[i]))
2024-10-29 15:14:02 +08:00
{
2024-10-30 17:58:20 +08:00
allAnimation.Remove(allAnimation[i]);
Debug.LogError("添加的类型只能是AnimationClip或者AnimationSet");
2024-10-29 15:14:02 +08:00
}
}
}
private bool IsAllowedType(object value)
{
if (value is AnimationClip or AnimationSet)
{
return true;
}
return false;
}
2024-10-11 10:12:15 +08:00
private Animator animator { get; set; }
public AnimancerComponent animancer { get; private set; }
private IEnumerable<string> GetAllAnim()
{
2024-10-30 17:58:20 +08:00
return allAnimation.Select(value => value.name);
2024-10-11 10:12:15 +08:00
}
private void Awake()
{
animator = GetComponent<Animator>();
if (!animator)
{
animator = gameObject.AddComponent<Animator>();
}
animancer = GetComponent<AnimancerComponent>();
if (!animancer)
{
animancer = gameObject.AddComponent<AnimancerComponent>();
animancer.Animator = animator;
}
2024-10-30 17:58:20 +08:00
allAnimationSet = allAnimation.ToDictionary(item => item.name, item => item);
2024-10-11 10:12:15 +08:00
}
private void OnEnable()
{
if (isPlayEnable)
{
PlayAnim(enablePlayAnim);
}
}
public void PlayAnim(string name)
{
2024-10-29 15:14:02 +08:00
if (allAnimationSet.TryGetValue(name, out Object animation))
2024-10-11 10:12:15 +08:00
{
2024-10-29 15:14:02 +08:00
AnimationClip clip = animation switch
{
AnimationSet animationSet => animationSet.GetClip(),
AnimationClip animationClip => animationClip,
_ => null
};
2024-10-11 10:12:15 +08:00
PlayAnim(clip);
}
}
public void PlayAnim(string name, Vector2 direction)
{
2024-10-29 15:14:02 +08:00
if (allAnimationSet.TryGetValue(name, out Object animation))
2024-10-11 10:12:15 +08:00
{
2024-10-29 15:14:02 +08:00
AnimationClip clip = animation switch
{
AnimationSet animationSet => animationSet.GetClip(direction),
AnimationClip animationClip => animationClip,
_ => null
};
2024-10-11 10:12:15 +08:00
PlayAnim(clip);
}
}
public void PlayAnim(string name, Action endCallback)
{
2024-10-29 15:14:02 +08:00
if (allAnimationSet.TryGetValue(name, out Object animation))
2024-10-11 10:12:15 +08:00
{
2024-10-29 15:14:02 +08:00
AnimationClip clip = animation switch
{
AnimationSet animationSet => animationSet.GetClip(),
AnimationClip animationClip => animationClip,
_ => null
};
2024-10-11 10:12:15 +08:00
PlayAnim(clip);
}
}
private void PlayAnim(AnimationClip clip)
{
if (animancer && clip)
{
animancer.Play(clip);
}
}
Timer disableAnimTimer;
public void PlayDisableAnim(Action endCallback)
{
if (isPlayDisable)
{
2024-10-29 15:14:02 +08:00
if (allAnimationSet.TryGetValue(disablePlayAnim, out Object animation))
2024-10-11 10:12:15 +08:00
{
2024-10-29 15:14:02 +08:00
AnimationClip clip = animation switch
{
AnimationSet animationSet => animationSet.GetClip(),
AnimationClip animationClip => animationClip,
_ => null
};
2024-10-11 10:12:15 +08:00
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();
}
2024-10-30 17:58:20 +08:00
2024-10-11 10:12:15 +08:00
}
}