Skip to content

Commit c2e75b7

Browse files
fix: remove auto-send animation parameters
1 parent 40703a0 commit c2e75b7

File tree

3 files changed

+22
-220
lines changed

3 files changed

+22
-220
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2828
- Fixed The NetworkConfig's checksum hash includes the NetworkTick so that clients with a different tickrate than the server are identified and not allowed to connect (#1728)
2929
- Fixed OwnedObjects not being properly modified when using ChangeOwnership (#1731)
3030
- Improved performance in NetworkAnimator (#1735)
31-
- Fixed display over "always sync" network animator parameters even when an animator controller override is in use (#1735)
31+
- Removed the "always sync" network animator (aka "autosend") parameters (#1746)
3232

3333
## [1.0.0-pre.5] - 2022-01-26
3434

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

Lines changed: 17 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
2626
}
2727
}
2828

29-
internal struct AnimationParametersMessage : INetworkSerializable
30-
{
31-
public byte[] Parameters;
32-
33-
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
34-
{
35-
serializer.SerializeValue(ref Parameters);
36-
}
37-
}
38-
3929
internal struct AnimationTriggerMessage : INetworkSerializable
4030
{
4131
public int Hash;
@@ -49,51 +39,22 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
4939
}
5040

5141
[SerializeField] private Animator m_Animator;
52-
[SerializeField] private uint m_ParameterSendBits;
53-
[SerializeField] private float m_SendRate = 0.1f;
5442

5543
public Animator Animator
5644
{
5745
get { return m_Animator; }
5846
set
5947
{
6048
m_Animator = value;
61-
ResetParameterOptions();
62-
}
63-
}
64-
65-
/*
66-
* AutoSend is the ability to select which parameters linked to this animator
67-
* get replicated on a regular basis regardless of a state change. The thinking
68-
* behind this is that many of the parameters people use are usually booleans
69-
* which result in a state change and thus would cause a full sync of state.
70-
* Thus if you really care about a parameter syncing then you need to be explicit
71-
* by selecting it in the inspector when an NetworkAnimator is selected.
72-
*/
73-
public void SetParameterAutoSend(int index, bool value)
74-
{
75-
if (value)
76-
{
77-
m_ParameterSendBits |= (uint)(1 << index);
78-
}
79-
else
80-
{
81-
m_ParameterSendBits &= (uint)(~(1 << index));
8249
}
8350
}
8451

85-
public bool GetParameterAutoSend(int index)
86-
{
87-
return (m_ParameterSendBits & (uint)(1 << index)) != 0;
88-
}
89-
9052
private bool m_SendMessagesAllowed = false;
9153

9254
// Animators only support up to 32 params
9355
public static int K_MaxAnimationParams = 32;
9456

9557
private int m_TransitionHash;
96-
private double m_NextSendTime = 0.0f;
9758

9859
private int m_AnimationHash;
9960
public int AnimationHash { get => m_AnimationHash; }
@@ -105,7 +66,7 @@ private unsafe struct AnimatorParamCache
10566
public fixed byte Value[4]; // this is a max size of 4 bytes
10667
}
10768

108-
// 128bytes per Animator
69+
// 128 bytes per Animator
10970
private FastBufferWriter m_ParameterWriter = new FastBufferWriter(K_MaxAnimationParams * sizeof(float), Allocator.Persistent);
11071
private NativeArray<AnimatorParamCache> m_CachedAnimatorParameters;
11172

@@ -124,17 +85,6 @@ static AnimationParamEnumWrapper()
12485
}
12586
}
12687

