Network Library Documentation
Loading...
Searching...
No Matches
NetworkAnimator Class Reference

Synchronizes animator parameters (bools, floats, triggers) over the network. Sends changes from the local user to the server and applies received parameters on remote clients. More...

Inheritance diagram for NetworkAnimator:

Public Member Functions

void Trigger (string triggerName)
 Triggers an animation parameter both locally and across the network if controlled by local user.
void ApplyRemoteParameter (int type, string name, float value)
 Applies an animation parameter update received from a remote user.

Public Attributes

NetworkUser UserInfo
 Information about the network user used to check local or remote status.

Private Member Functions

void Start ()
 Initializes the animator reference and caches all initial animator parameter states.
void Update ()
 Checks for any changed animator parameters if the object is controlled by the local user. Sends updated parameters to the server.
void CacheInitialStates ()
 Stores initial states of all bool and float animator parameters for change tracking.
void CheckAndSendBoolChanges ()
 Detects and sends changed boolean parameters to the server.
void CheckAndSendFloatChanges ()
 Detects and sends changed float parameters to the server.

Private Attributes

Animator animator
 Reference to the Animator component used to control animations.
Dictionary< string, bool > boolStates = new Dictionary<string, bool>()
 Tracks last known values of boolean parameters to detect changes.
Dictionary< string, float > floatStates = new Dictionary<string, float>()
 Tracks last known values of float parameters to detect changes.

Detailed Description

Synchronizes animator parameters (bools, floats, triggers) over the network. Sends changes from the local user to the server and applies received parameters on remote clients.

Definition at line 8 of file NetworkAnimator.cs.

Member Function Documentation

◆ ApplyRemoteParameter()

void NetworkAnimator.ApplyRemoteParameter ( int type,
string name,
float value )

Applies an animation parameter update received from a remote user.

Definition at line 122 of file NetworkAnimator.cs.

123 {
124 switch (type)
125 {
126 case 0: animator.SetBool(name, value > 0.5f); break;
127 case 1: animator.SetFloat(name, value); break;
128 case 2: animator.SetTrigger(name); break;
129 }
130 }
Animator animator
Reference to the Animator component used to control animations.

◆ CacheInitialStates()

void NetworkAnimator.CacheInitialStates ( )
private

Stores initial states of all bool and float animator parameters for change tracking.

Definition at line 50 of file NetworkAnimator.cs.

51 {
52 foreach (AnimatorControllerParameter param in animator.parameters)
53 {
54 switch (param.type) {
55 case AnimatorControllerParameterType.Bool:
56 boolStates[param.name] = animator.GetBool(param.name);
57 break;
58 case AnimatorControllerParameterType.Float:
59 floatStates[param.name] = animator.GetFloat(param.name);
60 break;
61 }
62 }
63 }
Dictionary< string, float > floatStates
Tracks last known values of float parameters to detect changes.
Dictionary< string, bool > boolStates
Tracks last known values of boolean parameters to detect changes.

◆ CheckAndSendBoolChanges()

void NetworkAnimator.CheckAndSendBoolChanges ( )
private

Detects and sends changed boolean parameters to the server.

Definition at line 67 of file NetworkAnimator.cs.

68 {
69 List<string> changedKeys = new List<string>();
70
71 foreach (var entry in boolStates) {
72 bool current = animator.GetBool(entry.Key);
73 if (current != entry.Value) {
74 changedKeys.Add(entry.Key);
75 }
76 }
77
78 foreach (string key in changedKeys) {
79 bool newValue = animator.GetBool(key);
80 boolStates[key] = newValue;
81 NetworkManager.networkManager.SendAnimatorParameter(0, key, newValue ? 1f : 0f);
82 }
83 }

◆ CheckAndSendFloatChanges()

void NetworkAnimator.CheckAndSendFloatChanges ( )
private

Detects and sends changed float parameters to the server.

Definition at line 88 of file NetworkAnimator.cs.

89 {
90 List<string> changedKeys = new List<string>();
91
92 foreach (var kvp in floatStates)
93 {
94 float current = animator.GetFloat(kvp.Key);
95 if (Mathf.Abs(current - kvp.Value) > 0.01f)
96 {
97 changedKeys.Add(kvp.Key);
98 }
99 }
100
101 foreach (string key in changedKeys)
102 {
103 float newValue = animator.GetFloat(key);
104 floatStates[key] = newValue;
105 NetworkManager.networkManager.SendAnimatorParameter(1, key, newValue);
106 }
107 }

◆ Start()

void NetworkAnimator.Start ( )
private

Initializes the animator reference and caches all initial animator parameter states.

Definition at line 29 of file NetworkAnimator.cs.

30 {
31 if (animator == null)
32 animator = GetComponent<Animator>();
33
35 }
void CacheInitialStates()
Stores initial states of all bool and float animator parameters for change tracking.

◆ Trigger()

void NetworkAnimator.Trigger ( string triggerName)

Triggers an animation parameter both locally and across the network if controlled by local user.

Definition at line 111 of file NetworkAnimator.cs.

112 {
113 animator.SetTrigger(triggerName);
114 if (UserInfo.isLocalUser)
115 {
116 NetworkManager.networkManager.SendAnimatorParameter(2, triggerName, 0f);
117 }
118 }
NetworkUser UserInfo
Information about the network user used to check local or remote status.

◆ Update()

void NetworkAnimator.Update ( )
private

Checks for any changed animator parameters if the object is controlled by the local user. Sends updated parameters to the server.

Definition at line 40 of file NetworkAnimator.cs.

41 {
42 if (!UserInfo.isLocalUser) return;
43
46 }
void CheckAndSendFloatChanges()
Detects and sends changed float parameters to the server.
void CheckAndSendBoolChanges()
Detects and sends changed boolean parameters to the server.

Member Data Documentation

◆ animator

Animator NetworkAnimator.animator
private

Reference to the Animator component used to control animations.

Definition at line 17 of file NetworkAnimator.cs.

◆ boolStates

Dictionary<string, bool> NetworkAnimator.boolStates = new Dictionary<string, bool>()
private

Tracks last known values of boolean parameters to detect changes.

Definition at line 21 of file NetworkAnimator.cs.

◆ floatStates

Dictionary<string, float> NetworkAnimator.floatStates = new Dictionary<string, float>()
private

Tracks last known values of float parameters to detect changes.

Definition at line 25 of file NetworkAnimator.cs.

◆ UserInfo

NetworkUser NetworkAnimator.UserInfo

Information about the network user used to check local or remote status.

Definition at line 13 of file NetworkAnimator.cs.