-
Notifications
You must be signed in to change notification settings - Fork 461
fix: NetworkAnimator does not update animator's parameters unless the animator's layer changes [MTT-3492] #1946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
2c9715f
5eb6691
c17bf81
881f701
b198c36
3c1158f
586b8a6
b645a5a
395fe2d
c969dd4
7aee925
e7ccb41
0de2cb6
15b3e92
26a5424
ffe55fc
db9a051
f28ba3f
51fea68
73adf63
3e867cf
04b247a
19e6605
00cf503
808fb7e
b0cbc07
faadd4d
f96e012
b01fe0e
3427152
5df40ee
d7a6402
9337020
080301f
01b7e76
85d359b
6eb0b1c
28138a0
d65f045
1099dc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||
| #if COM_UNITY_MODULES_ANIMATION | ||||||
| using System.Collections.Generic; | ||||||
| using Unity.Collections; | ||||||
| using Unity.Collections.LowLevel.Unsafe; | ||||||
| using UnityEngine; | ||||||
|
|
@@ -70,8 +71,25 @@ private unsafe struct AnimatorParamCache | |||||
| internal fixed byte Value[4]; // this is a max size of 4 bytes | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Used to track parameter state change in order to update the changed | ||||||
| /// parameters when the animator's layer has not changed | ||||||
| /// </summary> | ||||||
| private class ParameterState | ||||||
| { | ||||||
| public AnimatorControllerParameterType AnimatorControllerParameterType; | ||||||
| public bool CurrentStateBool; | ||||||
| public int CurrentStateInt; | ||||||
| public float CurrentStateFloat; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| private Dictionary<int, ParameterState> m_ParameterStates = new Dictionary<int, ParameterState>(); | ||||||
|
|
||||||
| // 128 bytes per Animator | ||||||
| private FastBufferWriter m_ParameterWriter = new FastBufferWriter(k_MaxAnimationParams * sizeof(float), Allocator.Persistent); | ||||||
|
|
||||||
|
|
||||||
| private NativeArray<AnimatorParamCache> m_CachedAnimatorParameters; | ||||||
|
|
||||||
| // We cache these values because UnsafeUtility.EnumToInt uses direct IL that allows a non-boxing conversion | ||||||
|
|
@@ -138,15 +156,29 @@ public override void OnNetworkSpawn() | |||||
| case AnimatorControllerParameterType.Float: | ||||||
| var value = m_Animator.GetFloat(cacheParam.Hash); | ||||||
| UnsafeUtility.WriteArrayElement(cacheParam.Value, 0, value); | ||||||
| // Track this parameter | ||||||
| if (!m_ParameterStates.ContainsKey(i)) | ||||||
| { | ||||||
| m_ParameterStates.Add(i, new ParameterState() { CurrentStateFloat = value, AnimatorControllerParameterType = AnimatorControllerParameterType.Float }); | ||||||
| } | ||||||
| break; | ||||||
| case AnimatorControllerParameterType.Int: | ||||||
| var valueInt = m_Animator.GetInteger(cacheParam.Hash); | ||||||
| UnsafeUtility.WriteArrayElement(cacheParam.Value, 0, valueInt); | ||||||
|
|
||||||
| // Track this parameter | ||||||
| if (!m_ParameterStates.ContainsKey(i)) | ||||||
| { | ||||||
| m_ParameterStates.Add(i, new ParameterState() { CurrentStateInt = valueInt, AnimatorControllerParameterType = AnimatorControllerParameterType.Int }); | ||||||
| } | ||||||
| break; | ||||||
| case AnimatorControllerParameterType.Bool: | ||||||
| var valueBool = m_Animator.GetBool(cacheParam.Hash); | ||||||
| UnsafeUtility.WriteArrayElement(cacheParam.Value, 0, valueBool); | ||||||
| // Track this parameter | ||||||
| if (!m_ParameterStates.ContainsKey(i)) | ||||||
| { | ||||||
| m_ParameterStates.Add(i, new ParameterState() { CurrentStateBool = valueBool, AnimatorControllerParameterType = AnimatorControllerParameterType.Bool }); | ||||||
| } | ||||||
| break; | ||||||
| case AnimatorControllerParameterType.Trigger: | ||||||
| default: | ||||||
|
|
@@ -197,7 +229,7 @@ private void FixedUpdate() | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| private bool CheckAnimStateChanged(out int stateHash, out float normalizedTime, int layer) | ||||||
| unsafe private bool CheckAnimStateChanged(out int stateHash, out float normalizedTime, int layer) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied suggestion |
||||||
| { | ||||||
| bool shouldUpdate = false; | ||||||
| stateHash = 0; | ||||||
|
|
@@ -239,6 +271,43 @@ private bool CheckAnimStateChanged(out int stateHash, out float normalizedTime, | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Whether the layer has changed or not, we always check to see if parameters have changed and send updates if they have. | ||||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think for a future PR we could create a full index of what changed and then send only what has changed. |
||||||
| for (int i = 0; i < m_CachedAnimatorParameters.Length; i++) | ||||||
| { | ||||||
| ref var cacheValue = ref UnsafeUtility.ArrayElementAsRef<AnimatorParamCache>(m_CachedAnimatorParameters.GetUnsafePtr(), i); | ||||||
| var hash = cacheValue.Hash; | ||||||
| if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterInt) | ||||||
| { | ||||||
| var valueInt = m_Animator.GetInteger(hash); | ||||||
| var currentState = m_ParameterStates[i].CurrentStateInt; | ||||||
| if (valueInt != currentState) | ||||||
| { | ||||||
| shouldUpdate = true; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterBool) | ||||||
| { | ||||||
| var valueBool = m_Animator.GetBool(hash); | ||||||
| var currentState = (bool)m_ParameterStates[i].CurrentStateBool; | ||||||
| if (valueBool != currentState) | ||||||
| { | ||||||
| shouldUpdate = true; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterFloat) | ||||||
| { | ||||||
| var valueFloat = m_Animator.GetFloat(hash); | ||||||
| var currentState = (float)m_ParameterStates[i].CurrentStateFloat; | ||||||
| if (Mathf.Abs(valueFloat - currentState) > 0.00f) | ||||||
| { | ||||||
| shouldUpdate = true; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return shouldUpdate; | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -258,6 +327,7 @@ private unsafe void WriteParameters(FastBufferWriter writer) | |||||
| if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterInt) | ||||||
| { | ||||||
| var valueInt = m_Animator.GetInteger(hash); | ||||||
| m_ParameterStates[i].CurrentStateInt = valueInt; | ||||||
| fixed (void* value = cacheValue.Value) | ||||||
| { | ||||||
| UnsafeUtility.WriteArrayElement(value, 0, valueInt); | ||||||
|
|
@@ -267,6 +337,7 @@ private unsafe void WriteParameters(FastBufferWriter writer) | |||||
| else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterBool) | ||||||
| { | ||||||
| var valueBool = m_Animator.GetBool(hash); | ||||||
| m_ParameterStates[i].CurrentStateBool = valueBool; | ||||||
| fixed (void* value = cacheValue.Value) | ||||||
| { | ||||||
| UnsafeUtility.WriteArrayElement(value, 0, valueBool); | ||||||
|
|
@@ -276,6 +347,7 @@ private unsafe void WriteParameters(FastBufferWriter writer) | |||||
| else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterFloat) | ||||||
| { | ||||||
| var valueFloat = m_Animator.GetFloat(hash); | ||||||
| m_ParameterStates[i].CurrentStateFloat = valueFloat; | ||||||
| fixed (void* value = cacheValue.Value) | ||||||
| { | ||||||
|
|
||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.