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

Manages network-related operations on the client side, including connection handling, user synchronization, transform and animation updates, and messaging. It is part of EDA architecture of the library. More...

Inheritance diagram for NetworkManager:

Public Member Functions

void ConnectToServer ()
 Initiates a connection to the server.
void DisconnectFromServer ()
 Sends a request to disconnect the client from the server.
void AddUser (GameObject userPrefab)
 Adds a newly spawned user object to the list of online users.
GameObject GetUserByName (string username)
 Retrieves a user GameObject by username.
GameObject GetUserById (int id)
 Retrieves a user GameObject by user ID.
void SpawnUser (int id, string userName, bool isLocal)
 Instantiates and initializes a new user object in the scene.
void DisconnectForeignUser (int id)
 Removes a remote user object from the scene based on its ID.
void SendTextMessage (string message, int mode, int id)
 Sends a text message to either all users or a specific user.
void sendTestMessage ()
 Sends a test broadcast message to the server.
void UpdateTransform (Transform objectTransform)
 Sends the updated transform of the local user to the server.
void SendAnimatorParameter (int type, string name, float value)
 Sends an animation parameter change to the server for synchronization.

Public Attributes

List< GameObject > onlineUsers
 List of all currently active (connected) user GameObjects in the scene.
Transform[] spawnPoints
 Predefined spawn points used for placing newly connected users in the scene.

Static Public Attributes

static NetworkManager networkManager
 Singleton instance of the NetworkManager.

Private Member Functions

void Awake ()
 Initializes the singleton instance of the NetworkManager.
void Start ()
 Subscribes to client events for handling network state changes and data reception, can be easily extended of new events.
void HandleConnected ()
 Callback invoked when the client successfully connects to the server.
void HandleConnectionFailed (string reason)
 Callback invoked when the connection to the server fails.
void HandleMessage (string message)
 Callback for processing a received message from the server.
void HandleDisconnected ()
 Callback invoked when the client is disconnected from the server. Removes all user objects from the scene.
void HandleTransformChange (int id, Vector3 pos, Quaternion rot)
 Applies a received position and rotation update to the corresponding remote user.
void HandleAnimationChange (int id, int type, string name, float value)
 Applies a received animation parameter change to the corresponding remote user.

Private Attributes

ClientSettings ClientSettings
 Configuration settings for the client, including user prefab and other preferences.

Detailed Description

Manages network-related operations on the client side, including connection handling, user synchronization, transform and animation updates, and messaging. It is part of EDA architecture of the library.

Definition at line 10 of file NetworkManager.cs.

Member Function Documentation

◆ AddUser()

void NetworkManager.AddUser ( GameObject userPrefab)

Adds a newly spawned user object to the list of online users.

Definition at line 142 of file NetworkManager.cs.

143 {
144 onlineUsers.Add(userPrefab);
145 }
List< GameObject > onlineUsers
List of all currently active (connected) user GameObjects in the scene.

◆ Awake()

void NetworkManager.Awake ( )
private

Initializes the singleton instance of the NetworkManager.

Definition at line 32 of file NetworkManager.cs.

33 {
34 if (networkManager == null)
35 {
36 networkManager = this;
37 }
38 else if (networkManager != this)
39 {
40 Destroy(this);
41 }
42 }
static NetworkManager networkManager
Singleton instance of the NetworkManager.

◆ ConnectToServer()

void NetworkManager.ConnectToServer ( )

Initiates a connection to the server.

Definition at line 58 of file NetworkManager.cs.

59 {
60 Client.instance.ConnectToServer();
61 }

◆ DisconnectForeignUser()

void NetworkManager.DisconnectForeignUser ( int id)

Removes a remote user object from the scene based on its ID.

Definition at line 186 of file NetworkManager.cs.

187 {
188 foreach (GameObject user in onlineUsers)
189 {
190 if (user.GetComponent<NetworkUser>().GetId() == id)
191 {
192 GameObject UserObject = user;
193 onlineUsers.Remove(user);
194 Destroy(UserObject);
195 break;
196 }
197 }
198 }

