Skip to content

Commit 88ee99c

Browse files
fix: network animator override (#1735)
* fix: using animator override controller breaks parameters * animator tests * addred trigger reset test * added hash version of tests * perf improvement idea from Noel * changelog
1 parent f809fb4 commit 88ee99c

20 files changed

+865
-25
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ Additional documentation and release notes are available at [Multiplayer Documen
99
## [Unreleased]
1010

1111
### Added
12+
- First set of tests for NetworkAnimator - parameter syncing, trigger set / reset, override network animator (#7135)
1213

1314
### Changed
1415

1516
### Fixed
16-
1717
- Fixed an issue where sometimes the first client to connect to the server could see messages from the server as coming from itself. (#1683)
1818
- Fixed an issue where clients seemed to be able to send messages to ClientId 1, but these messages would actually still go to the server (id 0) instead of that client. (#1683)
1919
- Improved clarity of error messaging when a client attempts to send a message to a destination other than the server, which isn't allowed. (#1683)
@@ -27,6 +27,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
2727
- Fixed NetworkList to properly call INetworkSerializable's NetworkSerialize() method (#1682)
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)
30+
- Improved performance in NetworkAnimator (#1735)
31+
- Fixed display over "always sync" network animator parameters even when an animator controller override is in use (#1735)
3032

3133
## [1.0.0-pre.5] - 2022-01-26
3234

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

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ public Animator Animator
6262
}
6363
}
6464

65-
/*
66-
* AutoSend is the ability to select which parameters linked to this animator
65+
/*
66+
* AutoSend is the ability to select which parameters linked to this animator
6767
* 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 explict
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
7171
* by selecting it in the inspector when an NetworkAnimator is selected.
7272
*/
7373
public void SetParameterAutoSend(int index, bool value)
@@ -87,6 +87,8 @@ public bool GetParameterAutoSend(int index)
8787
return (m_ParameterSendBits & (uint)(1 << index)) != 0;
8888
}
8989

90+
private bool m_SendMessagesAllowed = false;
91+
9092
// Animators only support up to 32 params
9193
public static int K_MaxAnimationParams = 32;
9294

@@ -103,11 +105,11 @@ private unsafe struct AnimatorParamCache
103105
public fixed byte Value[4]; // this is a max size of 4 bytes
104106
}
105107

106-
// 128bytes per Animator
108+
// 128bytes per Animator
107109
private FastBufferWriter m_ParameterWriter = new FastBufferWriter(K_MaxAnimationParams * sizeof(float), Allocator.Persistent);
108110
private NativeArray<AnimatorParamCache> m_CachedAnimatorParameters;
109111

110-
// We cache these values because UnsafeUtility.EnumToInt use direct IL that allows a nonboxing conversion
112+
// We cache these values because UnsafeUtility.EnumToInt uses direct IL that allows a non-boxing conversion
111113
private struct AnimationParamEnumWrapper
112114
{
113115
public static readonly int AnimatorControllerParameterInt;
@@ -133,14 +135,6 @@ internal void ResetParameterOptions()
133135
m_ParameterSendBits = 0;
134136
}
135137

136-
private bool sendMessagesAllowed
137-
{
138-
get
139-
{
140-
return IsServer && NetworkObject.IsSpawned;
141-
}
142-
}
143-
144138
public override void OnDestroy()
145139
{
146140
if (m_CachedAnimatorParameters.IsCreated)
@@ -153,6 +147,10 @@ public override void OnDestroy()
153147

154148
public override void OnNetworkSpawn()
155149
{
150+
if (IsServer)
151+
{
152+
m_SendMessagesAllowed = true;
153+
}
156154
var parameters = m_Animator.parameters;
157155
m_CachedAnimatorParameters = new NativeArray<AnimatorParamCache>(parameters.Length, Allocator.Persistent);
158156

@@ -199,9 +197,14 @@ public override void OnNetworkSpawn()
199197
}
200198
}
201199

200+
public override void OnNetworkDespawn()
201+
{
202+
m_SendMessagesAllowed = false;
203+
}
204+
202205
private void FixedUpdate()
203206
{
204-
if (!sendMessagesAllowed)
207+
if (!m_SendMessagesAllowed)
205208
{
206209
return;
207210
}
@@ -233,7 +236,7 @@ private void FixedUpdate()
233236
private void CheckAndSend()
234237
{
235238
var networkTime = NetworkManager.ServerTime.Time;
236-
if (sendMessagesAllowed && m_SendRate != 0 && m_NextSendTime < networkTime)
239+
if (m_SendMessagesAllowed && m_SendRate != 0 && m_NextSendTime < networkTime)
237240
{
238241
m_NextSendTime = networkTime + m_SendRate;
239242

@@ -290,8 +293,8 @@ private bool CheckAnimStateChanged(out int stateHash, out float normalizedTime)
290293

291294
/* $AS TODO: Right now we are not checking for changed values this is because
292295
the read side of this function doesn't have similar logic which would cause
293-
an overflow read because it doesn't know if the value is there or not. So
294-
there needs to be logic to track which indexes changed in order for there
296+
an overflow read because it doesn't know if the value is there or not. So
297+
there needs to be logic to track which indexes changed in order for there
295298
to be proper value change checking. Will revist in 1.1.0.
296299
*/
297300
private unsafe bool WriteParameters(FastBufferWriter writer, bool autoSend)
@@ -438,11 +441,24 @@ private void SendAnimTriggerClientRpc(AnimationTriggerMessage animSnapshot, Clie
438441
}
439442
}
440443

444+
/// <summary>
445+
/// Sets the trigger for the associated animation
446+
/// Note, triggers are special vs other kinds of parameters. For all the other parameters we watch for changes
447+
/// in FixedUpdate and users can just set them normally off of Animator. But because triggers are transitory
448+
/// and likely to come and go between FixedUpdate calls, we require users to set them here to guarantee us to
449+
/// catch it...then we forward it to the Animator component
450+
/// </summary>
451+
/// <param name="triggerName">The string name of the trigger to activate</param>
441452
public void SetTrigger(string triggerName)
442453
{
443454
SetTrigger(Animator.StringToHash(triggerName));
444455
}
445456

457+
/// <summary>
458+
/// Sets the trigger for the associated animation. See note for SetTrigger(string)
459+
/// </summary>
460+
/// <param name="hash">The hash for the trigger to activate</param>
461+
/// <param name="reset">If true, resets the trigger</param>
446462
public void SetTrigger(int hash, bool reset = false)
447463
{
448464
var animMsg = new AnimationTriggerMessage();
@@ -455,11 +471,19 @@ public void SetTrigger(int hash, bool reset = false)
455471
}
456472
}
457473

474+
/// <summary>
475+
/// Resets the trigger for the associated animation. See note for SetTrigger(string)
476+
/// </summary>
477+
/// <param name="triggerName">The string name of the trigger to reset</param>
458478
public void ResetTrigger(string triggerName)
459479
{
460480
ResetTrigger(Animator.StringToHash(triggerName));
461481
}
462482

483+
/// <summary>
484+
/// Resets the trigger for the associated animation. See note for SetTrigger(string)
485+
/// </summary>
486+
/// <param name="hash">The hash for the trigger to reset</param>
463487
public void ResetTrigger(int hash)
464488
{
465489
SetTrigger(hash, true);

com.unity.netcode.gameobjects/Editor/NetworkAnimatorEditor.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,17 @@ private void DrawControls()
6666
return;
6767
}
6868

69-
var controller = m_AnimSync.Animator.runtimeAnimatorController as AnimatorController;
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+
7080
if (controller != null)
7181
{
7282
var showWarning = false;

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,7 @@ internal void InternalOnNetworkSpawn()
370370
InitializeVariables();
371371
}
372372

373-
internal void InternalOnNetworkDespawn()
374-
{
375-
376-
}
373+
internal void InternalOnNetworkDespawn() { }
377374

378375
/// <summary>
379376
/// Gets called when the local client gains ownership of this object

com.unity.netcode.gameobjects/Tests/Runtime/NetworkAnimator.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)