using System.Collections; using System.Collections.Generic; using System.Linq; using System.Xml.Serialization; using UnityEngine; namespace GameplayIngredients.Rigs { public class RigManager : Manager { Dictionary> m_UpdateRigs; Dictionary> m_LateUpdateRigs; Dictionary> m_FixedUpdateRigs; private void OnEnable() { m_UpdateRigs = new Dictionary>(); m_LateUpdateRigs = new Dictionary>(); m_FixedUpdateRigs = new Dictionary>(); } private void OnDisable() { m_UpdateRigs.Clear(); m_LateUpdateRigs.Clear(); m_FixedUpdateRigs.Clear(); } public void RegistedRig(Rig rig) { Rig.UpdateMode updateMode; if (rig.canChangeUpdateMode) updateMode = rig.updateMode; else updateMode = rig.defaultUpdateMode; Dictionary> dict; switch (updateMode) { default: case Rig.UpdateMode.Update: dict = m_UpdateRigs; break; case Rig.UpdateMode.LateUpdate: dict = m_LateUpdateRigs; break; case Rig.UpdateMode.FixedUpdate: dict = m_FixedUpdateRigs; break; } if(!dict.ContainsKey(rig.rigPriority)) { dict.Add(rig.rigPriority, new List()); } dict[rig.rigPriority].Add(rig); } public void RemoveRig(Rig rig) { Dictionary> dict; Rig.UpdateMode updateMode; if (rig.canChangeUpdateMode) updateMode = rig.updateMode; else updateMode = rig.defaultUpdateMode; switch (updateMode) { default: case Rig.UpdateMode.Update: dict = m_UpdateRigs; break; case Rig.UpdateMode.LateUpdate: dict = m_LateUpdateRigs; break; case Rig.UpdateMode.FixedUpdate: dict = m_FixedUpdateRigs; break; } int priority = rig.rigPriority; if (dict.ContainsKey(priority) && dict[priority].Contains(rig)) { dict[priority].Remove(rig); } else { Debug.LogError($"Could not remove rig {rig.gameObject.name} ({rig.GetType().Name}) from {updateMode}/#{priority.ToString()}"); } if(dict.ContainsKey(priority) && dict[priority].Count == 0) { dict.Remove(priority); } } void UpdateRigDictionary(Dictionary> dict, float deltaTime) { var priorities = dict.Keys.OrderBy(i => i); foreach(int priority in priorities) { foreach(var rig in dict[priority]) { rig.UpdateRig(deltaTime); } } } public void Update() { UpdateRigDictionary(m_UpdateRigs, Time.deltaTime); } public void FixedUpdate() { UpdateRigDictionary(m_FixedUpdateRigs, Time.fixedDeltaTime); } public void LateUpdate() { UpdateRigDictionary(m_LateUpdateRigs, Time.deltaTime); } } }