127-
internal void ResetParameterOptions()
128-
{
129-
130-
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
131-
{
132-
NetworkLog.LogInfoServer("ResetParameterOptions");
133-
}
134-
135-
m_ParameterSendBits = 0;
136-
}
137-
13888
public override void OnDestroy()
13989
{
14090
if (m_CachedAnimatorParameters.IsCreated)
@@ -162,14 +112,17 @@ public override void OnNetworkSpawn()
162112

163113
if (m_Animator.IsParameterControlledByCurve(parameter.nameHash))
164114
{
165-
//we are ignoring parameters that are controlled by animation curves - syncing the layer states indirectly syncs the values that are driven by the animation curves
115+
// we are ignoring parameters that are controlled by animation curves - syncing the layer
116+
// states indirectly syncs the values that are driven by the animation curves
166117
continue;
167118
}
168119

169-
var cacheParam = new AnimatorParamCache();
120+
var cacheParam = new AnimatorParamCache
121+
{
122+
Type = UnsafeUtility.EnumToInt(parameter.type),
123+
Hash = parameter.nameHash
124+
};
170125

171-
cacheParam.Type = UnsafeUtility.EnumToInt(parameter.type);
172-
cacheParam.Hash = parameter.nameHash;
173126
unsafe
174127
{
175128
switch (parameter.type)
@@ -213,49 +166,24 @@ private void FixedUpdate()
213166
float normalizedTime;
214167
if (!CheckAnimStateChanged(out stateHash, out normalizedTime))
215168
{
216-
// We only want to check and send if we don't have any other state to since
217-
// as we will sync all params as part of the state sync
218-
CheckAndSend();
219-
220169
return;
221170
}
222171

223-
var animMsg = new AnimationMessage();
224-
animMsg.StateHash = stateHash;
225-
animMsg.NormalizedTime = normalizedTime;
172+
var animMsg = new AnimationMessage
173+
{
174+
StateHash = stateHash,
175+
NormalizedTime = normalizedTime
176+
};
226177

227178
m_ParameterWriter.Seek(0);
228179
m_ParameterWriter.Truncate();
229180

230-
WriteParameters(m_ParameterWriter, false);
181+
WriteParameters(m_ParameterWriter);
231182
animMsg.Parameters = m_ParameterWriter.ToArray();
232183

233184
SendAnimStateClientRpc(animMsg);
234185
}
235186

236-
private void CheckAndSend()
237-
{
238-
var networkTime = NetworkManager.ServerTime.Time;
239-
if (m_SendMessagesAllowed && m_SendRate != 0 && m_NextSendTime < networkTime)
240-
{
241-
m_NextSendTime = networkTime + m_SendRate;
242-
243-
m_ParameterWriter.Seek(0);
244-
m_ParameterWriter.Truncate();
245-
246-
if (WriteParameters(m_ParameterWriter, true))
247-
{
248-
// we then sync the params we care about
249-
var animMsg = new AnimationParametersMessage()
250-
{
251-
Parameters = m_ParameterWriter.ToArray()
252-
};
253-
254-
SendParamsClientRpc(animMsg);
255-
}
256-
}
257-
}
258-
259187
private bool CheckAnimStateChanged(out int stateHash, out float normalizedTime)
260188
{
261189
stateHash = 0;
@@ -297,20 +225,10 @@ the read side of this function doesn't have similar logic which would cause
297225
there needs to be logic to track which indexes changed in order for there
298226
to be proper value change checking. Will revist in 1.1.0.
299227
*/
300-
private unsafe bool WriteParameters(FastBufferWriter writer, bool autoSend)
228+
private unsafe void WriteParameters(FastBufferWriter writer)
301229
{
302-
if (m_CachedAnimatorParameters == null)
303-
{
304-
return false;
305-
}
306-
307230
for (int i = 0; i < m_CachedAnimatorParameters.Length; i++)
308231
{
309-
if (autoSend && !GetParameterAutoSend(i))
310-
{
311-
continue;
312-
}
313-
314232
ref var cacheValue = ref UnsafeUtility.ArrayElementAsRef<AnimatorParamCache>(m_CachedAnimatorParameters.GetUnsafePtr(), i);
315233
var hash = cacheValue.Hash;
316234

@@ -343,24 +261,12 @@ private unsafe bool WriteParameters(FastBufferWriter writer, bool autoSend)
343261
}
344262
}
345263
}
346-
347-
// If we do not write any values to the writer then we should not send any data
348-
return writer.Length > 0;
349264
}
350265

351-
private unsafe void ReadParameters(FastBufferReader reader, bool autoSend)
266+
private unsafe void ReadParameters(FastBufferReader reader)
352267
{
353-
if (m_CachedAnimatorParameters == null)
354-
{
355-
return;
356-
}
357-
358268
for (int i = 0; i < m_CachedAnimatorParameters.Length; i++)
359269
{
360-
if (autoSend && !GetParameterAutoSend(i))
361-
{
362-
continue;
363-
}
364270
ref var cacheValue = ref UnsafeUtility.ArrayElementAsRef<AnimatorParamCache>(m_CachedAnimatorParameters.GetUnsafePtr(), i);
365271
var hash = cacheValue.Hash;
366272

@@ -394,20 +300,6 @@ private unsafe void ReadParameters(FastBufferReader reader, bool autoSend)
394300
}
395301
}
396302