◆ DisconnectFromServer()

void NetworkManager.DisconnectFromServer ( )

Sends a request to disconnect the client from the server.

Definition at line 65 of file NetworkManager.cs.

66 {
67 Client.instance.DisconnectRequested();
68 }

◆ GetUserById()

GameObject NetworkManager.GetUserById ( int id)

Retrieves a user GameObject by user ID.

Definition at line 163 of file NetworkManager.cs.

164 {
165 foreach (GameObject user in onlineUsers)
166 {
167 if (user.GetComponent<NetworkUser>().GetId() == id)
168 {
169 return user;
170 }
171 }
172 return null;
173 }

◆ GetUserByName()

GameObject NetworkManager.GetUserByName ( string username)

Retrieves a user GameObject by username.

Definition at line 149 of file NetworkManager.cs.

150 {
151 foreach (GameObject user in onlineUsers)
152 {
153 if (user.GetComponent<NetworkUser>().GetUsername() == username)
154 {
155 return user;
156 }
157 }
158 return null;
159 }

◆ HandleAnimationChange()

void NetworkManager.HandleAnimationChange ( int id,
int type,
string name,
float value )
private

Applies a received animation parameter change to the corresponding remote user.

Definition at line 125 of file NetworkManager.cs.

126 {
127 foreach (GameObject userObject in onlineUsers)
128 {
129 if (userObject.GetComponent<NetworkUser>().GetId() == id)
130 {
131 NetworkAnimator nt = userObject.GetComponent<NetworkAnimator>();
132 if (nt != null && !nt.UserInfo.isLocalUser)
133 {
134 nt.ApplyRemoteParameter(type, name, value);
135 }
136 }
137 }
138 }
void ApplyRemoteParameter(int type, string name, float value)
Applies an animation parameter update received from a remote user.
NetworkUser UserInfo
Information about the network user used to check local or remote status.
bool isLocalUser

◆ HandleConnected()

void NetworkManager.HandleConnected ( )
private

Callback invoked when the client successfully connects to the server.

Definition at line 72 of file NetworkManager.cs.

73 {
74 //
75 }

◆ HandleConnectionFailed()

void NetworkManager.HandleConnectionFailed ( string reason)
private

Callback invoked when the connection to the server fails.

Definition at line 79 of file NetworkManager.cs.

80 {
81 Debug.LogError("NETWORKMANAGER: Connection failed: " + reason);
82 //
83 }

◆ HandleDisconnected()

void NetworkManager.HandleDisconnected ( )
private

Callback invoked when the client is disconnected from the server. Removes all user objects from the scene.

Definition at line 95 of file NetworkManager.cs.

96 {
97 Debug.Log("NETWORKMANAGER: Disconnected from server.");
98 foreach (GameObject user in onlineUsers)
99 {
100 GameObject UserObject = user;
101 onlineUsers.Remove(user);
102 Destroy(UserObject);
103 }
104 }

◆ HandleMessage()

void NetworkManager.HandleMessage ( string message)
private

Callback for processing a received message from the server.

Definition at line 87 of file NetworkManager.cs.

88 {
89 //
90 }

◆ HandleTransformChange()

void NetworkManager.HandleTransformChange ( int id,
Vector3 pos,
Quaternion rot )
private

Applies a received position and rotation update to the corresponding remote user.

Definition at line 108 of file NetworkManager.cs.

109 {
110 foreach(GameObject userObject in onlineUsers)
111 {
112 if(userObject.GetComponent<NetworkUser>().GetId() == id)
113 {
114 NetworkTransform nt = userObject.GetComponent<NetworkTransform>();
115 if (nt != null && !nt.UserInfo.isLocalUser)
116 {
117 nt.ApplyNetworkTransform(pos, rot);
118 }
119 }
120 }
121 }
void ApplyNetworkTransform(Vector3 pos, Quaternion rot)
Applies the received position and rotation from the network to be used for interpolation.
NetworkUser UserInfo
UserInfo class used for checking if the object belongs to the local user or another player.

