1
1
#if COM_UNITY_MODULES_ANIMATION
2
2
using System ;
3
3
using System . Collections . Generic ;
4
+ using System . Runtime . CompilerServices ;
4
5
using Unity . Collections ;
5
6
using Unity . Collections . LowLevel . Unsafe ;
6
7
using UnityEngine ;
@@ -72,32 +73,46 @@ private void FlushMessages()
72
73
m_SendTriggerUpdates . Clear ( ) ;
73
74
}
74
75
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
+
75
83
/// <inheritdoc />
76
84
public void NetworkUpdate ( NetworkUpdateStage updateStage )
77
85
{
78
86
switch ( updateStage )
79
87
{
80
88
case NetworkUpdateStage . PreUpdate :
81
89
{
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 )
84
98
{
85
99
// Flush any pending messages
86
100
FlushMessages ( ) ;
87
101
}
88
102
89
103
// Everyone applies any parameters updated
90
- for ( int i = 0 ; i < m_ProcessParameterUpdates . Count ; i ++ )
104
+ if ( m_ProcessParameterUpdates . Count > 0 )
91
105
{
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 ( ) ;
94
112
}
95
- m_ProcessParameterUpdates . Clear ( ) ;
96
- var isServerAuthority = m_NetworkAnimator . IsServerAuthoritative ( ) ;
97
113
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 )
101
116
{
102
117
m_NetworkAnimator . CheckForAnimatorChanges ( ) ;
103
118
}
@@ -1511,6 +1526,10 @@ internal void SendAnimStateRpc(AnimationMessage animationMessage)
1511
1526
ProcessAnimStates ( animationMessage ) ;
1512
1527
}
1513
1528
1529
+ /// <summary>
1530
+ /// Process incoming <see cref="AnimationMessage"/>.
1531
+ /// </summary>
1532
+ /// <param name="animationMessage">The message to process.</param>
1514
1533
private void ProcessAnimStates ( AnimationMessage animationMessage )
1515
1534
{
1516
1535
if ( HasAuthority )
@@ -1530,8 +1549,6 @@ private void ProcessAnimStates(AnimationMessage animationMessage)
1530
1549
}
1531
1550
}
1532
1551
1533
-
1534
-
1535
1552
/// <summary>
1536
1553
/// Server-side trigger state update request
1537
1554
/// The server sets its local state and then forwards the message to the remaining clients
@@ -1675,24 +1692,31 @@ public void ResetTrigger(int hash)
1675
1692
}
1676
1693
1677
1694
/// <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.
1680
1696
/// </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 )
1684
1710
{
1685
1711
var serverAuthoritative = OnIsServerAuthoritative ( ) ;
1686
1712
if ( ! IsSpawned || serverAuthoritative && IsServer || ! serverAuthoritative && IsOwner )
1687
1713
{
1688
- var hash32 = Animator . StringToHash ( parameterName ) ;
1689
-
1690
1714
for ( int i = 0 ; i < m_CachedAnimatorParameters . Length ; i ++ )
1691
1715
{
1692
1716
var cachedParameter = m_CachedAnimatorParameters [ i ] ;
1693
- if ( cachedParameter . Hash == hash32 )
1717
+ if ( cachedParameter . Hash == parameterNameHash )
1694
1718
{
1695
- cachedParameter . Exclude = ! enable ;
1719
+ cachedParameter . Exclude = ! isEnabled ;
1696
1720
m_CachedAnimatorParameters [ i ] = cachedParameter ;
1697
1721
break ;
1698
1722
}
0 commit comments