This commit is contained in:
2025-11-13 17:40:28 +08:00
parent 962ab49609
commit 10156da245
5503 changed files with 805282 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
using UnityEngine;
using Mirror;
using Mirror.Discovery;
using Cysharp.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Tuan.GameFramework
{
public class AutoStart : MonoBehaviour
{
public NetworkManager networkManager;
public NetworkDiscovery networkDiscovery;
public float waitForHostTimeout = 1f;
private bool hostFound = false;
private async void Start()
{
networkDiscovery.OnServerFound.AddListener(OnServerFound);
await Connect();
}
private async UniTask Connect()
{
if (networkManager == null || networkDiscovery == null)
{
Debug.LogError("请确认 NetworkManager 和 NetworkDiscovery 已设置");
return;
}
hostFound = false;
networkDiscovery.StartDiscovery();
await UniTask.WaitForSeconds(1.1f);
if (!hostFound)
{
Debug.Log("未发现 Host自己成为 Host");
networkManager.StartHost();
networkDiscovery.AdvertiseServer();
}
}
private void OnServerFound(ServerResponse info)
{
if (hostFound) return;
hostFound = true;
Debug.Log($"发现 Host: {info.uri.Host}");
networkDiscovery.StopDiscovery();
networkManager.networkAddress = info.uri.Host;
networkManager.StartClient();
}
private void OnDestroy()
{
if (networkDiscovery != null)
networkDiscovery.OnServerFound.RemoveListener(OnServerFound);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9827faa6e4a06d6448411c03e4e2dff5

View File

@@ -0,0 +1,32 @@
using Mirror;
using System.Collections.Generic;
using UnityEngine;
public class VRNetworkPlayerScript : NetworkBehaviour
{
public Transform headTransform;
public VRPlayerRig vrPlayerRig;
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
public override void OnStartLocalPlayer()
{
// create a link to local vr rig, so that rig can sync to our local network players transforms
vrPlayerRig = GameObject.FindFirstObjectByType<VRPlayerRig>();
vrPlayerRig.localVRNetworkPlayerScript = this;
}
// a static global list of players that can be used for a variery of features, one being enemies
public readonly static List<VRNetworkPlayerScript> playersList = new List<VRNetworkPlayerScript>();
public override void OnStartServer()
{
playersList.Add(this);
}
public override void OnStopServer()
{
playersList.Remove(this);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: db3715461db44b54b96114e9a88f1afe

View File

@@ -0,0 +1,16 @@
using UnityEngine;
public class VRPlayerRig : MonoBehaviour
{
public Transform headTransform;
public VRNetworkPlayerScript localVRNetworkPlayerScript;
private void Update()
{
if (localVRNetworkPlayerScript)
{
// presuming you want a head object to sync, optional, same as hands.
localVRNetworkPlayerScript.headTransform.position = new Vector3(this.headTransform.position.x, this.headTransform.position.y - 1.25f, this.headTransform.position.z);
localVRNetworkPlayerScript.headTransform.eulerAngles = new Vector3(0, this.headTransform.eulerAngles.y, 0);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 2603a3a2bb8ff8f47b1e45289f1d9f70