diff --git a/Runtime/HumanoidFree/Scripts/Networking/HumanoidNetworking.cs b/Runtime/HumanoidFree/Scripts/Networking/HumanoidNetworking.cs index 4ab63ab..32f0d39 100644 --- a/Runtime/HumanoidFree/Scripts/Networking/HumanoidNetworking.cs +++ b/Runtime/HumanoidFree/Scripts/Networking/HumanoidNetworking.cs @@ -137,6 +137,7 @@ namespace Passer.Humanoid { public string name; public string avatarPrefabName; public bool physics; + public string possessionLocation = null; #if RemoteAvatarBundles public int remoteAvatarBundleSize; public byte[] remoteAvatarBundle; @@ -155,8 +156,13 @@ namespace Passer.Humanoid { if (humanoid.remoteAvatar == null) avatarPrefabName = humanoid.avatarRig.name; // .Substring(0, humanoid.avatarRig.name.Length - 7); - else + else { + Possessable avatarPossessable = humanoid.remoteAvatar.GetComponent(); + if (avatarPossessable != null) + possessionLocation = avatarPossessable.siteLocation; + avatarPrefabName = humanoid.remoteAvatar.name; + } #if RemoteAvatarBundles /* Get the remoteAvatarBundle from the streaming assets */ @@ -176,6 +182,7 @@ namespace Passer.Humanoid { bw.Write(name); bw.Write(avatarPrefabName); bw.Write(physics); + bw.Write(possessionLocation); #if RemoteAvatarBundles bw.Write(remoteAvatarBundleSize); bw.Write(remoteAvatarBundle); @@ -194,6 +201,7 @@ namespace Passer.Humanoid { name = br.ReadString(); avatarPrefabName = br.ReadString(); physics = br.ReadBoolean(); + possessionLocation = br.ReadString(); #if RemoteAvatarBundles remoteAvatarBundleSize = br.ReadInt32(); remoteAvatarBundle = br.ReadBytes(remoteAvatarBundleSize); @@ -222,12 +230,12 @@ namespace Passer.Humanoid { Receive(networking, data); } - public static void Receive(this IHumanoidNetworking receivingNetworking, InstantiateHumanoid data) { + public static void Receive(this IHumanoidNetworking receivingNetworking, InstantiateHumanoid msg) { - GameObject networkingObj = receivingNetworking.GetGameObject(data.nwId); + GameObject networkingObj = receivingNetworking.GetGameObject(msg.nwId); if (networkingObj == null) { if (receivingNetworking.debug <= DebugLevel.Error) - receivingNetworking.DebugLog("Could not find Networking for Instantiate Humanoid " + data.nwId + "/" + data.humanoidId); + receivingNetworking.DebugLog("Could not find Networking for Instantiate Humanoid " + msg.nwId + "/" + msg.humanoidId); return; } @@ -235,24 +243,24 @@ namespace Passer.Humanoid { if (networking.isLocal && !networking.createLocalRemotes) { if (networking.debug <= DebugLevel.Debug) - networking.DebugLog("Remote Humanoid " + data.nwId + "/" + data.humanoidId + " is local and local remotes are not created"); + networking.DebugLog("Remote Humanoid " + msg.nwId + "/" + msg.humanoidId + " is local and local remotes are not created"); return; } - HumanoidControl remoteHumanoid = FindRemoteHumanoid(networking.humanoids, data.humanoidId); + HumanoidControl remoteHumanoid = FindRemoteHumanoid(networking.humanoids, msg.humanoidId); if (remoteHumanoid != null) { if (networking.debug <= DebugLevel.Warning) - networking.DebugLog("Remote Humanoid " + data.nwId + "/" + data.humanoidId + " already exists"); + networking.DebugLog("Remote Humanoid " + msg.nwId + "/" + msg.humanoidId + " already exists"); // This remote humanoid already exists return; } if (networking.debug <= DebugLevel.Info) - networking.DebugLog("Receive Instantiate Humanoid " + data.nwId + "/" + data.humanoidId); + networking.DebugLog("Receive Instantiate Humanoid " + msg.nwId + "/" + msg.humanoidId); - remoteHumanoid = InstantiateRemoteHumanoid(data.name, Vector3.zero, Quaternion.identity); //, position, rotation); - remoteHumanoid.nwId = data.nwId; - remoteHumanoid.humanoidId = data.humanoidId; + remoteHumanoid = InstantiateRemoteHumanoid(msg.name, Vector3.zero, Quaternion.identity); //, position, rotation); + remoteHumanoid.nwId = msg.nwId; + remoteHumanoid.humanoidId = msg.humanoidId; if (networking.debug <= DebugLevel.Info) networking.DebugLog("Remote Humanoid " + remoteHumanoid.nwId + "/" + remoteHumanoid.humanoidId + " Added"); @@ -266,14 +274,26 @@ namespace Passer.Humanoid { return; } #else - GameObject remoteAvatar = (GameObject)Resources.Load(data.avatarPrefabName); + if (msg.possessionLocation != null) { + Debug.Log("Need to download instantiate avatar possession from " + msg.possessionLocation); + remoteHumanoid.physics = msg.physics; + + HumanoidPlayer.instance.StartCoroutine( + VisitorPossessions.RetrievePossessableAsync(msg.possessionLocation, msg.avatarPrefabName, + retrievedAvatar => InstantiateRetrievedAvatar(networking, remoteHumanoid, retrievedAvatar)) + ); + + return; + } + + GameObject remoteAvatar = (GameObject)Resources.Load(msg.avatarPrefabName); if (remoteAvatar == null) { if (networking.debug <= DebugLevel.Error) - networking.DebugError("Could not load remote avatar " + data.avatarPrefabName + ". Is it located in a Resources folder?"); + networking.DebugError("Could not load remote avatar " + msg.avatarPrefabName + ". Is it located in a Resources folder?"); return; } #endif - remoteHumanoid.physics = data.physics; + remoteHumanoid.physics = msg.physics; remoteHumanoid.LocalChangeAvatar(remoteAvatar); networking.humanoids.Add(remoteHumanoid); @@ -281,6 +301,25 @@ namespace Passer.Humanoid { OnNewRemoteHumanoid(remoteHumanoid); } + private static void InstantiateRetrievedAvatar(IHumanoidNetworking networking, HumanoidControl remoteHumanoid, GameObject retrievedAvatar) { + if (retrievedAvatar == null) { + if (networking.debug <= DebugLevel.Error) + Debug.LogError("Could not retrieve avatar."); + return; + } + + if (networking.debug <= DebugLevel.Info) + networking.DebugLog("Receive Change Possessesable Avatar " + retrievedAvatar); + + remoteHumanoid.LocalChangeAvatar(retrievedAvatar); + + networking.humanoids.Add(remoteHumanoid); + if (OnNewRemoteHumanoid != null) + OnNewRemoteHumanoid(remoteHumanoid); + + } + + public static HumanoidControl FindRemoteHumanoid(List humanoids, ulong nwId, int humanoidId) { foreach (HumanoidControl humanoid in humanoids) { if (humanoid.isRemote && humanoid.nwId == nwId && humanoid.humanoidId == humanoidId)