using System; using System.Collections.Generic; using UnityEngine; [Serializable] public class AnimationNode { public GameObject node; public Animation animation; public string clipName; } [Serializable] public class AnimationGroup { public string groupKey; public List nodes = new List(); } public class AnimationManager : MonoBehaviour { public List groups = new List(); ViTimeNode1 _showTimeNode = new ViTimeNode1(); public bool HasGroup(string groupKey) { var group = GetGroup(groupKey); return group != null; } public AnimationGroup GetGroup(string groupKey) { return groups.Find(g => g.groupKey == groupKey); } public void Play(string groupKey, bool force = false, ViTimeNode1.Callback callback = null) { _showTimeNode.Detach(); var group = GetGroup(groupKey); if (group == null) return; float animationTime = 0; foreach (AnimationNode node in group.nodes) { Animation animationComp = node.animation; string clipName = node.clipName; if (animationComp && !string.IsNullOrEmpty(clipName)) { animationTime = Math.Max(animationComp[clipName].length, animationTime); if (force) { animationComp.Stop(); animationComp[clipName].time = 0f; animationComp[clipName].enabled = true; animationComp[clipName].weight = 1f; animationComp.Sample(); animationComp[clipName].enabled = false; } animationComp.Play(clipName); } } if (callback != null) { ViTimerInstance.SetTime(_showTimeNode, animationTime, callback); } } public void SetTime(string groupKey, float time) { var group = GetGroup(groupKey); if (group == null) return; foreach (var node in group.nodes) { if (node.animation && !string.IsNullOrEmpty(node.clipName)) { node.animation.Stop(); node.animation[node.clipName].time = node.animation[node.clipName].length * time; node.animation[node.clipName].enabled = true; node.animation[node.clipName].weight = 1f; node.animation.Sample(); node.animation[node.clipName].enabled = false; } } } public void Dispose() { _showTimeNode.Detach(); } }