◆ SendAnimatorParameter()

void NetworkManager.SendAnimatorParameter ( int type,
string name,
float value )

Sends an animation parameter change to the server for synchronization.

Definition at line 228 of file NetworkManager.cs.

229 {
230 Client.instance.AnimationUpdate(type,name,value);
231 }

◆ sendTestMessage()

void NetworkManager.sendTestMessage ( )

Sends a test broadcast message to the server.

Definition at line 214 of file NetworkManager.cs.

215 {
216 SendTextMessage("Test message", 1, 0);
217 }
void SendTextMessage(string message, int mode, int id)
Sends a text message to either all users or a specific user.

◆ SendTextMessage()

void NetworkManager.SendTextMessage ( string message,
int mode,
int id )

Sends a text message to either all users or a specific user.

Definition at line 202 of file NetworkManager.cs.

203 {
204 if(mode == 1) {
205 Client.instance.MessageToAllRequest(message);
206 } else
207 {
208 Client.instance.MessageToUserRequest(message, id);
209 }
210 }

◆ SpawnUser()

void NetworkManager.SpawnUser ( int id,
string userName,
bool isLocal )

Instantiates and initializes a new user object in the scene.

Definition at line 177 of file NetworkManager.cs.

178 {
179 GameObject UserPrefab = Instantiate(ClientSettings.userPrefab[0], spawnPoints[0]);
180 UserPrefab.GetComponent<NetworkUser>().SetUser(id, userName, isLocal);
181 networkManager.AddUser(UserPrefab);
182 }
Transform[] spawnPoints
Predefined spawn points used for placing newly connected users in the scene.
ClientSettings ClientSettings
Configuration settings for the client, including user prefab and other preferences.

◆ Start()

void NetworkManager.Start ( )
private

Subscribes to client events for handling network state changes and data reception, can be easily extended of new events.

Definition at line 46 of file NetworkManager.cs.

47 {
48 Client.instance.OnConnected += HandleConnected;
49 Client.instance.OnConnectionFailed += HandleConnectionFailed;
50 Client.instance.OnDisconnected += HandleDisconnected;
51 Client.instance.OnMessageReceived += HandleMessage;
52 Client.instance.OnTransformReceived += HandleTransformChange;
53 Client.instance.OnAnimationChangeReceived += HandleAnimationChange;
54 }
void HandleAnimationChange(int id, int type, string name, float value)
Applies a received animation parameter change to the corresponding remote user.
void HandleMessage(string message)
Callback for processing a received message from the server.
void HandleDisconnected()
Callback invoked when the client is disconnected from the server. Removes all user objects from the s...
void HandleConnectionFailed(string reason)
Callback invoked when the connection to the server fails.
void HandleTransformChange(int id, Vector3 pos, Quaternion rot)
Applies a received position and rotation update to the corresponding remote user.
void HandleConnected()
Callback invoked when the client successfully connects to the server.

◆ UpdateTransform()

void NetworkManager.UpdateTransform ( Transform objectTransform)

Sends the updated transform of the local user to the server.

Definition at line 221 of file NetworkManager.cs.

222 {
223 Client.instance.UpdateTransform(objectTransform);
224 }

Member Data Documentation

◆ ClientSettings

ClientSettings NetworkManager.ClientSettings
private

Configuration settings for the client, including user prefab and other preferences.

Definition at line 23 of file NetworkManager.cs.

◆ networkManager

NetworkManager NetworkManager.networkManager
static

Singleton instance of the NetworkManager.

Definition at line 15 of file NetworkManager.cs.

◆ onlineUsers

List<GameObject> NetworkManager.onlineUsers

List of all currently active (connected) user GameObjects in the scene.

Definition at line 19 of file NetworkManager.cs.

◆ spawnPoints

Transform [] NetworkManager.spawnPoints

Predefined spawn points used for placing newly connected users in the scene.

Definition at line 27 of file NetworkManager.cs.