Fixed an issue where old scene networked objects would not get destroyed & added position sync to spawn

This commit is contained in:
Albin Corén 2018-04-02 06:52:17 +02:00
parent 20310d44dd
commit c325dec973
2 changed files with 53 additions and 4 deletions

View File

@ -752,6 +752,7 @@ namespace MLAPI.MonoBehaviours.Core
}
if(NetworkConfig.HandleObjectSpawning)
{
SpawnManager.DestroyUnspawnedObjects();
int objectCount = messageReader.ReadInt32();
for (int i = 0; i < objectCount; i++)
{
@ -760,13 +761,24 @@ namespace MLAPI.MonoBehaviours.Core
int ownerId = messageReader.ReadInt32();
int prefabId = messageReader.ReadInt32();
bool isActive = messageReader.ReadBoolean();
if(isPlayerObject)
float xPos = messageReader.ReadSingle();
float yPos = messageReader.ReadSingle();
float zPos = messageReader.ReadSingle();
float xRot = messageReader.ReadSingle();
float yRot = messageReader.ReadSingle();
float zRot = messageReader.ReadSingle();
if (isPlayerObject)
{
SpawnManager.SpawnPlayerObject(ownerId, networkId);
}
else
{
GameObject go = SpawnManager.SpawnObject(prefabId, networkId, ownerId);
GameObject go = SpawnManager.SpawnObject(prefabId, networkId, ownerId,
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
go.SetActive(isActive);
}
}
@ -798,6 +810,14 @@ namespace MLAPI.MonoBehaviours.Core
int ownerId = messageReader.ReadInt32();
int prefabId = messageReader.ReadInt32();
float xPos = messageReader.ReadSingle();
float yPos = messageReader.ReadSingle();
float zPos = messageReader.ReadSingle();
float xRot = messageReader.ReadSingle();
float yRot = messageReader.ReadSingle();
float zRot = messageReader.ReadSingle();
if (isPlayerObject)
{
connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
@ -805,7 +825,8 @@ namespace MLAPI.MonoBehaviours.Core
}
else
{
SpawnManager.SpawnObject(prefabId, networkId, ownerId);
SpawnManager.SpawnObject(prefabId, networkId, ownerId,
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
}
}
else
@ -1504,6 +1525,14 @@ namespace MLAPI.MonoBehaviours.Core
writer.Write(pair.Value.OwnerClientId);
writer.Write(pair.Value.SpawnablePrefabIndex);
writer.Write(pair.Value.gameObject.activeInHierarchy);
writer.Write(pair.Value.transform.position.x);
writer.Write(pair.Value.transform.position.y);
writer.Write(pair.Value.transform.position.z);
writer.Write(pair.Value.transform.rotation.x);
writer.Write(pair.Value.transform.rotation.y);
writer.Write(pair.Value.transform.rotation.z);
}
}
}

View File

@ -65,7 +65,17 @@ namespace MLAPI.NetworkingManagerComponents.Core
}
}
internal static GameObject SpawnObject(int spawnablePrefabIndex, uint networkId, int ownerId)
internal static void DestroyUnspawnedObjects()
{
NetworkedObject[] netObjects = MonoBehaviour.FindObjectsOfType<NetworkedObject>();
for (int i = 0; i < netObjects.Length; i++)
{
if (!netObjects[i].isSpawned)
MonoBehaviour.Destroy(netObjects[i].gameObject);
}
}
internal static GameObject SpawnObject(int spawnablePrefabIndex, uint networkId, int ownerId, Vector3 position, Quaternion rotation)
{
GameObject go = MonoBehaviour.Instantiate(netManager.SpawnablePrefabs[spawnablePrefabIndex]);
NetworkedObject netObject = go.GetComponent<NetworkedObject>();
@ -84,6 +94,8 @@ namespace MLAPI.NetworkingManagerComponents.Core
netObject.networkId = networkId;
}
netObject.ownerClientId = ownerId;
netObject.transform.position = position;
netObject.transform.rotation = rotation;
spawnedObjects.Add(netObject.NetworkId, netObject);
netObject.InvokeBehaviourNetworkSpawn();
@ -193,6 +205,14 @@ namespace MLAPI.NetworkingManagerComponents.Core
writer.Write(netObject.NetworkId);
writer.Write(netObject.OwnerClientId);
writer.Write(netObject.SpawnablePrefabIndex);
writer.Write(netObject.transform.position.x);
writer.Write(netObject.transform.position.y);
writer.Write(netObject.transform.position.z);
writer.Write(netObject.transform.rotation.x);
writer.Write(netObject.transform.rotation.y);
writer.Write(netObject.transform.rotation.z);
}
netManager.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer());