Instantiating is working

This commit is contained in:
Pascal Serrarens 2024-04-15 16:55:13 +02:00
parent c57476770c
commit 48f13ca4fa
3 changed files with 74 additions and 40 deletions

View File

@ -71,4 +71,4 @@ MonoBehaviour:
_syncTracking: 0
_debug: 1
_sendRate: 25
_createLocalRemotes: 1
_createLocalRemotes: 0

View File

@ -40,7 +40,11 @@ namespace Passer.Humanoid {
// end temporary dummies
public ulong nwId {
get { return identity.NetworkObjectId; }
get {
if (identity == null)
identity = GetComponent<Unity.Netcode.NetworkObject>();
return identity.NetworkObjectId;
}
}
[SerializeField]
@ -70,10 +74,10 @@ namespace Passer.Humanoid {
Unity.Netcode.NetworkObject nwObject = GetNetworkObject(objIdentity);
if (nwObject == null) {
// Player Objects
nwObject = NetworkManager.Singleton.ConnectedClients[objIdentity].PlayerObject;
if (nwObject != null)
return nwObject.gameObject;
else
//nwObject = NetworkManager.Singleton.ConnectedClients[objIdentity].PlayerObject;
//if (nwObject != null)
// return nwObject.gameObject;
//else
return null;
}
GameObject gameObject = nwObject.gameObject;
@ -94,21 +98,47 @@ namespace Passer.Humanoid {
#region Init
public void Start() {
public void Awake() {
if (identity == null)
identity = GetComponent<Unity.Netcode.NetworkObject>();
humanoids = HumanoidNetworking.FindLocalHumanoids();
if (debug <= HumanoidNetworking.DebugLevel.Info)
DebugLog("Found " + humanoids.Count + " Humanoids");
DebugLog($"Found {humanoids.Count} Humanoids");
//if (identity.IsLocalPlayer) {
// isLocal = true;
// name = "HumanoidPlayer(Local)";
// humanoids = HumanoidNetworking.FindLocalHumanoids();
// if (debug <= HumanoidNetworking.DebugLevel.Info)
// DebugLog($"Found {humanoids.Count} Humanoids");
//}
//DebugLog($"awakened {this}");
}
public void Start() {
if (identity.IsLocalPlayer == false)
return;
isLocal = true;
name = "HumanoidPlayer(Local)";
//humanoids = HumanoidNetworking.FindLocalHumanoids();
//if (debug <= HumanoidNetworking.DebugLevel.Info)
// DebugLog($"Found {humanoids.Count} Humanoids");
for (int i = 0; i < humanoids.Count; i++) {
HumanoidControl humanoid = humanoids[i];
if (humanoid.isRemote || createLocalRemotes == false)
if (humanoid.isRemote) // || createLocalRemotes == false)
continue;
humanoid.nwId = identity.NetworkObjectId;
humanoid.humanoidNetworking = this;
if (debug <= HumanoidNetworking.DebugLevel.Info)
Debug.Log(humanoid.nwId + ": Send Start Humanoid " + humanoid.humanoidId);
((IHumanoidNetworking)this).InstantiateHumanoid(humanoid);
}
}
@ -144,39 +174,39 @@ namespace Passer.Humanoid {
#endregion
#region Start
//#region Start
public virtual void OnStartLocalPlayer() {
isLocal = true;
name = "HumanoidPlayer(Local)";
//public virtual void OnStartLocalPlayer() {
// isLocal = true;
// name = "HumanoidPlayer(Local)";
humanoids = HumanoidNetworking.FindLocalHumanoids();
if (debug <= HumanoidNetworking.DebugLevel.Info)
Debug.Log((int)identity.NetworkObjectId + ": Found " + humanoids.Count + " Humanoids");
// humanoids = HumanoidNetworking.FindLocalHumanoids();
// if (debug <= HumanoidNetworking.DebugLevel.Info)
// Debug.Log((int)identity.NetworkObjectId + ": Found " + humanoids.Count + " Humanoids");
for (int i = 0; i < humanoids.Count; i++) {
HumanoidControl humanoid = humanoids[i];
if (humanoid.isRemote)
continue;
// for (int i = 0; i < humanoids.Count; i++) {
// HumanoidControl humanoid = humanoids[i];
// if (humanoid.isRemote)
// continue;
humanoid.nwId = identity.NetworkObjectId;
humanoid.humanoidNetworking = this;
// humanoid.nwId = identity.NetworkObjectId;
// humanoid.humanoidNetworking = this;
if (debug <= HumanoidNetworking.DebugLevel.Info)
Debug.Log(humanoid.nwId + ": Send Start Humanoid " + humanoid.humanoidId);
// if (debug <= HumanoidNetworking.DebugLevel.Info)
// Debug.Log(humanoid.nwId + ": Send Start Humanoid " + humanoid.humanoidId);
((IHumanoidNetworking)this).InstantiateHumanoid(humanoid);
}
foreach (HumanoidControl humanoid in humanoids)
DetectNetworkObjects(humanoid);
// ((IHumanoidNetworking)this).InstantiateHumanoid(humanoid);
// }
// //foreach (HumanoidControl humanoid in humanoids)
// // DetectNetworkObjects(humanoid);
NetworkingSpawner spawner = FindObjectOfType<NetworkingSpawner>();
if (spawner != null)
spawner.OnNetworkingStarted();
}
// //NetworkingSpawner spawner = FindObjectOfType<NetworkingSpawner>();
// //if (spawner != null)
// // spawner.OnNetworkingStarted();
//}
#endregion
//#endregion
#region Update
@ -298,7 +328,7 @@ namespace Passer.Humanoid {
}
[Rpc(SendTo.NotServer)] // @ remote client
[Rpc(SendTo.NotOwner)] // @ remote client
protected virtual void ReceiveInitiateHumanoidRpc(byte[] data) {
HumanoidNetworking.InstantiateHumanoid instantiateHumanoid = new HumanoidNetworking.InstantiateHumanoid(data);
if (debug <= HumanoidNetworking.DebugLevel.Info)
@ -595,9 +625,13 @@ namespace Passer.Humanoid {
#region Debug
public void DebugLog(string message) {
if (identity == null)
Debug.Log("??: " + message);
else
Debug.Log(identity.NetworkObjectId + ": " + message);
}
public void DebugWarning(string message) {
Debug.LogWarning(identity.NetworkObjectId + ": " + message);
}

View File

@ -35,12 +35,12 @@ namespace Passer {
void INetworkingStarter.StartClient(NetworkingStarter nwStarter) {
Debug.Log("start Netcode Client");
//NetworkManager networkManager = nwStarter.GetComponent<NetworkManager>();
//NetworkClient nwClient =
NetworkManager.Singleton.StartClient();
NetworkManager.Singleton.GetComponent<UnityTransport>().SetConnectionData(
nwStarter.serverIpAddress,
nwStarter.serverPort
);
//NetworkClient nwClient =
NetworkManager.Singleton.StartClient();
}
public static void StartClient(string serverIpAddress, ushort serverPort) {