Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

126 行
3.6 KiB

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace GameplayIngredients.Rigs
{
[AddComponentMenu(ComponentMenu.managersPath + "Rig Manager")]
[NonExcludeableManager]
public class RigManager : Manager
{
Dictionary<int, List<Rig>> m_UpdateRigs;
Dictionary<int, List<Rig>> m_LateUpdateRigs;
Dictionary<int, List<Rig>> m_FixedUpdateRigs;
private void OnEnable()
{
m_UpdateRigs = new Dictionary<int, List<Rig>>();
m_LateUpdateRigs = new Dictionary<int, List<Rig>>();
m_FixedUpdateRigs = new Dictionary<int, List<Rig>>();
}
public void RegistedRig(Rig rig)
{
Rig.UpdateMode updateMode;
if (rig.canChangeUpdateMode)
updateMode = rig.updateMode;
else
updateMode = rig.defaultUpdateMode;
Dictionary<int, List<Rig>> 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<Rig>());
}
dict[rig.rigPriority].Add(rig);
}
public void RemoveRig(Rig rig)
{
Dictionary<int, List<Rig>> 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<int, List<Rig>> dict, float deltaTime)
{
var priorities = dict.Keys.OrderBy(i => i);
foreach(int priority in priorities)
{
if(dict.ContainsKey(priority))
{
foreach (var rig in dict[priority].ToArray())
{
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);
}
}
}