1
- using System . IO ;
1
+ using System . Collections . Generic ;
2
+ using System . IO ;
2
3
using UnityEngine ;
3
4
4
5
namespace MLAPI . MonoBehaviours . Prototyping
5
6
{
6
7
public class NetworkedAnimator : NetworkedBehaviour
7
8
{
9
+ public bool EnableProximity = false ;
10
+ public float ProximityRange = 50f ;
11
+
8
12
[ SerializeField ]
9
13
private Animator _animator ;
10
14
[ SerializeField ]
11
15
private uint parameterSendBits ;
12
16
[ SerializeField ]
13
- private float sendRate = 0.1f ;
17
+ private readonly float sendRate = 0.1f ;
14
18
private AnimatorControllerParameter [ ] animatorParameters ;
15
19
16
20
private int animationHash ;
@@ -98,7 +102,18 @@ private void FixedUpdate()
98
102
}
99
103
if ( isServer )
100
104
{
101
- SendToNonLocalClientsTarget ( "MLAPI_HandleAnimationMessage" , "MLAPI_ANIMATION_UPDATE" , stream . ToArray ( ) ) ;
105
+ if ( EnableProximity )
106
+ {
107
+ List < int > clientsInProximity = new List < int > ( ) ;
108
+ foreach ( KeyValuePair < int , NetworkedClient > client in NetworkingManager . singleton . connectedClients )
109
+ {
110
+ if ( Vector3 . Distance ( transform . position , client . Value . PlayerObject . transform . position ) <= ProximityRange )
111
+ clientsInProximity . Add ( client . Key ) ;
112
+ }
113
+ SendToClientsTarget ( clientsInProximity , "MLAPI_HandleAnimationMessage" , "MLAPI_ANIMATION_UPDATE" , stream . ToArray ( ) ) ;
114
+ }
115
+ else
116
+ SendToNonLocalClientsTarget ( "MLAPI_HandleAnimationMessage" , "MLAPI_ANIMATION_UPDATE" , stream . ToArray ( ) ) ;
102
117
}
103
118
else
104
119
{
@@ -156,7 +171,18 @@ private void CheckSendRate()
156
171
}
157
172
if ( isServer )
158
173
{
159
- SendToNonLocalClientsTarget ( "MLAPI_HandleAnimationParameterMessage" , "MLAPI_ANIMATION_UPDATE" , stream . ToArray ( ) ) ;
174
+ if ( EnableProximity )
175
+ {
176
+ List < int > clientsInProximity = new List < int > ( ) ;
177
+ foreach ( KeyValuePair < int , NetworkedClient > client in NetworkingManager . singleton . connectedClients )
178
+ {
179
+ if ( Vector3 . Distance ( transform . position , client . Value . PlayerObject . transform . position ) <= ProximityRange )
180
+ clientsInProximity . Add ( client . Key ) ;
181
+ }
182
+ SendToClientsTarget ( clientsInProximity , "MLAPI_HandleAnimationParameterMessage" , "MLAPI_ANIMATION_UPDATE" , stream . ToArray ( ) ) ;
183
+ }
184
+ else
185
+ SendToNonLocalClientsTarget ( "MLAPI_HandleAnimationParameterMessage" , "MLAPI_ANIMATION_UPDATE" , stream . ToArray ( ) ) ;
160
186
}
161
187
else
162
188
{
@@ -196,7 +222,18 @@ private void HandleAnimMsg(int clientId, byte[] data)
196
222
197
223
if ( isServer )
198
224
{
199
- SendToNonLocalClientsTarget ( "MLAPI_HandleAnimationMessage" , "MLAPI_ANIMATION_UPDATE" , data ) ;
225
+ if ( EnableProximity )
226
+ {
227
+ List < int > clientsInProximity = new List < int > ( ) ;
228
+ foreach ( KeyValuePair < int , NetworkedClient > client in NetworkingManager . singleton . connectedClients )
229
+ {
230
+ if ( Vector3 . Distance ( transform . position , client . Value . PlayerObject . transform . position ) <= ProximityRange )
231
+ clientsInProximity . Add ( client . Key ) ;
232
+ }
233
+ SendToClientsTarget ( clientsInProximity , "MLAPI_HandleAnimationMessage" , "MLAPI_ANIMATION_UPDATE" , data ) ;
234
+ }
235
+ else
236
+ SendToNonLocalClientsTarget ( "MLAPI_HandleAnimationMessage" , "MLAPI_ANIMATION_UPDATE" , data ) ;
200
237
}
201
238
using ( MemoryStream stream = new MemoryStream ( data ) )
202
239
{
@@ -217,7 +254,18 @@ private void HandleAnimParamsMsg(int clientId, byte[] data)
217
254
{
218
255
if ( isServer )
219
256
{
220
- SendToNonLocalClientsTarget ( "MLAPI_HandleAnimationParameterMessage" , "MLAPI_ANIMATION_UPDATE" , data ) ;
257
+ if ( EnableProximity )
258
+ {
259
+ List < int > clientsInProximity = new List < int > ( ) ;
260
+ foreach ( KeyValuePair < int , NetworkedClient > client in NetworkingManager . singleton . connectedClients )
261
+ {
262
+ if ( Vector3 . Distance ( transform . position , client . Value . PlayerObject . transform . position ) <= ProximityRange )
263
+ clientsInProximity . Add ( client . Key ) ;
264
+ }
265
+ SendToClientsTarget ( clientsInProximity , "MLAPI_HandleAnimationParameterMessage" , "MLAPI_ANIMATION_UPDATE" , data ) ;
266
+ }
267
+ else
268
+ SendToNonLocalClientsTarget ( "MLAPI_HandleAnimationParameterMessage" , "MLAPI_ANIMATION_UPDATE" , data ) ;
221
269
}
222
270
using ( MemoryStream stream = new MemoryStream ( data ) )
223
271
{
@@ -232,7 +280,18 @@ private void HandleAnimTriggerMsg(int clientId, byte[] data)
232
280
{
233
281
if ( isServer )
234
282
{
235
- SendToNonLocalClientsTarget ( "MLAPI_HandleAnimationTriggerMessage" , "MLAPI_ANIMATION_UPDATE" , data ) ;
283
+ if ( EnableProximity )
284
+ {
285
+ List < int > clientsInProximity = new List < int > ( ) ;
286
+ foreach ( KeyValuePair < int , NetworkedClient > client in NetworkingManager . singleton . connectedClients )
287
+ {
288
+ if ( Vector3 . Distance ( transform . position , client . Value . PlayerObject . transform . position ) <= ProximityRange )
289
+ clientsInProximity . Add ( client . Key ) ;
290
+ }
291
+ SendToClientsTarget ( clientsInProximity , "MLAPI_HandleAnimationTriggerMessage" , "MLAPI_ANIMATION_UPDATE" , data ) ;
292
+ }
293
+ else
294
+ SendToNonLocalClientsTarget ( "MLAPI_HandleAnimationTriggerMessage" , "MLAPI_ANIMATION_UPDATE" , data ) ;
236
295
}
237
296
using ( MemoryStream stream = new MemoryStream ( data ) )
238
297
{
@@ -331,7 +390,18 @@ public void SetTrigger(int hash)
331
390
}
332
391
if ( isServer )
333
392
{
334
- SendToNonLocalClientsTarget ( "MLAPI_HandleAnimationTriggerMessage" , "MLAPI_ANIMATION_UPDATE" , stream . ToArray ( ) ) ;
393
+ if ( EnableProximity )
394
+ {
395
+ List < int > clientsInProximity = new List < int > ( ) ;
396
+ foreach ( KeyValuePair < int , NetworkedClient > client in NetworkingManager . singleton . connectedClients )
397
+ {
398
+ if ( Vector3 . Distance ( transform . position , client . Value . PlayerObject . transform . position ) <= ProximityRange )
399
+ clientsInProximity . Add ( client . Key ) ;
400
+ }
401
+ SendToClientsTarget ( clientsInProximity , "MLAPI_HandleAnimationTriggerMessage" , "MLAPI_ANIMATION_UPDATE" , stream . ToArray ( ) ) ;
402
+ }
403
+ else
404
+ SendToNonLocalClientsTarget ( "MLAPI_HandleAnimationTriggerMessage" , "MLAPI_ANIMATION_UPDATE" , stream . ToArray ( ) ) ;
335
405
}
336
406
else
337
407
{
0 commit comments