Network animator optimizations (#55)

* Store animator parameters in a variable to avoid expensive get operation

* Convert series of if statements to switch

* Add a comment explaining the optimization

* Convert switch statement to else-if

* Fix typo

* Revert changes to if statements
This commit is contained in:
mateusz 2018-10-05 17:02:55 +02:00 committed by vis2k
parent 40db215fc9
commit 03c49de258

View File

@ -231,12 +231,16 @@ internal void HandleAnimTriggerMsg(int hash)
void WriteParameters(NetworkWriter writer, bool autoSend) void WriteParameters(NetworkWriter writer, bool autoSend)
{ {
for (int i = 0; i < m_Animator.parameters.Length; i++) // store the animator parameters in a variable - the "Animator.parameters" getter allocates
// a new parameter array every time it is accessed so we should avoid doing it in a loop
AnimatorControllerParameter[] parameters = m_Animator.parameters;
for (int i = 0; i < parameters.Length; i++)
{ {
if (autoSend && !GetParameterAutoSend(i)) if (autoSend && !GetParameterAutoSend(i))
continue; continue;
AnimatorControllerParameter par = m_Animator.parameters[i]; AnimatorControllerParameter par = parameters[i];
if (par.type == AnimatorControllerParameterType.Int) if (par.type == AnimatorControllerParameterType.Int)
{ {
writer.WritePackedUInt32((uint)m_Animator.GetInteger(par.nameHash)); writer.WritePackedUInt32((uint)m_Animator.GetInteger(par.nameHash));
@ -262,12 +266,16 @@ void WriteParameters(NetworkWriter writer, bool autoSend)
void ReadParameters(NetworkReader reader, bool autoSend) void ReadParameters(NetworkReader reader, bool autoSend)
{ {
for (int i = 0; i < m_Animator.parameters.Length; i++) // store the animator parameters in a variable - the "Animator.parameters" getter allocates
// a new parameter array every time it is accessed so we should avoid doing it in a loop
AnimatorControllerParameter[] parameters = m_Animator.parameters;
for (int i = 0; i < parameters.Length; i++)
{ {
if (autoSend && !GetParameterAutoSend(i)) if (autoSend && !GetParameterAutoSend(i))
continue; continue;
AnimatorControllerParameter par = m_Animator.parameters[i]; AnimatorControllerParameter par = parameters[i];
if (par.type == AnimatorControllerParameterType.Int) if (par.type == AnimatorControllerParameterType.Int)
{ {
int newValue = (int)reader.ReadPackedUInt32(); int newValue = (int)reader.ReadPackedUInt32();