The networkstarter starts in host mode

This commit is contained in:
Pascal Serrarens 2024-03-20 15:24:03 +01:00
parent fcffefc794
commit 8cf6002858
12 changed files with 138 additions and 84 deletions

View File

@ -1,9 +1,6 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
#if !UNITY_2019_1_OR_NEWER
using UnityEngine.Networking;
#endif
namespace Passer.Humanoid {
@ -28,6 +25,9 @@ namespace Passer.Humanoid {
}
public static void UpdateHumanoidPrefab(GameObject prefab, string prefabPath) {
if (prefab == null)
return;
if (!Application.isPlaying) {
Debug.Log("UpdateHumanoidPrefab " + Application.isFocused + " " + Application.isBatchMode + " " + Application.isEditor);
if (Application.isFocused && !Application.isBatchMode && Application.isEditor) {

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 2d90d19d1758e7640a802a318b83849a
guid: 1e00cb872f4ac3e4086b96d4e9ccdc7a
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@ -1,33 +1,34 @@
using UnityEngine;
#if !UNITY_2019_1_OR_NEWER
using UnityEngine.Networking;
#endif
using UnityEditor;
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<NetworkManager>();
#if hNW_UNET
#if hNETCODE
Unity.Netcode.NetworkManager nwManager = Object.FindObjectOfType<Unity.Netcode.NetworkManager>();
if (nwManager == null) {
NetworkingStarter nwStarter = Object.FindObjectOfType<NetworkingStarter>();
if (nwStarter != null)
nwManager = nwStarter.gameObject.AddComponent<NetworkManager>();
nwManager = nwStarter.gameObject.AddComponent<Unity.Netcode.NetworkManager>();
}
if (nwManager != null && nwManager.playerPrefab == null)
nwManager.playerPrefab = (GameObject)Resources.Load("HumanoidPlayer");
Unity.Netcode.NetworkConfig nwConfig = nwManager.NetworkConfig;
if (nwConfig.NetworkTransport == null) {
Unity.Netcode.Transports.UTP.UnityTransport unityTransport = new();
nwConfig.NetworkTransport = unityTransport;
}
if (nwConfig.PlayerPrefab == null)
nwConfig.PlayerPrefab = (GameObject)Resources.Load("HumanoidPlayer");
if (playerPrefab != null) {
NetworkIdentity nwId = playerPrefab.GetComponent<NetworkIdentity>();
Unity.Netcode.NetworkObject nwId = playerPrefab.GetComponent<Unity.Netcode.NetworkObject>();
if (nwId == null)
nwId = playerPrefab.AddComponent<NetworkIdentity>();
nwId = playerPrefab.AddComponent<Unity.Netcode.NetworkObject>();
}
#else
if (nwManager != null)
@ -39,9 +40,7 @@ namespace Passer.Humanoid {
Object.DestroyImmediate(nwId, true);
}
#endif
#pragma warning restore 0618
OnLoadHumanoidPlayer.UpdateHumanoidPrefab(playerPrefab, prefabPath);
#endif
}
}
}

View File

@ -1,5 +1,6 @@
{
"name": "PasserVR.HumanoidControl.Editor",
"rootNamespace": "",
"references": [
"PasserVR.HumanoidControl",
"Passer.Visitors.Editor",
@ -8,7 +9,8 @@
"PhotonVoice.PUN",
"PhotonRealtime",
"PhotonVoice.API",
"SteamVR"
"SteamVR",
"Unity.Netcode.Runtime"
],
"includePlatforms": [
"Editor"

View File

@ -17,7 +17,9 @@ namespace Passer.Humanoid {
public virtual void OnEnable() {
nwStarter = (NetworkingStarter)target;
#if hUNET
#if hNETCODE
OnLoadHumanoidPlayerNetcode.CheckHumanoidPlayer();
#elif hUNET
OnLoadHumanoidPlayerUnet.CheckHumanoidPlayer();
#endif
#if hPHOTON1 || hPHOTON2
@ -34,7 +36,7 @@ namespace Passer.Humanoid {
public override void OnInspectorGUI() {
serializedObject.Update();
#if !hNW_UNET && !hNW_PHOTON && !hNW_MIRROR && !hNW_BOLT
#if !hNW_PHOTON && !hNETCODE
EditorGUILayout.HelpBox("Networking Support is disabled. Check Preferences to enable it.", MessageType.Warning);
#else
@ -57,7 +59,10 @@ namespace Passer.Humanoid {
private void Inspector() {
autoStartProp = serializedObject.FindProperty("autoStart");
autoStartProp.boolValue = EditorGUILayout.Toggle("Auto Start", autoStartProp.boolValue);
#if hNW_UNET
#if hNETCODE
NetworkingPrefabInspector();
ServerTypeInspector();
#elif hNW_UNET
NetworkingPrefabInspector();
ServerTypeInspector();
#elif hNW_PHOTON
@ -82,11 +87,13 @@ namespace Passer.Humanoid {
}
private void ServerTypeInspector() {
#if !hNETCODE
SerializedProperty serverTypeProp = serializedObject.FindProperty("serverType");
serverTypeProp.intValue = (int)(NetworkingStarter.ServerType)EditorGUILayout.EnumPopup("Server Type", (NetworkingStarter.ServerType)serverTypeProp.intValue);
if ((NetworkingStarter.ServerType)serverTypeProp.intValue == NetworkingStarter.ServerType.CloudServer)
CloudServerInspector();
else
#endif
OwnServerInspector();
}
@ -100,6 +107,7 @@ namespace Passer.Humanoid {
private void OwnServerInspector() {
SerializedProperty serverIpAddressProp = serializedObject.FindProperty("serverIpAddress");
SerializedProperty serverPortProp = serializedObject.FindProperty("serverPort");
SerializedProperty useRoleFileProp = serializedObject.FindProperty("useRoleFile");
SerializedProperty roleFileName = serializedObject.FindProperty("roleFileName");
SerializedProperty roleProp = serializedObject.FindProperty("role");
@ -109,7 +117,7 @@ namespace Passer.Humanoid {
if (useRoleFileProp.boolValue) {
roleFileName.stringValue = EditorGUILayout.TextField("Role File Name", roleFileName.stringValue);
EditorGUI.indentLevel++;
#if hNW_UNET || hMIRROR
#if hNETCODE || hMIRROR
serverIpAddressProp.stringValue = EditorGUILayout.TextField("Server IP Address", serverIpAddressProp.stringValue);
#endif
EditorGUILayout.BeginHorizontal();
@ -124,19 +132,22 @@ namespace Passer.Humanoid {
}
else {
roleProp.intValue = (int)(NetworkingStarter.Role)EditorGUILayout.EnumPopup("Role", (NetworkingStarter.Role)roleProp.intValue);
#if hNW_UNET || hMIRROR
#if hNETCODE || hMIRROR
serverIpAddressProp.stringValue = EditorGUILayout.TextField("Server IP Address", serverIpAddressProp.stringValue);
#endif
}
}
#if hNW_UNET || hMIRROR
#if hNETCODE || hMIRROR
else
serverIpAddressProp.stringValue = EditorGUILayout.TextField("Server IP Address", serverIpAddressProp.stringValue);
#endif
#if hMIRROR
Mirror.NetworkManager nwManager = nwStarter.GetComponent<Mirror.NetworkManager>();
NetworkManager nwManager = nwStarter.GetComponent<Mirror.NetworkManager>();
nwManager.networkAddress = serverIpAddressProp.stringValue;
#elif hNETCODE
serverPortProp.intValue = EditorGUILayout.IntField("Server Port", serverPortProp.intValue);
#endif
}
private void WriteToRoleFile(string roleFileName, string roleText, string ipAddress) {

View File

@ -10,6 +10,7 @@ GameObject:
m_Component:
- component: {fileID: 4191195719973714}
- component: {fileID: 114636771493283014}
- component: {fileID: 1251152422678924623}
m_Layer: 0
m_Name: HumanoidPlayer
m_TagString: Untagged
@ -27,6 +28,7 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
@ -43,4 +45,30 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: eb69cd2012a95b549a6c414c3619432c, type: 3}
m_Name:
m_EditorClassIdentifier:
_syncFingerSwing: 0
_syncFace: 0
_syncTracking: 0
_debug: 3
_sendRate: 25
_createLocalRemotes: 0
--- !u!114 &1251152422678924623
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1810220956971058}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3}
m_Name:
m_EditorClassIdentifier:
GlobalObjectIdHash: 2555736071
InScenePlacedSourceGlobalObjectIdHash: 0
AlwaysReplicateAsRoot: 0
SynchronizeTransform: 1
ActiveSceneSynchronization: 0
SceneMigrationSynchronization: 1
SpawnWithObservers: 1
DontDestroyWithOwner: 0
AutoObjectParentSync: 1

View File

@ -7,7 +7,7 @@ using Unity.Netcode.Components;
namespace Passer.Humanoid {
#pragma warning disable 0618
[RequireComponent(typeof(NetworkObject))]
[RequireComponent(typeof(Unity.Netcode.NetworkObject))]
public partial class HumanoidPlayer : NetworkBehaviour, IHumanoidNetworking {
// temporary (?) dummies
@ -40,7 +40,7 @@ namespace Passer.Humanoid {
// end temporary dummies
public ulong nwId {
get { return identity.objectIdentity; }
get { return identity.NetworkObjectId; }
}
[SerializeField]
@ -55,14 +55,14 @@ namespace Passer.Humanoid {
public List<HumanoidControl> humanoids { get; set; }
protected NetworkObject identity;
protected Unity.Netcode.NetworkObject identity;
public ulong GetObjectIdentity(GameObject obj) {
NetworkObject identity = obj.GetComponent<NetworkObject>();
Unity.Netcode.NetworkObject identity = obj.GetComponent<Unity.Netcode.NetworkObject>();
if (identity == null)
return 0;
return identity.objectIdentity;
return identity.NetworkObjectId;
}
public GameObject GetGameObject(ulong objIdentity) {
@ -88,23 +88,23 @@ namespace Passer.Humanoid {
public virtual void OnStartClient() {
//name = name + " " + netId;
//NetworkManager nwManager = FindObjectOfType<NetworkManager>();
NetworkManager nwManager = FindObjectOfType<NetworkManager>();
//short msgType = MsgType.Highest + 2;
//nwManager.client.RegisterHandler(msgType, ClientProcessAvatarPose);
//No idea how this works with Netcode
//if (identity.isServer) {
// IHumanoidNetworking[] nwHumanoids = FindObjectsOfType<HumanoidPlayer>();
// foreach (IHumanoidNetworking nwHumanoid in nwHumanoids) {
// foreach (HumanoidControl humanoid in nwHumanoid.humanoids) {
// if (humanoid.isRemote)
// continue;
if (IsServer) {
IHumanoidNetworking[] nwHumanoids = FindObjectsOfType<HumanoidPlayer>();
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);
// }
// }
//}
DebugLog("Server Instantiate " + humanoid.nwId + " " + humanoid.humanoidId);
((IHumanoidNetworking)this).InstantiateHumanoid(humanoid);
}
}
}
}
public virtual void OnStartServer() {
@ -122,14 +122,14 @@ namespace Passer.Humanoid {
humanoids = HumanoidNetworking.FindLocalHumanoids();
if (debug <= HumanoidNetworking.DebugLevel.Info)
Debug.Log((int)identity.objectIdentity + ": Found " + humanoids.Count + " Humanoids");
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;
humanoid.nwId = identity.objectIdentity;
humanoid.nwId = identity.NetworkObjectId;
humanoid.humanoidNetworking = this;
if (debug <= HumanoidNetworking.DebugLevel.Info)
@ -173,6 +173,9 @@ namespace Passer.Humanoid {
public virtual void LateUpdate() {
if (Time.time > lastSend + 1 / sendRate) {
if (humanoids == null)
return;
foreach (HumanoidControl humanoid in humanoids) {
if (!humanoid.isRemote) {
UpdateHumanoidPose(humanoid);
@ -190,7 +193,7 @@ namespace Passer.Humanoid {
override public void OnDestroy() {
if (debug <= HumanoidNetworking.DebugLevel.Info)
Debug.Log((int)identity.objectIdentity + ": Destroy Remote Humanoid");
Debug.Log((int)identity.NetworkObjectId + ": Destroy Remote Humanoid");
foreach (HumanoidControl humanoid in humanoids) {
if (humanoid == null)
@ -269,7 +272,7 @@ namespace Passer.Humanoid {
if (debug <= HumanoidNetworking.DebugLevel.Info)
DebugLog("Received Instantiate Humanoid " + instantiateHumanoid.nwId + "/" + instantiateHumanoid.humanoidId);
if (instantiateHumanoid.nwId != identity.objectIdentity) {
if (instantiateHumanoid.nwId != identity.NetworkObjectId) {
// Get the right HumanoidPlayer for this humanoid
//NetworkInstanceId netId = new NetworkInstanceId((uint)instantiateHumanoid.nwId);
//GameObject gameObject = ClientScene.FindLocalObject(netId);
@ -560,15 +563,15 @@ namespace Passer.Humanoid {
#region Debug
public void DebugLog(string message) {
Debug.Log(identity.objectIdentity + ": " + message);
Debug.Log(identity.NetworkObjectId + ": " + message);
}
public void DebugWarning(string message) {
Debug.LogWarning(identity.objectIdentity + ": " + message);
Debug.LogWarning(identity.NetworkObjectId + ": " + message);
}
public void DebugError(string message) {
Debug.LogError(identity.objectIdentity + ": " + message);
Debug.LogError(identity.NetworkObjectId + ": " + message);
}
#endregion

View File

@ -1,20 +1,19 @@
#if hNW_UNET && !UNITY_2019_1_OR_NEWER
#pragma warning disable 0618
using System.Collections;
using System.Collections.Generic;
#if hNETCODE
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using Unity.Netcode;
using Unity.Netcode.Transports.UTP;
namespace Passer {
public class UnetStarter : INetworkingStarter {
public class NetcodeStarter : INetworkingStarter {
private string roomName;
private int gameVersion;
public ushort serverPort;
bool matchCreated;
private NetworkManager networkManager;
private NetworkMatch networkMatch;
//private NetworkMatch networkMatch;
GameObject INetworkingStarter.GetHumanoidPrefab() {
GameObject humanoidPrefab = Resources.Load<GameObject>("HumanoidPlayer");
@ -22,42 +21,54 @@ namespace Passer {
}
void INetworkingStarter.StartHost(NetworkingStarter nwStarter) {
Debug.Log("start Unet Host");
Debug.Log("start Netcode Host");
NetworkManager networkManager = nwStarter.GetComponent<NetworkManager>();
if (networkManager == null) {
Debug.LogError("Could not start host: NetworkManager is missing");
return;
}
networkManager.StartHost();
//networkManager.StartHost();
NetworkManager.Singleton.StartHost();
}
void INetworkingStarter.StartClient(NetworkingStarter nwStarter) {
Debug.Log("start Unet Client");
NetworkManager networkManager = nwStarter.GetComponent<NetworkManager>();
NetworkClient nwClient = networkManager.StartClient();
nwClient.Connect(nwStarter.serverIpAddress, networkManager.networkPort);
Debug.Log("start Netcode Client");
//NetworkManager networkManager = nwStarter.GetComponent<NetworkManager>();
//NetworkClient nwClient =
NetworkManager.Singleton.StartClient();
NetworkManager.Singleton.GetComponent<UnityTransport>().SetConnectionData(
nwStarter.serverIpAddress,
nwStarter.serverPort
);
}
public static void StartClient(string serverIpAddress) {
Debug.Log("start Unet Client");
NetworkManager networkManager = Object.FindObjectOfType<NetworkManager>();
NetworkClient nwClient = networkManager.StartClient();
nwClient.Connect(serverIpAddress, networkManager.networkPort);
public static void StartClient(string serverIpAddress, ushort serverPort) {
Debug.Log("start Netcode Client");
//NetworkManager networkManager = Object.FindObjectOfType<NetworkManager>();
//NetworkClient nwClient =
NetworkManager.Singleton.StartClient();
NetworkManager.Singleton.GetComponent<UnityTransport>().SetConnectionData(
serverIpAddress,
serverPort
);
}
void INetworkingStarter.StartClient(NetworkingStarter networking, string _roomName, int _gameVersion) {
roomName = _roomName;
gameVersion = _gameVersion;
networkMatch = networking.gameObject.AddComponent<NetworkMatch>();
networkManager = networking.GetComponent<NetworkManager>();
//networkMatch = networking.gameObject.AddComponent<NetworkMatch>();
//networkManager = networking.GetComponent<NetworkManager>();
networkMatch.ListMatches(0, 10, "", true, 0, gameVersion, OnMatchList);
//networkMatch.ListMatches(0, 10, "", true, 0, gameVersion, OnMatchList);
}
#region Events
public void StopClient() {; }
#region Events
/*
public void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matches) {
if (success && matches != null) {
int foundRoom = -1;
@ -108,7 +119,7 @@ namespace Passer {
Debug.LogError("Join match failed " + extendedInfo);
}
}
*/
#endregion
}
}

View File

@ -16,7 +16,8 @@
"SteamVR",
"Unity.XR.OpenVR",
"Unity.Netcode.Runtime",
"Unity.Netcode.Components"
"Unity.Netcode.Components",
"Unity.Networking.Transport"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@ -1,5 +1,7 @@
using System;
using System.IO;
using Unity.Netcode;
#if hNW_BOLT
using UdpKit;
#endif
@ -26,6 +28,8 @@ namespace Passer {
#endif
#elif hNW_BOLT
public class NetworkingStarter : Bolt.GlobalEventListener
#elif hNETCODE
public class NetworkingStarter : NetworkBehaviour
#else
public class NetworkingStarter : MonoBehaviour
#endif
@ -33,6 +37,8 @@ namespace Passer {
public bool autoStart = true;
#if hNW_UNET
protected INetworkingStarter starter = new UnetStarter();
#elif hNETCODE
protected INetworkingStarter starter = new NetcodeStarter();
#elif hNW_PHOTON
protected INetworkingStarter starter = new PunStarter();
#elif hNW_BOLT
@ -47,6 +53,7 @@ namespace Passer {
/// The IP address of the host
/// </summary>
public string serverIpAddress = "127.0.0.1";
public ushort serverPort = 1234;
/// <summary>
/// The name of the environment shared by all the users
/// </summary>

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 86153051f48cf8c488177d8f1b7aedaa
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: