diff --git a/Editor/HumanoidControl/Configuration_Editor.cs b/Editor/HumanoidControl/Configuration_Editor.cs index f77b9de..0ef7116 100644 --- a/Editor/HumanoidControl/Configuration_Editor.cs +++ b/Editor/HumanoidControl/Configuration_Editor.cs @@ -953,6 +953,7 @@ namespace Passer.Humanoid { } private static void CheckExtensionNetworking(Configuration configuration) { + CheckExtensionNetcode(); if (isPhotonPun2Available && isPhotonPun2SupportAvailable) { GlobalDefine("hPHOTON2"); GlobalUndefine("hPHOTON1"); @@ -1022,6 +1023,13 @@ namespace Passer.Humanoid { GlobalUndefine("hPUNVOICE2"); } + private static void CheckExtensionNetcode() { + if (isNetcodeAvailable) + GlobalDefine("hNETCODE"); + else + GlobalUndefine("hNETCODE"); + } + private static void CheckFaceTracking(Configuration configuration) { if (DoesTypeExist("Passer.Humanoid.FaceTarget")) { GlobalDefine("hFACE"); @@ -1261,6 +1269,11 @@ namespace Passer.Humanoid { } } + private static bool isNetcodeAvailable { + get { + return DoesTypeExist("Unity.Netcode.NetworkObject"); + } + } #endregion SDKs diff --git a/Runtime/HumanoidControl/Scripts/Networking/Netcode.meta b/Runtime/HumanoidControl/Scripts/Networking/Netcode.meta new file mode 100644 index 0000000..ab88be0 --- /dev/null +++ b/Runtime/HumanoidControl/Scripts/Networking/Netcode.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ed3a111ced8ca6c428e6f00eef61d46d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/HumanoidControl/Scripts/Networking/Netcode/Editor.meta b/Runtime/HumanoidControl/Scripts/Networking/Netcode/Editor.meta new file mode 100644 index 0000000..d4f4a97 --- /dev/null +++ b/Runtime/HumanoidControl/Scripts/Networking/Netcode/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2d90d19d1758e7640a802a318b83849a +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/HumanoidControl/Scripts/Networking/Netcode/Editor/HumanoidNetcode_Editor.cs b/Runtime/HumanoidControl/Scripts/Networking/Netcode/Editor/HumanoidNetcode_Editor.cs new file mode 100644 index 0000000..716486b --- /dev/null +++ b/Runtime/HumanoidControl/Scripts/Networking/Netcode/Editor/HumanoidNetcode_Editor.cs @@ -0,0 +1,47 @@ +using UnityEngine; +#if !UNITY_2019_1_OR_NEWER +using UnityEngine.Networking; +#endif + +namespace Passer.Humanoid { + + public class OnLoadHumanoidPlayerNetcode { + + public static void CheckHumanoidPlayer() { +#if !UNITY_2019_1_OR_NEWER + string prefabPath = OnLoadHumanoidPlayer.GetHumanoidPlayerPrefabPath(); + GameObject playerPrefab = OnLoadHumanoidPlayer.GetHumanoidPlayerPrefab(prefabPath); + +#pragma warning disable 0618 + NetworkManager nwManager = Object.FindObjectOfType(); +#if hNW_UNET + if (nwManager == null) { + NetworkingStarter nwStarter = Object.FindObjectOfType(); + if (nwStarter != null) + nwManager = nwStarter.gameObject.AddComponent(); + } + + if (nwManager != null && nwManager.playerPrefab == null) + nwManager.playerPrefab = (GameObject)Resources.Load("HumanoidPlayer"); + + if (playerPrefab != null) { + NetworkIdentity nwId = playerPrefab.GetComponent(); + if (nwId == null) + nwId = playerPrefab.AddComponent(); + } +#else + if (nwManager != null) + Object.DestroyImmediate(nwManager, true); + + if (playerPrefab != null) { + NetworkIdentity nwId = playerPrefab.GetComponent(); + if (nwId != null) + Object.DestroyImmediate(nwId, true); + } +#endif +#pragma warning restore 0618 + OnLoadHumanoidPlayer.UpdateHumanoidPrefab(playerPrefab, prefabPath); +#endif + } + } +} diff --git a/Runtime/HumanoidControl/Scripts/Networking/Netcode/Editor/HumanoidNetcode_Editor.cs.meta b/Runtime/HumanoidControl/Scripts/Networking/Netcode/Editor/HumanoidNetcode_Editor.cs.meta new file mode 100644 index 0000000..f6f44ca --- /dev/null +++ b/Runtime/HumanoidControl/Scripts/Networking/Netcode/Editor/HumanoidNetcode_Editor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cf06de02979276348ac7c299bc830dd2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/HumanoidControl/Scripts/Networking/Netcode/HumanoidPlayerNetcode.cs b/Runtime/HumanoidControl/Scripts/Networking/Netcode/HumanoidPlayerNetcode.cs new file mode 100644 index 0000000..3fc61b1 --- /dev/null +++ b/Runtime/HumanoidControl/Scripts/Networking/Netcode/HumanoidPlayerNetcode.cs @@ -0,0 +1,528 @@ +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Networking; + +namespace Passer.Humanoid { + +#if hNW_UNET +#pragma warning disable 0618 + [RequireComponent(typeof(NetworkIdentity))] + public partial class HumanoidPlayer : PawnUnet, IHumanoidNetworking { + + public ulong nwId { + get { return netId.Value; } + } + + public List humanoids { get; set; } + + protected NetworkIdentity identity; + + public ulong GetObjectIdentity(GameObject obj) { + NetworkIdentity identity = obj.GetComponent(); + if (identity == null) + return 0; + + return identity.netId.Value; + } + + public GameObject GetGameObject(ulong objIdentity) { + NetworkInstanceId netId = new NetworkInstanceId((uint)objIdentity); + GameObject gameObject = ClientScene.FindLocalObject(netId); + return gameObject; + } + + #region Init + + override public void Awake() { + DontDestroyOnLoad(this); + + identity = GetComponent(); + humanoids = new List(); + + lastSend = Time.time; + } + + public override void OnStartClient() { + name = name + " " + netId; + + //NetworkManager nwManager = FindObjectOfType(); + //short msgType = MsgType.Highest + 2; + //nwManager.client.RegisterHandler(msgType, ClientProcessAvatarPose); + + if (identity.isServer) { + IHumanoidNetworking[] nwHumanoids = FindObjectsOfType(); + foreach (IHumanoidNetworking nwHumanoid in nwHumanoids) { + foreach (HumanoidControl humanoid in nwHumanoid.humanoids) { + if (humanoid.isRemote) + continue; + + DebugLog("Server Instantiate " + humanoid.nwId + " " + humanoid.humanoidId); + ((IHumanoidNetworking)this).InstantiateHumanoid(humanoid); + } + } + } + } + + public override void OnStartServer() { + //short msgType = MsgType.Highest + 1; + //NetworkServer.RegisterHandler(msgType, ForwardAvatarPose); + } + + #endregion + + #region Start + + public override void OnStartLocalPlayer() { + isLocal = true; + name = "HumanoidPlayer(Local)"; + + humanoids = HumanoidNetworking.FindLocalHumanoids(); + if (debug <= PawnNetworking.DebugLevel.Info) + Debug.Log((int)netId.Value + ": Found " + humanoids.Count + " Humanoids"); + + for (int i = 0; i < humanoids.Count; i++) { + HumanoidControl humanoid = humanoids[i]; + if (humanoid.isRemote) + continue; + + humanoid.nwId = netId.Value; + humanoid.humanoidNetworking = this; + + if (debug <= PawnNetworking.DebugLevel.Info) + Debug.Log(humanoid.nwId + ": Send Start Humanoid " + humanoid.humanoidId); + + ((IHumanoidNetworking)this).InstantiateHumanoid(humanoid); + } + foreach (HumanoidControl humanoid in humanoids) + DetectNetworkObjects(humanoid); + + + NetworkingSpawner spawner = FindObjectOfType(); + if (spawner != null) + spawner.OnNetworkingStarted(); + } + + #endregion + + #region Update + + protected virtual void FixedUpdate() { + //if (Time.time > lastSend + 1 / sendRate) { + // foreach (HumanoidControl humanoid in humanoids) { + // if (!humanoid.isRemote) { + // UpdateHumanoidPose(humanoid); + // if (syncTracking) + // SyncTrackingSpace(humanoid); + // } + // } + // lastSend = Time.time; + //} + } + + protected virtual void Update() { + if (smoothing == PawnNetworking.Smoothing.Interpolation || + smoothing == PawnNetworking.Smoothing.Extrapolation) { + + HumanoidNetworking.SmoothUpdate(humanoids); + } + } + + public override void LateUpdate() { + if (Time.time > lastSend + 1 / sendRate) { + foreach (HumanoidControl humanoid in humanoids) { + if (!humanoid.isRemote) { + UpdateHumanoidPose(humanoid); + if (syncTracking) + SyncTrackingSpace(humanoid); + } + } + lastSend = Time.time; + } + } + + #endregion + + #region Stop + + override public void OnDestroy() { + if (debug <= PawnNetworking.DebugLevel.Info) + Debug.Log((int)netId.Value + ": Destroy Remote Humanoid"); + + foreach (HumanoidControl humanoid in humanoids) { + if (humanoid == null) + continue; + + if (humanoid.isRemote) { + if (humanoid.gameObject != null) + Destroy(humanoid.gameObject); + } + else + humanoid.nwId = 0; + } + } + #endregion + + protected virtual void SendToServer(NetworkIdentity identity, HumanoidNetworking.IMessage msg) { + byte[] data = msg.Serialize(); + + short msgType = MsgType.Highest + 1; + writer = new NetworkWriter(); + writer.StartMessage(msgType); + writer.WriteBytesAndSize(data, data.Length); + writer.FinishMessage(); + identity.connectionToServer.SendWriter(writer, Channels.DefaultUnreliable); + } + + protected virtual void SendToClients(byte[] data) { + short msgType = MsgType.Highest + 2; + NetworkWriter sWriter = new NetworkWriter(); + + sWriter.StartMessage(msgType); + sWriter.WriteBytesAndSize(data, data.Length); + sWriter.FinishMessage(); + + NetworkServer.SendWriterToReady(null, sWriter, Channels.DefaultUnreliable); + } + + #region Instantiate Humanoid + + void IHumanoidNetworking.InstantiateHumanoid(HumanoidControl humanoid) { + if (debug <= PawnNetworking.DebugLevel.Info) + DebugLog("Instantiate Humanoid " + humanoid.nwId + "/" + humanoid.humanoidId); + + HumanoidNetworking.InstantiateHumanoid instantiateHumanoid = new HumanoidNetworking.InstantiateHumanoid(humanoid); + byte[] data = instantiateHumanoid.Serialize(); + + CmdForwardInstantiateHumanoid(data); + } + + protected HumanoidNetworking.InstantiateHumanoid instantiatedHumanoid; + + [Command] // @ server + protected virtual void CmdForwardInstantiateHumanoid(byte[] data) { + + instantiatedHumanoid = new HumanoidNetworking.InstantiateHumanoid(data); + HumanoidPlayer[] nwHumanoids = FindObjectsOfType(); + foreach (HumanoidPlayer nwHumanoid in nwHumanoids) + nwHumanoid.ServerSendInstantiateHumanoid(); + } + + protected virtual void ServerSendInstantiateHumanoid() { + if (debug <= PawnNetworking.DebugLevel.Info) { + DebugLog("Server Send InstantiateHumanoid: " + instantiatedHumanoid.nwId + "/" + instantiatedHumanoid.humanoidId); + } + + byte[] data = instantiatedHumanoid.Serialize(); + RpcReceiveInitiateHumanoid(data); + } + + + [ClientRpc] // @ remote client + protected virtual void RpcReceiveInitiateHumanoid(byte[] data) { + HumanoidNetworking.InstantiateHumanoid instantiateHumanoid = new HumanoidNetworking.InstantiateHumanoid(data); + + if (debug <= PawnNetworking.DebugLevel.Info) + DebugLog("Received Instantiate Humanoid " + instantiateHumanoid.nwId + "/" + instantiateHumanoid.humanoidId); + + if (instantiateHumanoid.nwId != identity.netId.Value) { + // Get the right HumanoidPlayer for this humanoid + NetworkInstanceId netId = new NetworkInstanceId((uint)instantiateHumanoid.nwId); + GameObject gameObject = ClientScene.FindLocalObject(netId); + HumanoidPlayer humanoidPlayer = gameObject.GetComponent(); + if (humanoidPlayer != null) + humanoidPlayer.ReceiveInstantiate(data); + else + DebugError("Could not find HumanoidPlayer with id = " + instantiateHumanoid.nwId); + } + else + this.ReceiveInstantiate(data); + } + + #endregion + + #region Destroy Humanoid + + void IHumanoidNetworking.DestroyHumanoid(HumanoidControl humanoid) { + if (humanoid == null) + return; + + if (debug <= PawnNetworking.DebugLevel.Info) + DebugLog("Destroy Humanoid " + humanoid.humanoidId); + + HumanoidNetworking.DestroyHumanoid destroyHumanoid = new HumanoidNetworking.DestroyHumanoid(humanoid); + byte[] data = destroyHumanoid.Serialize(); + + CmdForwardDestroyHumanoid(data); + } + + [Command] // @ server + private void CmdForwardDestroyHumanoid(byte[] data) { + if (debug <= PawnNetworking.DebugLevel.Debug) + DebugLog("Forward DestroyHumanoid"); + + RpcReceiveDestroyHumanoid(data); + } + + [ClientRpc] + private void RpcReceiveDestroyHumanoid(byte[] data) { + this.ReceiveDestroy(data); + } + + #endregion + + #region Pose + + public HumanoidNetworking.HumanoidPose lastHumanoidPose { get; set; } + + public void UpdateHumanoidPose(HumanoidControl humanoid) { + HumanoidNetworking.HumanoidPose humanoidPose = + new HumanoidNetworking.HumanoidPose(humanoid, Time.time, syncFingerSwing, syncFace); + + if (debug <= PawnNetworking.DebugLevel.Debug) + DebugLog("Send Humanoid Pose " + humanoid.nwId + "/" + humanoid.humanoidId); + + byte[] data = humanoidPose.Serialize(); + CmdForwardHumanoidPose(data); + } + + [Command] + protected virtual void CmdForwardHumanoidPose(byte[] data) { + if (debug <= PawnNetworking.DebugLevel.Debug) { + HumanoidNetworking.HumanoidPose humanoidPose = new HumanoidNetworking.HumanoidPose(data); + DebugLog("Forward HumanoidPose " + humanoidPose.nwId + "/" + humanoidPose.humanoidId); + } + RpcReceiveHumanoidPose(data); + } + + [ClientRpc] + protected virtual void RpcReceiveHumanoidPose(byte[] data) { + this.ReceiveHumanoidPose(data); + } + + #endregion + + #region Grab + + void IHumanoidNetworking.Grab(HandTarget handTarget, GameObject obj, bool rangeCheck, HandTarget.GrabType grabType) { + if (debug <= PawnNetworking.DebugLevel.Info) + Debug.Log(handTarget.humanoid.nwId + ": Grab " + obj); + + ulong objIdentity = GetObjectIdentity(obj); + if (objIdentity == 0) { + if (debug <= PawnNetworking.DebugLevel.Error) + Debug.LogError("Grabbed object " + obj + " does not have a network identity"); + return; + } + + HumanoidNetworking.Grab grab = new HumanoidNetworking.Grab(handTarget, objIdentity, rangeCheck, grabType); + byte[] data = grab.Serialize(); + CmdForwardGrab(data); + } + + [Command] + protected virtual void CmdForwardGrab(byte[] data) { + RpcReceiveGrab(data); + } + + [ClientRpc] + protected virtual void RpcReceiveGrab(byte[] data) { + this.ReceiveGrab(data); + } + + #endregion + + #region Let Go + + void IHumanoidNetworking.LetGo(HandTarget handTarget) { + if (debug <= PawnNetworking.DebugLevel.Info) + DebugLog("LetGo"); + + HumanoidNetworking.LetGo letGo = new HumanoidNetworking.LetGo(handTarget); + byte[] data = letGo.Serialize(); + CmdForwardLetGo(data); + } + + [Command] + protected virtual void CmdForwardLetGo(byte[] data) { + RpcReceiveLetGo(data); + } + + [ClientRpc] + protected virtual void RpcReceiveLetGo(byte[] data) { + this.ReceiveLetGo(data); + } + + #endregion + + #region ChangeAvatar + + void IHumanoidNetworking.ChangeAvatar(HumanoidControl humanoid, string avatarPrefabName) { + if (debug <= PawnNetworking.DebugLevel.Info) + DebugLog("Change Avatar: " + avatarPrefabName); + + HumanoidNetworking.ChangeAvatar changeAvatar = new HumanoidNetworking.ChangeAvatar(humanoid, avatarPrefabName); + byte[] data = changeAvatar.Serialize(); + CmdForwardChangeAvatar(data); + } + + [Command] + protected virtual void CmdForwardChangeAvatar(byte[] data) { + RpcReceiveChangeAvatar(data); + } + + [ClientRpc] + protected virtual void RpcReceiveChangeAvatar(byte[] data) { + this.ReceiveChangeAvatar(data); + } + + #endregion + + #region Tracking + + public void SyncTrackingSpace(HumanoidControl humanoid) { + Transform trackingTransform = HumanoidNetworking.GetTrackingTransform(humanoid); + if (trackingTransform == null) + return; + + HumanoidNetworking.SyncTrackingSpace syncTracking = new HumanoidNetworking.SyncTrackingSpace(humanoid, trackingTransform.position, trackingTransform.rotation); + byte[] data = syncTracking.Serialize(); + CmdForwardSyncTracking(data); + } + + [Command] + protected virtual void CmdForwardSyncTracking(byte[] data) { + RpcReceiveSyncTracking(data); + } + + [ClientRpc] + protected virtual void RpcReceiveSyncTracking(byte[] data) { + this.ReceiveSyncTrackingSpace(data); + } + + #endregion + + #region Network Sync + + void IHumanoidNetworking.ReenableNetworkSync(GameObject obj) { + ReenableNetworkSync(obj); + } + + void IHumanoidNetworking.DisableNetworkSync(GameObject obj) { + DisableNetworkSync(obj); + } + + public static void ReenableNetworkSync(GameObject obj) { + NetworkTransform networkTransform = obj.GetComponent(); + if (networkTransform != null) { + networkTransform.enabled = true; + } + } + + public static void DisableNetworkSync(GameObject obj) { + NetworkTransform networkTransform = obj.GetComponent(); + if (networkTransform != null) { + networkTransform.enabled = false; + } + } + + #endregion + + #region Network Object + + protected NetworkObject[] networkObjects; + + protected void DetectNetworkObjects(HumanoidControl humanoid) { + if (networkObjects != null) + Debug.LogError("Network Objects currently support only one humanoid"); + networkObjects = humanoid.GetComponentsInChildren(); + } + + public int GetNwObjectId(NetworkObject nwObject) { + for (int i = 0; i < networkObjects.Length; i++) { + if (networkObjects[i] == nwObject) + return i; + } + return -1; + } + + #region Void Event + + public void RPC(FunctionCall functionCall) { + CmdRPCVoid(functionCall.targetGameObject, functionCall.methodName); + } + + [Command] // @ server + public void CmdRPCVoid(GameObject target, string methodName) { + RpcRPCVoid(target, methodName); + } + + [ClientRpc] // @ remote client + public void RpcRPCVoid(GameObject targetGameObject, string methodName) { + Debug.Log("RPC: " + methodName); + FunctionCall.Execute(targetGameObject, methodName); + } + + #endregion + + #region Bool Event + + public void RPC(FunctionCall functionCall, bool value) { + CmdRPCBool(functionCall.targetGameObject, functionCall.methodName, value); + } + + [Command] // @ server + public void CmdRPCBool(GameObject target, string methodName, bool value) { + RpcRPCBool(target, methodName, value); + } + + [ClientRpc] // @ remote client + public void RpcRPCBool(GameObject target, string methodName, bool value) { + FunctionCall.Execute(target, methodName, value); + } + + #endregion + + #region String Event + + //public void RPC(FunctionCall functionCall, string value) { + + // Debug.Log("HC RPC functioncall " + value); + // //CmdRPCString(nwObjectId, functionCall.methodName, value); + //} + + //[Command] // @ server + //public void CmdRPCString(int nwObjectId, string methodName, string value) { + // Debug.Log("HC Cmd functioncall " + value); + // RpcRPCString(nwObjectId, methodName, value); + //} + + //[ClientRpc] // @ remote client + //public void RpcRPCString(int nwObjectId, string methodName, string value) { + // Debug.Log("HC Client RPC functioncall " + value); + // GameObject target = networkObjects[nwObjectId].gameObject; + // FunctionCall.Execute(target, methodName, value); + //} + + #endregion + #endregion + + #region Debug + + public void DebugLog(string s) { + Debug.Log(netId + ": " + s); + } + + public void DebugWarning(string s) { + Debug.LogWarning(netId + ": " + s); + } + + public void DebugError(string s) { + Debug.LogError(netId + ": " + s); + } + + #endregion + +#pragma warning restore 0618 + } +#endif + } \ No newline at end of file diff --git a/Runtime/HumanoidControl/Scripts/Networking/Netcode/HumanoidPlayerNetcode.cs.meta b/Runtime/HumanoidControl/Scripts/Networking/Netcode/HumanoidPlayerNetcode.cs.meta new file mode 100644 index 0000000..127ad02 --- /dev/null +++ b/Runtime/HumanoidControl/Scripts/Networking/Netcode/HumanoidPlayerNetcode.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d827ddd9432ae614cbbfdeafdf7ce52c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/HumanoidControl/Scripts/Networking/Netcode/NetcodeStarter.cs b/Runtime/HumanoidControl/Scripts/Networking/Netcode/NetcodeStarter.cs new file mode 100644 index 0000000..9207373 --- /dev/null +++ b/Runtime/HumanoidControl/Scripts/Networking/Netcode/NetcodeStarter.cs @@ -0,0 +1,116 @@ +#if hNW_UNET && !UNITY_2019_1_OR_NEWER +#pragma warning disable 0618 +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Networking; +using UnityEngine.Networking.Match; + +namespace Passer { + + public class UnetStarter : INetworkingStarter { + private string roomName; + private int gameVersion; + + bool matchCreated; + private NetworkManager networkManager; + private NetworkMatch networkMatch; + + GameObject INetworkingStarter.GetHumanoidPrefab() { + GameObject humanoidPrefab = Resources.Load("HumanoidPlayer"); + return humanoidPrefab; + } + + void INetworkingStarter.StartHost(NetworkingStarter nwStarter) { + Debug.Log("start Unet Host"); + NetworkManager networkManager = nwStarter.GetComponent(); + if (networkManager == null) { + Debug.LogError("Could not start host: NetworkManager is missing"); + return; + } + + networkManager.StartHost(); + } + + void INetworkingStarter.StartClient(NetworkingStarter nwStarter) { + Debug.Log("start Unet Client"); + NetworkManager networkManager = nwStarter.GetComponent(); + NetworkClient nwClient = networkManager.StartClient(); + nwClient.Connect(nwStarter.serverIpAddress, networkManager.networkPort); + } + + public static void StartClient(string serverIpAddress) { + Debug.Log("start Unet Client"); + NetworkManager networkManager = Object.FindObjectOfType(); + NetworkClient nwClient = networkManager.StartClient(); + nwClient.Connect(serverIpAddress, networkManager.networkPort); + } + + void INetworkingStarter.StartClient(NetworkingStarter networking, string _roomName, int _gameVersion) { + roomName = _roomName; + gameVersion = _gameVersion; + + networkMatch = networking.gameObject.AddComponent(); + networkManager = networking.GetComponent(); + + networkMatch.ListMatches(0, 10, "", true, 0, gameVersion, OnMatchList); + } + +#region Events + + public void OnMatchList(bool success, string extendedInfo, List matches) { + if (success && matches != null) { + int foundRoom = -1; + for (int i = 0; i < matches.Count; i++) { + if (matches[i].name == roomName) + foundRoom = i; + } + + if (foundRoom == -1) { + networkMatch.CreateMatch(roomName, 1000, true, "", "", "", 0, gameVersion, OnMatchCreated); + } + else { + networkMatch.JoinMatch(matches[foundRoom].networkId, "", "", "", 0, 0, OnMatchJoined); + + } + } + else if (!success) { + Debug.LogError("List match failed: " + extendedInfo); + } + } + + public void OnMatchCreated(bool success, string extendedInfo, MatchInfo matchInfo) { + if (success) { + matchCreated = true; + networkManager.StartHost(matchInfo); + } + else { + Debug.LogError("Create match failed: " + extendedInfo); + } + } + + //bool joined; + public void OnMatchJoined(bool success, string extendedInfo, MatchInfo matchInfo) { + if (success) { + if (matchCreated) { + Debug.LogWarning("Match already set up, aborting..."); + return; + } + //joined = true; + NetworkClient nwClient = networkManager.StartClient(matchInfo); +#if UNITY_WSA_10_0 && !UNITY_EDITOR + //nwClient.Connect(matchInfo); not supported on WSA... +#else + nwClient.Connect(matchInfo); +#endif + } + else { + Debug.LogError("Join match failed " + extendedInfo); + } + } + +#endregion + } +} +#pragma warning restore 0618 +#endif diff --git a/Runtime/HumanoidControl/Scripts/Networking/Netcode/NetcodeStarter.cs.meta b/Runtime/HumanoidControl/Scripts/Networking/Netcode/NetcodeStarter.cs.meta new file mode 100644 index 0000000..5750c9f --- /dev/null +++ b/Runtime/HumanoidControl/Scripts/Networking/Netcode/NetcodeStarter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c63c8978982038f4eb397475fb294e7a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: