Multiplayer Updated
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f7448592c139c41d29d8786a9ddbd99f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2a426b3dc3eb82b40bbe10eb21fd75d1
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using Mirror;
|
||||
|
||||
namespace Mirror.Examples.Common
|
||||
{
|
||||
public class CanvasNetworkManagerHUD : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject startButtonsGroup;
|
||||
[SerializeField] private GameObject statusLabelsGroup;
|
||||
|
||||
[SerializeField] private Button startHostButton;
|
||||
[SerializeField] private Button startServerOnlyButton;
|
||||
[SerializeField] private Button startClientButton;
|
||||
|
||||
[SerializeField] private Button mainStopButton;
|
||||
[SerializeField] private Text mainStopButtonText;
|
||||
[SerializeField] private Button secondaryStopButton;
|
||||
[SerializeField] private Text statusText;
|
||||
|
||||
[SerializeField] private InputField inputNetworkAddress;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Init the input field with Network Manager's network address.
|
||||
inputNetworkAddress.text = NetworkManager.singleton.networkAddress;
|
||||
|
||||
RegisterListeners();
|
||||
|
||||
//RegisterClientEvents();
|
||||
|
||||
CheckWebGLPlayer();
|
||||
}
|
||||
|
||||
private void RegisterListeners()
|
||||
{
|
||||
// Add button listeners. These buttons are already added in the inspector.
|
||||
startHostButton.onClick.AddListener(OnClickStartHostButton);
|
||||
startServerOnlyButton.onClick.AddListener(OnClickStartServerButton);
|
||||
startClientButton.onClick.AddListener(OnClickStartClientButton);
|
||||
mainStopButton.onClick.AddListener(OnClickMainStopButton);
|
||||
secondaryStopButton.onClick.AddListener(OnClickSecondaryStopButton);
|
||||
|
||||
// Add input field listener to update NetworkManager's Network Address
|
||||
// when changed.
|
||||
inputNetworkAddress.onValueChanged.AddListener(delegate { OnNetworkAddressChange(); });
|
||||
}
|
||||
|
||||
// Not working at the moment. Can't register events.
|
||||
/*private void RegisterClientEvents()
|
||||
{
|
||||
NetworkClient.OnConnectedEvent += OnClientConnect;
|
||||
NetworkClient.OnDisconnectedEvent += OnClientDisconnect;
|
||||
}*/
|
||||
|
||||
private void CheckWebGLPlayer()
|
||||
{
|
||||
// WebGL can't be host or server.
|
||||
if (Application.platform == RuntimePlatform.WebGLPlayer)
|
||||
{
|
||||
startHostButton.interactable = false;
|
||||
startServerOnlyButton.interactable = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshHUD()
|
||||
{
|
||||
if (!NetworkServer.active && !NetworkClient.isConnected)
|
||||
{
|
||||
StartButtons();
|
||||
}
|
||||
else
|
||||
{
|
||||
StatusLabelsAndStopButtons();
|
||||
}
|
||||
}
|
||||
|
||||
private void StartButtons()
|
||||
{
|
||||
if (!NetworkClient.active)
|
||||
{
|
||||
statusLabelsGroup.SetActive(false);
|
||||
startButtonsGroup.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowConnectingStatus();
|
||||
}
|
||||
}
|
||||
|
||||
private void StatusLabelsAndStopButtons()
|
||||
{
|
||||
startButtonsGroup.SetActive(false);
|
||||
statusLabelsGroup.SetActive(true);
|
||||
|
||||
// Host
|
||||
if (NetworkServer.active && NetworkClient.active)
|
||||
{
|
||||
statusText.text = $"<b>Host</b>: running via {Transport.active}";
|
||||
|
||||
mainStopButtonText.text = "Stop Client";
|
||||
}
|
||||
// Server only
|
||||
else if (NetworkServer.active)
|
||||
{
|
||||
statusText.text = $"<b>Server</b>: running via {Transport.active}";
|
||||
|
||||
mainStopButtonText.text = "Stop Server";
|
||||
}
|
||||
// Client only
|
||||
else if (NetworkClient.isConnected)
|
||||
{
|
||||
statusText.text = $"<b>Client</b>: connected to {NetworkManager.singleton.networkAddress} via {Transport.active}";
|
||||
|
||||
mainStopButtonText.text = "Stop Client";
|
||||
}
|
||||
|
||||
// Note secondary button is only used to Stop Host, and is only needed in host mode.
|
||||
secondaryStopButton.gameObject.SetActive(NetworkServer.active && NetworkClient.active);
|
||||
}
|
||||
|
||||
private void ShowConnectingStatus()
|
||||
{
|
||||
startButtonsGroup.SetActive(false);
|
||||
statusLabelsGroup.SetActive(true);
|
||||
|
||||
secondaryStopButton.gameObject.SetActive(false);
|
||||
|
||||
statusText.text = "Connecting to " + NetworkManager.singleton.networkAddress + "..";
|
||||
mainStopButtonText.text = "Cancel Connection Attempt";
|
||||
}
|
||||
|
||||
private void OnClickStartHostButton()
|
||||
{
|
||||
NetworkManager.singleton.StartHost();
|
||||
}
|
||||
|
||||
private void OnClickStartServerButton()
|
||||
{
|
||||
NetworkManager.singleton.StartServer();
|
||||
}
|
||||
|
||||
private void OnClickStartClientButton()
|
||||
{
|
||||
NetworkManager.singleton.StartClient();
|
||||
//ShowConnectingStatus();
|
||||
}
|
||||
|
||||
private void OnClickMainStopButton()
|
||||
{
|
||||
if (NetworkClient.active)
|
||||
{
|
||||
NetworkManager.singleton.StopClient();
|
||||
}
|
||||
else
|
||||
{
|
||||
NetworkManager.singleton.StopServer();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnClickSecondaryStopButton()
|
||||
{
|
||||
NetworkManager.singleton.StopHost();
|
||||
}
|
||||
|
||||
private void OnNetworkAddressChange()
|
||||
{
|
||||
NetworkManager.singleton.networkAddress = inputNetworkAddress.text;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
RefreshHUD();
|
||||
}
|
||||
|
||||
/* This does not work because we can't register the handler.
|
||||
void OnClientConnect() {}
|
||||
|
||||
private void OnClientDisconnect()
|
||||
{
|
||||
RefreshHUD();
|
||||
}
|
||||
*/
|
||||
|
||||
// Do a check for the presence of a Network Manager component when
|
||||
// you first add this script to a gameobject.
|
||||
private void Reset()
|
||||
{
|
||||
#if UNITY_2021_3_OR_NEWER
|
||||
if (!FindAnyObjectByType<NetworkManager>())
|
||||
Debug.LogError("This component requires a NetworkManager component to be present in the scene. Please add!");
|
||||
#else
|
||||
// Deprecated in Unity 2023.1
|
||||
if (!FindObjectOfType<NetworkManager>())
|
||||
Debug.LogError("This component requires a NetworkManager component to be present in the scene. Please add!");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 280f31d6f8b054f40b424ccf715b4488
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+1598
File diff suppressed because it is too large
Load Diff
+7
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 481ace15d5c62488e94a119ae14d62b8
|
||||
PrefabImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,37 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mirror.Examples.Common
|
||||
{
|
||||
public class FPS : MonoBehaviour
|
||||
{
|
||||
// fps accessible to the outside
|
||||
public int framesPerSecond { get; private set; }
|
||||
|
||||
// configuration
|
||||
public bool showGUI = true;
|
||||
public bool showLog = false;
|
||||
|
||||
// helpers
|
||||
int count;
|
||||
double startTime;
|
||||
|
||||
protected void Update()
|
||||
{
|
||||
++count;
|
||||
if (Time.time >= startTime + 1)
|
||||
{
|
||||
framesPerSecond = count;
|
||||
startTime = Time.time;
|
||||
count = 0;
|
||||
if (showLog) Debug.Log($"FPS: {framesPerSecond}");
|
||||
}
|
||||
}
|
||||
|
||||
protected void OnGUI()
|
||||
{
|
||||
if (!showGUI) return;
|
||||
|
||||
GUI.Label(new Rect(Screen.width - 70, 0, 70, 25), $"FPS: {framesPerSecond}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6635375fbc6be456ea640b75add6378e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
// Useful for Text Meshes that should face the camera.
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mirror.Examples.Common
|
||||
{
|
||||
public class FaceCamera : MonoBehaviour
|
||||
{
|
||||
// LateUpdate so that all camera updates are finished.
|
||||
void LateUpdate()
|
||||
{
|
||||
transform.forward = Camera.main.transform.forward;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: afa2d590c474413d9fc183551385ed85
|
||||
timeCreated: 1632052051
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 884ea91b35a345443a0320cea07aad68
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,39 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mirror.Examples.Common
|
||||
{
|
||||
public class PhysicsSimulator : MonoBehaviour
|
||||
{
|
||||
PhysicsScene physicsScene;
|
||||
PhysicsScene2D physicsScene2D;
|
||||
|
||||
bool simulatePhysicsScene;
|
||||
bool simulatePhysicsScene2D;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (NetworkServer.active)
|
||||
{
|
||||
physicsScene = gameObject.scene.GetPhysicsScene();
|
||||
simulatePhysicsScene = physicsScene.IsValid() && physicsScene != Physics.defaultPhysicsScene;
|
||||
|
||||
physicsScene2D = gameObject.scene.GetPhysicsScene2D();
|
||||
simulatePhysicsScene2D = physicsScene2D.IsValid() && physicsScene2D != Physics2D.defaultPhysicsScene;
|
||||
}
|
||||
else
|
||||
{
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
[ServerCallback]
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (simulatePhysicsScene)
|
||||
physicsScene.Simulate(Time.fixedDeltaTime);
|
||||
|
||||
if (simulatePhysicsScene2D)
|
||||
physicsScene2D.Simulate(Time.fixedDeltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 78e3051d2c03f27429276d8a55a6d15c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,46 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!1 &2581897434666803994
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 440985591853936351}
|
||||
- component: {fileID: 3973927562887465373}
|
||||
m_Layer: 0
|
||||
m_Name: PhysicsSimulator
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!4 &440985591853936351
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2581897434666803994}
|
||||
serializedVersion: 2
|
||||
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_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &3973927562887465373
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2581897434666803994}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 78e3051d2c03f27429276d8a55a6d15c, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 99d75386017b4ba44ad1482ee7938f5a
|
||||
PrefabImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,34 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mirror.Examples.Common
|
||||
{
|
||||
public class RandomColor : NetworkBehaviour
|
||||
{
|
||||
// Unity clones the material when GetComponent<Renderer>().material is called
|
||||
// Cache it here and destroy it in OnDestroy to prevent a memory leak
|
||||
Material cachedMaterial;
|
||||
|
||||
// Color32 packs to 4 bytes
|
||||
[SyncVar(hook = nameof(SetColor))]
|
||||
public Color32 color = Color.black;
|
||||
|
||||
void SetColor(Color32 _, Color32 newColor)
|
||||
{
|
||||
if (cachedMaterial == null) cachedMaterial = GetComponentInChildren<Renderer>().material;
|
||||
cachedMaterial.color = newColor;
|
||||
}
|
||||
|
||||
public override void OnStartServer()
|
||||
{
|
||||
// Only set the color once. Players may be respawned,
|
||||
// and we don't want to keep changing their colors.
|
||||
if (color == Color.black)
|
||||
color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
Destroy(cachedMaterial);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a91a718a70d01b347b75cb768a6f1a92
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user