From 21b5942106a585270841f7e6ff10f0042e455876 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Albin=20Cor=C3=A9n?= <2108U9@gmail.com>
Date: Fri, 13 Apr 2018 17:08:04 +0100
Subject: [PATCH] Added spawn position and rotation to ConnectionApproval
---
.../MonoBehaviours/Core/NetworkingManager.cs | 21 ++++++++++---------
.../Core/SpawnManager.cs | 4 ++--
2 files changed, 13 insertions(+), 12 deletions(-)
diff --git a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs
index 16a1753..bcdf548 100644
--- a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs
+++ b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs
@@ -136,7 +136,7 @@ namespace MLAPI.MonoBehaviours.Core
///
/// The callback to invoke during connection approval
///
- public Action> ConnectionApprovalCallback = null;
+ public Action> ConnectionApprovalCallback = null;
///
/// The current NetworkingConfiguration
///
@@ -586,13 +586,14 @@ namespace MLAPI.MonoBehaviours.Core
///
/// Starts a Host
///
- public void StartHost()
+ public void StartHost(Vector3? pos = null, Quaternion? rot = null)
{
if (isServer || isClient)
{
Debug.LogWarning("MLAPI: Cannot start host while an instance is already running");
return;
}
+
ConnectionConfig cConfig = Init(true);
if (NetworkConfig.ConnectionApproval)
{
@@ -622,7 +623,7 @@ namespace MLAPI.MonoBehaviours.Core
if(NetworkConfig.HandleObjectSpawning)
{
- SpawnManager.SpawnPlayerObject(netId.GetClientId(), 0);
+ SpawnManager.SpawnPlayerObject(netId.GetClientId(), 0, pos.GetValueOrDefault(), rot.GetValueOrDefault());
}
if (OnServerStarted != null)
@@ -952,7 +953,7 @@ namespace MLAPI.MonoBehaviours.Core
}
else
{
- HandleApproval(clientId, true);
+ HandleApproval(clientId, true, Vector3.zero, Quaternion.identity);
}
}
}
@@ -1035,7 +1036,7 @@ namespace MLAPI.MonoBehaviours.Core
if (isPlayerObject)
{
- SpawnManager.SpawnPlayerObject(ownerId, networkId);
+ SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
}
else
{
@@ -1086,7 +1087,7 @@ namespace MLAPI.MonoBehaviours.Core
if (isPlayerObject)
{
connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
- SpawnManager.SpawnPlayerObject(ownerId, networkId);
+ SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
}
else
{
@@ -1161,7 +1162,7 @@ namespace MLAPI.MonoBehaviours.Core
float yRot = messageReader.ReadSingle();
float zRot = messageReader.ReadSingle();
SpawnManager.spawnedObjects[netId].transform.position = new Vector3(xPos, yPos, zPos);
- SpawnManager.spawnedObjects[netId].transform.rotation = Quaternion.Euler(new Vector3(xRot, yRot, zRot));
+ SpawnManager.spawnedObjects[netId].transform.rotation = Quaternion.Euler(xRot, yRot, zRot);
SpawnManager.spawnedObjects[netId].gameObject.SetActive(true);
}
}
@@ -1336,7 +1337,7 @@ namespace MLAPI.MonoBehaviours.Core
if (isPlayerObject)
{
connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
- SpawnManager.SpawnPlayerObject(ownerId, networkId);
+ SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
}
else
{
@@ -1778,7 +1779,7 @@ namespace MLAPI.MonoBehaviours.Core
}
}
- private void HandleApproval(uint clientId, bool approved)
+ private void HandleApproval(uint clientId, bool approved, Vector3 position, Quaternion rotation)
{
if(approved)
{
@@ -1819,7 +1820,7 @@ namespace MLAPI.MonoBehaviours.Core
if(NetworkConfig.HandleObjectSpawning)
{
uint networkId = SpawnManager.GetNetworkObjectId();
- GameObject go = SpawnManager.SpawnPlayerObject(clientId, networkId);
+ GameObject go = SpawnManager.SpawnPlayerObject(clientId, networkId, position, rotation);
connectedClients[clientId].PlayerObject = go;
}
int sizeOfStream = 17 + ((connectedClients.Count - 1) * 4);
diff --git a/MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs b/MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs
index 81ac495..7b9e06b 100644
--- a/MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs
+++ b/MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs
@@ -217,14 +217,14 @@ namespace MLAPI.NetworkingManagerComponents.Core
}
}
- internal static GameObject SpawnPlayerObject(uint clientId, uint networkId)
+ internal static GameObject SpawnPlayerObject(uint clientId, uint networkId, Vector3 position, Quaternion rotation)
{
if (string.IsNullOrEmpty(netManager.NetworkConfig.PlayerPrefabName) || !netManager.NetworkConfig.NetworkPrefabIds.ContainsKey(netManager.NetworkConfig.PlayerPrefabName))
{
Debug.LogWarning("MLAPI: There is no player prefab in the NetworkConfig, or it's not registered at as a spawnable prefab");
return null;
}
- GameObject go = MonoBehaviour.Instantiate(netManager.NetworkConfig.NetworkedPrefabs[netManager.NetworkConfig.NetworkPrefabIds[netManager.NetworkConfig.PlayerPrefabName]].prefab);
+ GameObject go = MonoBehaviour.Instantiate(netManager.NetworkConfig.NetworkedPrefabs[netManager.NetworkConfig.NetworkPrefabIds[netManager.NetworkConfig.PlayerPrefabName]].prefab, position, rotation);
NetworkedObject netObject = go.GetComponent();
if (netObject == null)
{