Skip to content

Commit d67d698

Browse files
committed
Changed GameObject internal usage to NetworkedObject
1 parent 1c921a4 commit d67d698

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

MLAPI/Data/NetworkPool.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace MLAPI.Data
66
{
77
internal class NetworkPool
88
{
9-
internal GameObject[] objects;
9+
internal NetworkedObject[] objects;
1010
internal ushort poolId;
1111

1212
internal NetworkPool(int prefabId, uint size, ushort poolIndex)
1313
{
14-
objects = new GameObject[size];
14+
objects = new NetworkedObject[size];
1515
poolId = poolIndex;
1616
for (int i = 0; i < size; i++)
1717
{
@@ -24,13 +24,13 @@ internal NetworkPool(int prefabId, uint size, ushort poolIndex)
2424
}
2525
}
2626

27-
internal GameObject SpawnObject(Vector3 position, Quaternion rotation)
27+
internal NetworkedObject SpawnObject(Vector3 position, Quaternion rotation)
2828
{
2929
for (int i = 0; i < objects.Length; i++)
3030
{
31-
if (objects[i].activeInHierarchy)
31+
if (objects[i].gameObject.activeInHierarchy)
3232
{
33-
GameObject go = objects[i];
33+
GameObject go = objects[i].gameObject;
3434
go.transform.position = position;
3535
go.transform.rotation = rotation;
3636
go.SetActive(true);

MLAPI/Data/NetworkedClient.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using MLAPI.MonoBehaviours.Core;
22
using System.Collections.Generic;
3-
using UnityEngine;
43

54
namespace MLAPI.Data
65
{
@@ -16,7 +15,7 @@ public class NetworkedClient
1615
/// <summary>
1716
/// The PlayerObject of the Client
1817
/// </summary>
19-
public GameObject PlayerObject;
18+
public NetworkedObject PlayerObject;
2019
/// <summary>
2120
/// The NetworkedObject's owned by this Client
2221
/// </summary>

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,9 +1161,8 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
11611161
{
11621162
uint networkId = SpawnManager.GetNetworkObjectId();
11631163
prefabId = prefabId == -1 ? NetworkConfig.NetworkPrefabIds[NetworkConfig.PlayerPrefabName] : prefabId;
1164-
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, clientId, true, position, rotation, null, false, false);
1165-
netObject = go.GetComponent<NetworkedObject>();
1166-
connectedClients[clientId].PlayerObject = go;
1164+
netObject = SpawnManager.CreateSpawnedObject(prefabId, networkId, clientId, true, position, rotation, null, false, false);
1165+
connectedClients[clientId].PlayerObject = netObject;
11671166
}
11681167

11691168
int amountOfObjectsToSend = SpawnManager.spawnedObjects.Values.Count;

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ internal static void HandleConnectionApproved(uint clientId, BitReader reader, i
105105
float yRot = reader.ReadFloat();
106106
float zRot = reader.ReadFloat();
107107

108-
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject,
108+
NetworkedObject netObject = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject,
109109
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), reader, visible, false);
110-
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
111-
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
112-
go.SetActive(isActive);
110+
netObject.SetLocalVisibility(visible);
111+
netObject.sceneObject = sceneObject;
112+
netObject.gameObject.SetActive(isActive);
113113
}
114114
}
115115

@@ -149,11 +149,11 @@ internal static void HandleAddObject(uint clientId, BitReader reader, int channe
149149
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
150150
netManager.connectedClientsList.Add(netManager.connectedClients[ownerId]);
151151
}
152-
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject,
152+
NetworkedObject netObject = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject,
153153
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), reader, visible, hasPayload);
154154

155-
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
156-
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
155+
netObject.SetLocalVisibility(visible);
156+
netObject.sceneObject = sceneObject;
157157

158158
}
159159
else
@@ -273,9 +273,10 @@ internal static void HandleAddObjects(uint clientId, BitReader reader, int chann
273273
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
274274
netManager.connectedClientsList.Add(netManager.connectedClients[ownerId]);
275275
}
276-
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), reader, visible, false);
277-
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
278-
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
276+
NetworkedObject netObject = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject,
277+
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), reader, visible, false);
278+
netObject.SetLocalVisibility(visible);
279+
netObject.sceneObject = sceneObject;
279280

280281
}
281282
}

MLAPI/NetworkingManagerComponents/Core/NetworkPoolManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ public static void DestroyPool(string poolName)
5858
/// <param name="position">The position to spawn the object at</param>
5959
/// <param name="rotation">The rotation to spawn the object at</param>
6060
/// <returns></returns>
61-
public static GameObject SpawnPoolObject(string poolName, Vector3 position, Quaternion rotation)
61+
public static NetworkedObject SpawnPoolObject(string poolName, Vector3 position, Quaternion rotation)
6262
{
6363
if (!NetworkingManager.singleton.isServer)
6464
{
6565
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Object spawning can only occur on server");
6666
return null;
6767
}
68-
GameObject go = Pools[PoolNamesToIndexes[poolName]].SpawnObject(position, rotation);
68+
NetworkedObject netObject = Pools[PoolNamesToIndexes[poolName]].SpawnObject(position, rotation);
6969
using (BitWriter writer = BitWriter.Get())
7070
{
71-
writer.WriteUInt(go.GetComponent<NetworkedObject>().NetworkId);
71+
writer.WriteUInt(netObject.NetworkId);
7272

7373
writer.WriteFloat(position.x);
7474
writer.WriteFloat(position.y);
@@ -80,7 +80,7 @@ public static GameObject SpawnPoolObject(string poolName, Vector3 position, Quat
8080

8181
InternalMessageHandler.Send("MLAPI_SPAWN_POOL_OBJECT", "MLAPI_INTERNAL", writer, null);
8282
}
83-
return go;
83+
return netObject;
8484
}
8585

8686
/// <summary>

MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ internal static void FlushSceneObjects()
152152
}
153153
}
154154

155-
internal static GameObject CreateSpawnedObject(int networkedPrefabId, uint networkId, uint owner, bool playerObject, Vector3 position, Quaternion rotation, BitReader reader, bool readSyncedVar, bool readPayload)
155+
internal static NetworkedObject CreateSpawnedObject(int networkedPrefabId, uint networkId, uint owner, bool playerObject, Vector3 position, Quaternion rotation, BitReader reader, bool readSyncedVar, bool readPayload)
156156
{
157157
if (!netManager.NetworkConfig.NetworkPrefabNames.ContainsKey(networkedPrefabId))
158158
{
@@ -184,7 +184,7 @@ internal static GameObject CreateSpawnedObject(int networkedPrefabId, uint netwo
184184
netObject.transform.rotation = rotation;
185185
spawnedObjects.Add(netObject.NetworkId, netObject);
186186
netObject.InvokeBehaviourNetworkSpawn(reader);
187-
return go;
187+
return netObject;
188188
}
189189

190190
internal static void UnSpawnObject(NetworkedObject netObject)
@@ -242,7 +242,7 @@ internal static void SpawnPlayerObject(NetworkedObject netObject, uint clientId,
242242
netObject._isSpawned = true;
243243
netObject.sceneObject = false;
244244
netObject._isPlayerObject = true;
245-
netManager.connectedClients[clientId].PlayerObject = netObject.gameObject;
245+
netManager.connectedClients[clientId].PlayerObject = netObject;
246246

247247
if (payload == null) netObject.InvokeBehaviourNetworkSpawn(null);
248248
else using (BitReader payloadReader = BitReader.Get(payload.Finalize())) netObject.InvokeBehaviourNetworkSpawn(payloadReader);
@@ -353,7 +353,6 @@ internal static void OnDestroyObject(uint networkId, bool destroyGameObject)
353353
//Someone owns it.
354354
NetworkingManager.singleton.connectedClients[spawnedObjects[networkId].OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == networkId);
355355
}
356-
GameObject go = spawnedObjects[networkId].gameObject;
357356
spawnedObjects[networkId]._isSpawned = false;
358357

359358
if (netManager != null && netManager.isServer)
@@ -369,6 +368,8 @@ internal static void OnDestroyObject(uint networkId, bool destroyGameObject)
369368
}
370369
}
371370
}
371+
372+
GameObject go = spawnedObjects[networkId].gameObject;
372373
if (destroyGameObject && go != null)
373374
MonoBehaviour.Destroy(go);
374375
spawnedObjects.Remove(networkId);

0 commit comments

Comments
 (0)