mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
Updated Basic Example
This commit is contained in:
parent
c1ae882d76
commit
990bda659e
@ -26,7 +26,7 @@ Transform:
|
|||||||
m_PrefabAsset: {fileID: 0}
|
m_PrefabAsset: {fileID: 0}
|
||||||
m_GameObject: {fileID: 897184729387425976}
|
m_GameObject: {fileID: 897184729387425976}
|
||||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||||
m_LocalPosition: {x: 562, y: 368, z: 0}
|
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||||
m_Children: []
|
m_Children: []
|
||||||
m_Father: {fileID: 0}
|
m_Father: {fileID: 0}
|
||||||
@ -46,7 +46,8 @@ MonoBehaviour:
|
|||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
sceneId: 0
|
sceneId: 0
|
||||||
serverOnly: 0
|
serverOnly: 0
|
||||||
m_AssetId:
|
visible: 0
|
||||||
|
m_AssetId: dc2c4328591bef748abb8df795c17202
|
||||||
hasSpawned: 0
|
hasSpawned: 0
|
||||||
--- !u!114 &8550999602067651493
|
--- !u!114 &8550999602067651493
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
|
@ -176,6 +176,8 @@ MonoBehaviour:
|
|||||||
SendWindowSize: 4096
|
SendWindowSize: 4096
|
||||||
ReceiveWindowSize: 4096
|
ReceiveWindowSize: 4096
|
||||||
NonAlloc: 0
|
NonAlloc: 0
|
||||||
|
ReliableMaxMessageSize: 4811624
|
||||||
|
UnreliableMaxMessageSize: 1199
|
||||||
debugLog: 0
|
debugLog: 0
|
||||||
statisticsGUI: 0
|
statisticsGUI: 0
|
||||||
statisticsLog: 0
|
statisticsLog: 0
|
||||||
@ -192,7 +194,6 @@ MonoBehaviour:
|
|||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
dontDestroyOnLoad: 0
|
dontDestroyOnLoad: 0
|
||||||
PersistNetworkManagerToOfflineScene: 0
|
|
||||||
runInBackground: 1
|
runInBackground: 1
|
||||||
autoStartServerBuild: 1
|
autoStartServerBuild: 1
|
||||||
serverTickRate: 30
|
serverTickRate: 30
|
||||||
@ -207,8 +208,6 @@ MonoBehaviour:
|
|||||||
autoCreatePlayer: 1
|
autoCreatePlayer: 1
|
||||||
playerSpawnMethod: 1
|
playerSpawnMethod: 1
|
||||||
spawnPrefabs: []
|
spawnPrefabs: []
|
||||||
mainPanel: {fileID: 1712119861}
|
|
||||||
playersPanel: {fileID: 379082679}
|
|
||||||
--- !u!4 &249891957
|
--- !u!4 &249891957
|
||||||
Transform:
|
Transform:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@ -421,6 +420,7 @@ GameObject:
|
|||||||
- component: {fileID: 533055204}
|
- component: {fileID: 533055204}
|
||||||
- component: {fileID: 533055203}
|
- component: {fileID: 533055203}
|
||||||
- component: {fileID: 533055202}
|
- component: {fileID: 533055202}
|
||||||
|
- component: {fileID: 533055201}
|
||||||
m_Layer: 5
|
m_Layer: 5
|
||||||
m_Name: Canvas
|
m_Name: Canvas
|
||||||
m_TagString: Untagged
|
m_TagString: Untagged
|
||||||
@ -428,6 +428,20 @@ GameObject:
|
|||||||
m_NavMeshLayer: 0
|
m_NavMeshLayer: 0
|
||||||
m_StaticEditorFlags: 0
|
m_StaticEditorFlags: 0
|
||||||
m_IsActive: 1
|
m_IsActive: 1
|
||||||
|
--- !u!114 &533055201
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 533055200}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: 266fac335be17a243af86e88de84766d, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
mainPanel: {fileID: 1712119861}
|
||||||
|
playersPanel: {fileID: 379082679}
|
||||||
--- !u!114 &533055202
|
--- !u!114 &533055202
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
@ -10,14 +10,6 @@ namespace Mirror.Examples.Basic
|
|||||||
[AddComponentMenu("")]
|
[AddComponentMenu("")]
|
||||||
public class BasicNetManager : NetworkManager
|
public class BasicNetManager : NetworkManager
|
||||||
{
|
{
|
||||||
[Header("Canvas UI")]
|
|
||||||
|
|
||||||
[Tooltip("Assign Main Panel so it can be turned on from Player:OnStartClient")]
|
|
||||||
public RectTransform mainPanel;
|
|
||||||
|
|
||||||
[Tooltip("Assign Players Panel for instantiating PlayerUI as child")]
|
|
||||||
public RectTransform playersPanel;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called on the server when a client adds a new player with NetworkClient.AddPlayer.
|
/// Called on the server when a client adds a new player with NetworkClient.AddPlayer.
|
||||||
/// <para>The default implementation for this function creates a new player object from the playerPrefab.</para>
|
/// <para>The default implementation for this function creates a new player object from the playerPrefab.</para>
|
||||||
@ -39,6 +31,5 @@ public override void OnServerDisconnect(NetworkConnection conn)
|
|||||||
base.OnServerDisconnect(conn);
|
base.OnServerDisconnect(conn);
|
||||||
Player.ResetPlayerNumbers();
|
Player.ResetPlayerNumbers();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
18
Assets/Mirror/Examples/Basic/Scripts/CanvasUI.cs
Normal file
18
Assets/Mirror/Examples/Basic/Scripts/CanvasUI.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class CanvasUI : MonoBehaviour
|
||||||
|
{
|
||||||
|
[Tooltip("Assign Main Panel so it can be turned on from Player:OnStartClient")]
|
||||||
|
public RectTransform mainPanel;
|
||||||
|
|
||||||
|
[Tooltip("Assign Players Panel for instantiating PlayerUI as child")]
|
||||||
|
public RectTransform playersPanel;
|
||||||
|
|
||||||
|
// static instance that can be referenced directly from Player script
|
||||||
|
public static CanvasUI instance;
|
||||||
|
|
||||||
|
void Awake()
|
||||||
|
{
|
||||||
|
instance = this;
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Mirror/Examples/Basic/Scripts/CanvasUI.cs.meta
Normal file
11
Assets/Mirror/Examples/Basic/Scripts/CanvasUI.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 266fac335be17a243af86e88de84766d
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -5,7 +5,7 @@ namespace Mirror.Examples.Basic
|
|||||||
{
|
{
|
||||||
public class Player : NetworkBehaviour
|
public class Player : NetworkBehaviour
|
||||||
{
|
{
|
||||||
// Events that the UI will subscribe to
|
// Events that the PlayerUI will subscribe to
|
||||||
public event System.Action<int> OnPlayerNumberChanged;
|
public event System.Action<int> OnPlayerNumberChanged;
|
||||||
public event System.Action<Color32> OnPlayerColorChanged;
|
public event System.Action<Color32> OnPlayerColorChanged;
|
||||||
public event System.Action<int> OnPlayerDataChanged;
|
public event System.Action<int> OnPlayerDataChanged;
|
||||||
@ -13,17 +13,12 @@ public class Player : NetworkBehaviour
|
|||||||
// Players List to manage playerNumber
|
// Players List to manage playerNumber
|
||||||
static readonly List<Player> playersList = new List<Player>();
|
static readonly List<Player> playersList = new List<Player>();
|
||||||
|
|
||||||
internal static void ResetPlayerNumbers()
|
|
||||||
{
|
|
||||||
int playerNumber = 0;
|
|
||||||
foreach (Player player in playersList)
|
|
||||||
player.playerNumber = playerNumber++;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Header("Player UI")]
|
[Header("Player UI")]
|
||||||
public GameObject playerUIPrefab;
|
public GameObject playerUIPrefab;
|
||||||
GameObject playerUI;
|
GameObject playerUI;
|
||||||
|
|
||||||
|
#region SyncVars
|
||||||
|
|
||||||
[Header("SyncVars")]
|
[Header("SyncVars")]
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -62,6 +57,10 @@ void PlayerColorChanged(Color32 _, Color32 newPlayerColor)
|
|||||||
OnPlayerColorChanged?.Invoke(newPlayerColor);
|
OnPlayerColorChanged?.Invoke(newPlayerColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Server
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This is invoked for NetworkBehaviour objects when they become active on the server.
|
/// This is invoked for NetworkBehaviour objects when they become active on the server.
|
||||||
/// <para>This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created.</para>
|
/// <para>This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created.</para>
|
||||||
@ -81,14 +80,14 @@ public override void OnStartServer()
|
|||||||
InvokeRepeating(nameof(UpdateData), 1, 1);
|
InvokeRepeating(nameof(UpdateData), 1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
// This is called from BasicNetManager OnServerAddPlayer and OnServerDisconnect
|
||||||
/// Invoked on the server when the object is unspawned
|
// Player numbers are reset whenever a player joins / leaves
|
||||||
/// <para>Useful for saving object data in persistent storage</para>
|
[ServerCallback]
|
||||||
/// </summary>
|
internal static void ResetPlayerNumbers()
|
||||||
public override void OnStopServer()
|
|
||||||
{
|
{
|
||||||
CancelInvoke();
|
int playerNumber = 0;
|
||||||
playersList.Remove(this);
|
foreach (Player player in playersList)
|
||||||
|
player.playerNumber = playerNumber++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This only runs on the server, called from OnStartServer via InvokeRepeating
|
// This only runs on the server, called from OnStartServer via InvokeRepeating
|
||||||
@ -98,17 +97,38 @@ void UpdateData()
|
|||||||
playerData = Random.Range(100, 1000);
|
playerData = Random.Range(100, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Invoked on the server when the object is unspawned
|
||||||
|
/// <para>Useful for saving object data in persistent storage</para>
|
||||||
|
/// </summary>
|
||||||
|
public override void OnStopServer()
|
||||||
|
{
|
||||||
|
CancelInvoke();
|
||||||
|
playersList.Remove(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Client
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called when the local player object has been set up.
|
||||||
|
/// <para>This happens after OnStartClient(), as it is triggered by an ownership message from the server. This is an appropriate place to activate components or functionality that should only be active for the local player, such as cameras and input.</para>
|
||||||
|
/// </summary>
|
||||||
|
public override void OnStartLocalPlayer()
|
||||||
|
{
|
||||||
|
// Activate the main panel
|
||||||
|
CanvasUI.instance.mainPanel.gameObject.SetActive(true);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called on every NetworkBehaviour when it is activated on a client.
|
/// Called on every NetworkBehaviour when it is activated on a client.
|
||||||
/// <para>Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client.</para>
|
/// <para>Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client.</para>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public override void OnStartClient()
|
public override void OnStartClient()
|
||||||
{
|
{
|
||||||
// Activate the main panel
|
|
||||||
((BasicNetManager)NetworkManager.singleton).mainPanel.gameObject.SetActive(true);
|
|
||||||
|
|
||||||
// Instantiate the player UI as child of the Players Panel
|
// Instantiate the player UI as child of the Players Panel
|
||||||
playerUI = Instantiate(playerUIPrefab, ((BasicNetManager)NetworkManager.singleton).playersPanel);
|
playerUI = Instantiate(playerUIPrefab, CanvasUI.instance.playersPanel);
|
||||||
|
|
||||||
// Set this player object in PlayerUI to wire up event handlers
|
// Set this player object in PlayerUI to wire up event handlers
|
||||||
playerUI.GetComponent<PlayerUI>().SetPlayer(this, isLocalPlayer);
|
playerUI.GetComponent<PlayerUI>().SetPlayer(this, isLocalPlayer);
|
||||||
@ -130,7 +150,9 @@ public override void OnStopClient()
|
|||||||
|
|
||||||
// Disable the main panel for local player
|
// Disable the main panel for local player
|
||||||
if (isLocalPlayer)
|
if (isLocalPlayer)
|
||||||
((BasicNetManager)NetworkManager.singleton).mainPanel.gameObject.SetActive(false);
|
CanvasUI.instance.mainPanel.gameObject.SetActive(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,6 +59,5 @@ void OnPlayerDataChanged(int newPlayerData)
|
|||||||
// Show the data in the UI
|
// Show the data in the UI
|
||||||
playerDataText.text = string.Format("Data: {0:000}", newPlayerData);
|
playerDataText.text = string.Format("Data: {0:000}", newPlayerData);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user