Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
2c9715f
fix
NoelStephensUnity May 6, 2022
5eb6691
test and style
NoelStephensUnity May 9, 2022
c17bf81
style
NoelStephensUnity May 9, 2022
881f701
style
NoelStephensUnity May 9, 2022
b198c36
Merge branch 'develop' into fix/network-animator-single-layer
NoelStephensUnity May 9, 2022
3c1158f
update and test
NoelStephensUnity May 9, 2022
586b8a6
style
NoelStephensUnity May 9, 2022
b645a5a
Merge branch 'develop' into fix/network-animator-single-layer
NoelStephensUnity May 9, 2022
395fe2d
Merge branch 'develop' into fix/network-animator-single-layer
NoelStephensUnity May 10, 2022
c969dd4
update
NoelStephensUnity May 10, 2022
7aee925
Merge branch 'develop' into fix/network-animator-single-layer
NoelStephensUnity May 10, 2022
e7ccb41
update and fix
NoelStephensUnity May 10, 2022
0de2cb6
update and fix
NoelStephensUnity May 10, 2022
15b3e92
update
NoelStephensUnity May 10, 2022
26a5424
fix
NoelStephensUnity May 11, 2022
ffe55fc
fix
NoelStephensUnity May 11, 2022
db9a051
test
NoelStephensUnity May 11, 2022
f28ba3f
Merge branch 'develop' into fix/network-animator-single-layer
NoelStephensUnity May 11, 2022
51fea68
test and style
NoelStephensUnity May 11, 2022
73adf63
Test and Style
NoelStephensUnity May 11, 2022
3e867cf
Update
NoelStephensUnity May 12, 2022
04b247a
Merge branch 'develop' into fix/network-animator-single-layer
NoelStephensUnity May 12, 2022
19e6605
fix
NoelStephensUnity May 13, 2022
00cf503
wip
NoelStephensUnity May 14, 2022
808fb7e
fix
NoelStephensUnity May 15, 2022
b0cbc07
style
NoelStephensUnity May 15, 2022
faadd4d
style
NoelStephensUnity May 15, 2022
f96e012
update
NoelStephensUnity May 17, 2022
b01fe0e
Merge branch 'develop' into fix/network-animator-single-layer
NoelStephensUnity May 17, 2022
3427152
Merge branch 'develop' into fix/network-animator-single-layer
NoelStephensUnity May 19, 2022
5df40ee
Merge branch 'develop' into fix/network-animator-single-layer
NoelStephensUnity May 19, 2022
d7a6402
update
NoelStephensUnity May 23, 2022
9337020
Test
NoelStephensUnity May 23, 2022
080301f
update - optimize
NoelStephensUnity May 23, 2022
01b7e76
Merge branch 'develop' into fix/network-animator-single-layer
NoelStephensUnity May 23, 2022
85d359b
style
NoelStephensUnity May 23, 2022
6eb0b1c
Merge branch 'develop' into fix/network-animator-single-layer
NoelStephensUnity May 23, 2022
28138a0
update
NoelStephensUnity May 25, 2022
d65f045
update
NoelStephensUnity May 25, 2022
1099dc3
Merge branch 'develop' into fix/network-animator-single-layer
NoelStephensUnity May 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 74 additions & 2 deletions com.unity.netcode.gameobjects/Components/NetworkAnimator.cs
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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Comment thread
NoelStephensUnity marked this conversation as resolved.
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:
Expand Down Expand Up @@ -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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
unsafe private bool CheckAnimStateChanged(out int stateHash, out float normalizedTime, int layer)
private unsafe bool CheckAnimStateChanged(out int stateHash, out float normalizedTime, int layer)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied suggestion

{
bool shouldUpdate = false;
stateHash = 0;
Expand Down Expand Up @@ -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.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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 this PR I kept it relatively simple since updating meant it would send all values anyway.

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;
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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)
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,31 @@ AnimatorController:
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 9100000}
m_Controller: {fileID: 0}
- m_Name: Pulse
m_Type: 9
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 9100000}
m_Controller: {fileID: 0}
- m_Name: TestFloat
m_Type: 1
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 0}
- m_Name: TestBool
m_Type: 4
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 0}
- m_Name: TestInt
m_Type: 3
m_DefaultFloat: 0
m_DefaultInt: 0
m_DefaultBool: 0
m_Controller: {fileID: 0}
m_AnimatorLayers:
- serializedVersion: 5
m_Name: Base Layer
Expand Down
Loading