11#if COM_UNITY_MODULES_ANIMATION
22using System ;
33using System . Collections . Generic ;
4+ using System . Runtime . CompilerServices ;
45using Unity . Collections ;
56using Unity . Collections . LowLevel . Unsafe ;
67using UnityEngine ;
@@ -72,32 +73,46 @@ private void FlushMessages()
7273 m_SendTriggerUpdates . Clear ( ) ;
7374 }
7475
76+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
77+ private bool HasAuthority ( )
78+ {
79+ var isServerAuthority = m_NetworkAnimator . IsServerAuthoritative ( ) ;
80+ return ( ! isServerAuthority && m_NetworkAnimator . IsOwner ) || ( isServerAuthority && m_NetworkAnimator . IsServer ) ;
81+ }
82+
7583 /// <inheritdoc />
7684 public void NetworkUpdate ( NetworkUpdateStage updateStage )
7785 {
7886 switch ( updateStage )
7987 {
8088 case NetworkUpdateStage . PreUpdate :
8189 {
82- // Only the owner or the server send messages
83- if ( m_NetworkAnimator . IsOwner || m_IsServer )
90+ // NOTE: This script has an order of operations requirement where
91+ // the authority and/or server will flush messages first, parameter updates are applied
92+ // for all instances, and then only the authority will check for animator changes. Changing
93+ // the order could cause timing related issues.
94+
95+ var hasAuthority = HasAuthority ( ) ;
96+ // Only the authority or the server will send messages
97+ if ( hasAuthority || m_IsServer )
8498 {
8599 // Flush any pending messages
86100 FlushMessages ( ) ;
87101 }
88102
89103 // Everyone applies any parameters updated
90- for ( int i = 0 ; i < m_ProcessParameterUpdates . Count ; i ++ )
104+ if ( m_ProcessParameterUpdates . Count > 0 )
91105 {
92- var parameterUpdate = m_ProcessParameterUpdates [ i ] ;
93- m_NetworkAnimator . UpdateParameters ( ref parameterUpdate ) ;
106+ for ( int i = 0 ; i < m_ProcessParameterUpdates . Count ; i ++ )
107+ {
108+ var parameterUpdate = m_ProcessParameterUpdates [ i ] ;
109+ m_NetworkAnimator . UpdateParameters ( ref parameterUpdate ) ;
110+ }
111+ m_ProcessParameterUpdates . Clear ( ) ;
94112 }
95- m_ProcessParameterUpdates . Clear ( ) ;
96- var isServerAuthority = m_NetworkAnimator . IsServerAuthoritative ( ) ;
97113
98- // owners when owner authoritative or the server when server authoritative are the only instances that
99- // checks for Animator changes
100- if ( ( ! isServerAuthority && m_NetworkAnimator . IsOwner ) || ( isServerAuthority && m_NetworkAnimator . IsServer ) )
114+ // Only the authority checks for Animator changes
115+ if ( hasAuthority )
101116 {
102117 m_NetworkAnimator . CheckForAnimatorChanges ( ) ;
103118 }
@@ -1511,6 +1526,10 @@ internal void SendAnimStateRpc(AnimationMessage animationMessage)
15111526 ProcessAnimStates ( animationMessage ) ;
15121527 }
15131528
1529+ /// <summary>
1530+ /// Process incoming <see cref="AnimationMessage"/>.
1531+ /// </summary>
1532+ /// <param name="animationMessage">The message to process.</param>
15141533 private void ProcessAnimStates ( AnimationMessage animationMessage )
15151534 {
15161535 if ( HasAuthority )
@@ -1530,8 +1549,6 @@ private void ProcessAnimStates(AnimationMessage animationMessage)
15301549 }
15311550 }
15321551
1533-
1534-
15351552 /// <summary>
15361553 /// Server-side trigger state update request
15371554 /// The server sets its local state and then forwards the message to the remaining clients
@@ -1675,24 +1692,31 @@ public void ResetTrigger(int hash)
16751692 }
16761693
16771694 /// <summary>
1678- /// Allows for the enabling or disabling the synchronization of a specific
1679- /// <see cref="Animator"/> parameter.
1695+ /// Allows for the enabling or disabling the synchronization of a specific <see cref="UnityEngine.Animator"/> parameter.
16801696 /// </summary>
1681- /// <param name="parameterName">name of the parameter</param>
1682- /// <param name="enable">whether to enable or disable synchronizing it</param>
1683- public void ToggleParameterSync ( string parameterName , bool enable )
1697+ /// <param name="parameterName">The <see cref="string"/> name of the parameter.</param>
1698+ /// <param name="isEnabled">Whether to enable or disable the synchronization of the parameter.</param>
1699+ public void EnableParameterSynchronization ( string parameterName , bool isEnabled )
1700+ {
1701+ EnableParameterSynchronization ( Animator . StringToHash ( parameterName ) , isEnabled ) ;
1702+ }
1703+
1704+ /// <summary>
1705+ /// Allows for the enabling or disabling the synchronization of a specific <see cref="UnityEngine.Animator"/> parameter.
1706+ /// </summary>
1707+ /// <param name="parameterNameHash">The hash value (from using <see cref="Animator.StringToHash(string)"/>) of the parameter name.</param>
1708+ /// <param name="isEnabled">Whether to enable or disable the synchronization of the parameter.</param>
1709+ public void EnableParameterSynchronization ( int parameterNameHash , bool isEnabled )
16841710 {
16851711 var serverAuthoritative = OnIsServerAuthoritative ( ) ;
16861712 if ( ! IsSpawned || serverAuthoritative && IsServer || ! serverAuthoritative && IsOwner )
16871713 {
1688- var hash32 = Animator . StringToHash ( parameterName ) ;
1689-
16901714 for ( int i = 0 ; i < m_CachedAnimatorParameters . Length ; i ++ )
16911715 {
16921716 var cachedParameter = m_CachedAnimatorParameters [ i ] ;
1693- if ( cachedParameter . Hash == hash32 )
1717+ if ( cachedParameter . Hash == parameterNameHash )
16941718 {
1695- cachedParameter . Exclude = ! enable ;
1719+ cachedParameter . Exclude = ! isEnabled ;
16961720 m_CachedAnimatorParameters [ i ] = cachedParameter ;
16971721 break ;
16981722 }
0 commit comments