Skip to content

Commit 773d5f3

Browse files
fix: NetworkAnimator parameters writebuffer is limited [backport] (#3124)
* fix Pre-calculate the required parameter write size needed as opposed to using the legacy constant value. * update adding changelog entry * update Adding PR number to changelog entry
1 parent 2da4dd5 commit 773d5f3

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1717

1818
### Fixed
1919

20+
- Fixed issue where `NetworkAnimator` would statically allocate write buffer space for `Animator` parameters that could cause a write error if the number of parameters exceeded the space allocated. (#3124)
2021
- Fixed issue with the in-scene network prefab instance update menu tool where it was not properly updating scenes when invoked on the root prefab instance. (#3084)
2122
- Fixed issue where `NetworkAnimator` would send updates to non-observer clients. (#3058)
2223
- Fixed issue where an exception could occur when receiving a universal RPC for a `NetworkObject` that has been despawned. (#3055)

com.unity.netcode.gameobjects/Components/NetworkAnimator.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,6 @@ protected virtual bool OnIsServerAuthoritative()
481481
return true;
482482
}
483483

484-
// Animators only support up to 32 parameters
485-
// TODO: Look into making this a range limited property
486-
private const int k_MaxAnimationParams = 32;
487-
488484
private int[] m_TransitionHash;
489485
private int[] m_AnimationHash;
490486
private float[] m_LayerWeights;
@@ -508,7 +504,7 @@ private unsafe struct AnimatorParamCache
508504
}
509505

510506
// 128 bytes per Animator
511-
private FastBufferWriter m_ParameterWriter = new FastBufferWriter(k_MaxAnimationParams * sizeof(float), Allocator.Persistent);
507+
private FastBufferWriter m_ParameterWriter;
512508

513509
private NativeArray<AnimatorParamCache> m_CachedAnimatorParameters;
514510

@@ -560,6 +556,14 @@ public override void OnDestroy()
560556

561557
private void Awake()
562558
{
559+
if (!m_Animator)
560+
{
561+
#if !UNITY_EDITOR
562+
Debug.LogError($"{nameof(NetworkAnimator)} {name} does not have an {nameof(UnityEngine.Animator)} assigned to it. The {nameof(NetworkAnimator)} will not initialize properly.");
563+
#endif
564+
return;
565+
}
566+
563567
int layers = m_Animator.layerCount;
564568
// Initializing the below arrays for everyone handles an issue
565569
// when running in owner authoritative mode and the owner changes.
@@ -589,6 +593,9 @@ private void Awake()
589593
}
590594
}
591595

596+
// The total initialization size calculated for the m_ParameterWriter write buffer.
597+
var totalParameterSize = sizeof(uint);
598+
592599
// Build our reference parameter values to detect when they change
593600
var parameters = m_Animator.parameters;
594601
m_CachedAnimatorParameters = new NativeArray<AnimatorParamCache>(parameters.Length, Allocator.Persistent);
@@ -629,7 +636,37 @@ private void Awake()
629636
}
630637

631638
m_CachedAnimatorParameters[i] = cacheParam;
639+
640+
// Calculate parameter sizes (index + type size)
641+
switch (parameter.type)
642+
{
643+
case AnimatorControllerParameterType.Int:
644+
{
645+
totalParameterSize += sizeof(int) * 2;
646+
break;
647+
}
648+
case AnimatorControllerParameterType.Bool:
649+
case AnimatorControllerParameterType.Trigger:
650+
{
651+
// Bool is serialized to 1 byte
652+
totalParameterSize += sizeof(int) + 1;
653+
break;
654+
}
655+
case AnimatorControllerParameterType.Float:
656+
{
657+
totalParameterSize += sizeof(int) + sizeof(float);
658+
break;
659+
}
660+
}
632661
}
662+
663+
if (m_ParameterWriter.IsInitialized)
664+
{
665+
m_ParameterWriter.Dispose();
666+
}
667+
668+
// Create our parameter write buffer for serialization
669+
m_ParameterWriter = new FastBufferWriter(totalParameterSize, Allocator.Persistent);
633670
}
634671

635672
/// <summary>

0 commit comments

Comments
 (0)