397-
[ClientRpc]
398-
private unsafe void SendParamsClientRpc(AnimationParametersMessage animSnapshot, ClientRpcParams clientRpcParams = default)
399-
{
400-
if (animSnapshot.Parameters != null)
401-
{
402-
// We use a fixed value here to avoid the copy of data from the byte buffer since we own the data
403-
fixed (byte* parameters = animSnapshot.Parameters)
404-
{
405-
var reader = new FastBufferReader(parameters, Allocator.None, animSnapshot.Parameters.Length);
406-
ReadParameters(reader, true);
407-
}
408-
}
409-
}
410-
411303
[ClientRpc]
412304
private unsafe void SendAnimStateClientRpc(AnimationMessage animSnapshot, ClientRpcParams clientRpcParams = default)
413305
{
@@ -423,7 +315,7 @@ private unsafe void SendAnimStateClientRpc(AnimationMessage animSnapshot, Client
423315
fixed (byte* parameters = animSnapshot.Parameters)
424316
{
425317
var reader = new FastBufferReader(parameters, Allocator.None, animSnapshot.Parameters.Length);
426-
ReadParameters(reader, false);
318+
ReadParameters(reader);
427319
}
428320
}
429321
}
Lines changed: 4 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,23 @@
1-
using System;
21
using Unity.Netcode.Components;
32
using UnityEditor;
4-
using UnityEditor.Animations;
53
using UnityEngine;
64

75
namespace Unity.Netcode.Editor
86
{
9-
public static class TextUtility
10-
{
11-
public static GUIContent TextContent(string name, string tooltip)
12-
{
13-
var newContent = new GUIContent(name);
14-
newContent.tooltip = tooltip;
15-
return newContent;
16-
}
17-
18-
public static GUIContent TextContent(string name)
19-
{
20-
return new GUIContent(name);
21-
}
22-
}
23-
247
[CustomEditor(typeof(NetworkAnimator), true)]
258
[CanEditMultipleObjects]
269
public class NetworkAnimatorEditor : UnityEditor.Editor
2710
{
28-
private NetworkAnimator m_AnimSync;
29-
[NonSerialized] private bool m_Initialized;
30-
private SerializedProperty m_AnimatorProperty;
31-
private GUIContent m_AnimatorLabel;
32-
33-
private void Init()
34-
{
35-
if (m_Initialized)
36-
{
37-
return;
38-
}
39-
40-
m_Initialized = true;
41-
m_AnimSync = target as NetworkAnimator;
42-
43-
m_AnimatorProperty = serializedObject.FindProperty("m_Animator");
44-
m_AnimatorLabel = TextUtility.TextContent("Animator", "The Animator component to synchronize.");
45-
}
46-
4711
public override void OnInspectorGUI()
4812
{
49-
Init();
5013
serializedObject.Update();
51-
DrawControls();
52-
serializedObject.ApplyModifiedProperties();
53-
}
5414

55-
private void DrawControls()
56-
{
5715
EditorGUI.BeginChangeCheck();
58-
EditorGUILayout.PropertyField(m_AnimatorProperty, m_AnimatorLabel);
59-
if (EditorGUI.EndChangeCheck())
60-
{
61-
m_AnimSync.ResetParameterOptions();
62-
}
63-
64-
if (m_AnimSync.Animator == null)
65-
{
66-
return;
67-
}
16+
var label = new GUIContent("Animator", "The Animator component to synchronize");
17+
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Animator"), label);
18+
EditorGUI.EndChangeCheck();
6819

69-
AnimatorController controller;
70-
var overrideController = m_AnimSync.Animator.runtimeAnimatorController as AnimatorOverrideController;
71-
if (overrideController != null)
72-
{
73-
controller = overrideController.runtimeAnimatorController as AnimatorController;
74-
}
75-
else
76-
{
77-
controller = m_AnimSync.Animator.runtimeAnimatorController as AnimatorController;
78-
}
79-
80-
if (controller != null)
81-
{
82-
var showWarning = false;
83-
EditorGUI.indentLevel += 1;
84-
int i = 0;
85-
86-
foreach (var p in controller.parameters)
87-
{
88-
if (i >= NetworkAnimator.K_MaxAnimationParams)
89-
{
90-
showWarning = true;
91-
break;
92-
}
93-
94-
bool oldSend = m_AnimSync.GetParameterAutoSend(i);
95-
bool send = EditorGUILayout.Toggle(p.name, oldSend);
96-
if (send != oldSend)
97-
{
98-
m_AnimSync.SetParameterAutoSend(i, send);
99-
EditorUtility.SetDirty(target);
100-
}
101-
i += 1;
102-
}
103-
104-
if (showWarning)
105-
{
106-
EditorGUILayout.HelpBox($"NetworkAnimator can only select between the first {NetworkAnimator.K_MaxAnimationParams} parameters in a mecanim controller", MessageType.Warning);
107-
}
108-
109-
EditorGUI.indentLevel -= 1;
110-
}
20+
serializedObject.ApplyModifiedProperties();
11121
}
11222
}
11323
}

0 commit comments

Comments
 (0)