mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
Added Chat Example
This commit is contained in:
parent
bd7f75f50c
commit
ecf44518f5
8
Assets/Mirror/Examples/Chat/Prefabs.meta
Normal file
8
Assets/Mirror/Examples/Chat/Prefabs.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 55a4e4e8824ec4e329adf12e2cfb02a4
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
64
Assets/Mirror/Examples/Chat/Prefabs/Player.prefab
Normal file
64
Assets/Mirror/Examples/Chat/Prefabs/Player.prefab
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
%YAML 1.1
|
||||||
|
%TAG !u! tag:unity3d.com,2011:
|
||||||
|
--- !u!1 &5075528875289742095
|
||||||
|
GameObject:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
serializedVersion: 6
|
||||||
|
m_Component:
|
||||||
|
- component: {fileID: 3351063249001228125}
|
||||||
|
- component: {fileID: 718303009120396421}
|
||||||
|
- component: {fileID: 114398755512196590}
|
||||||
|
m_Layer: 0
|
||||||
|
m_Name: Player
|
||||||
|
m_TagString: Untagged
|
||||||
|
m_Icon: {fileID: 0}
|
||||||
|
m_NavMeshLayer: 0
|
||||||
|
m_StaticEditorFlags: 0
|
||||||
|
m_IsActive: 1
|
||||||
|
--- !u!4 &3351063249001228125
|
||||||
|
Transform:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 5075528875289742095}
|
||||||
|
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||||
|
m_LocalPosition: {x: 718.76324, y: 411.311, z: -4.8041315}
|
||||||
|
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||||
|
m_Children: []
|
||||||
|
m_Father: {fileID: 0}
|
||||||
|
m_RootOrder: 0
|
||||||
|
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||||
|
--- !u!114 &718303009120396421
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 5075528875289742095}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: 3addc5ad220944ed6888319897606739, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
syncMode: 0
|
||||||
|
syncInterval: 0.1
|
||||||
|
playerName:
|
||||||
|
--- !u!114 &114398755512196590
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 5075528875289742095}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: 9b91ecbcc199f4492b9a91e820070131, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
serverOnly: 0
|
||||||
|
m_AssetId:
|
||||||
|
m_SceneId: 0
|
7
Assets/Mirror/Examples/Chat/Prefabs/Player.prefab.meta
Normal file
7
Assets/Mirror/Examples/Chat/Prefabs/Player.prefab.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e5905ffa27de84009b346b49d518ba03
|
||||||
|
PrefabImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
8
Assets/Mirror/Examples/Chat/Scenes.meta
Normal file
8
Assets/Mirror/Examples/Chat/Scenes.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 71f6f21bb51d14dc0b231a8488826aac
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
3464
Assets/Mirror/Examples/Chat/Scenes/Main.unity
Normal file
3464
Assets/Mirror/Examples/Chat/Scenes/Main.unity
Normal file
File diff suppressed because it is too large
Load Diff
7
Assets/Mirror/Examples/Chat/Scenes/Main.unity.meta
Normal file
7
Assets/Mirror/Examples/Chat/Scenes/Main.unity.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: f4e8d4de4484e44bba666f2d1f66c73e
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
8
Assets/Mirror/Examples/Chat/Scripts.meta
Normal file
8
Assets/Mirror/Examples/Chat/Scripts.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 81da49d71176c41169a24259df78e50a
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
211
Assets/Mirror/Examples/Chat/Scripts/ChatAuthenticator.cs
Normal file
211
Assets/Mirror/Examples/Chat/Scripts/ChatAuthenticator.cs
Normal file
@ -0,0 +1,211 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Documentation: https://mirror-networking.gitbook.io/docs/components/network-authenticators
|
||||||
|
API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkAuthenticator.html
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Mirror.Examples.Chat
|
||||||
|
{
|
||||||
|
[AddComponentMenu("")]
|
||||||
|
public class ChatAuthenticator : NetworkAuthenticator
|
||||||
|
{
|
||||||
|
readonly HashSet<NetworkConnection> connectionsPendingDisconnect = new HashSet<NetworkConnection>();
|
||||||
|
|
||||||
|
[Header("Client Username")]
|
||||||
|
public string playerName;
|
||||||
|
|
||||||
|
#region Messages
|
||||||
|
|
||||||
|
public struct AuthRequestMessage : NetworkMessage
|
||||||
|
{
|
||||||
|
// use whatever credentials make sense for your game
|
||||||
|
// for example, you might want to pass the accessToken if using oauth
|
||||||
|
public string authUsername;
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct AuthResponseMessage : NetworkMessage
|
||||||
|
{
|
||||||
|
public byte code;
|
||||||
|
public string message;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Server
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called on server from StartServer to initialize the Authenticator
|
||||||
|
/// <para>Server message handlers should be registered in this method.</para>
|
||||||
|
/// </summary>
|
||||||
|
public override void OnStartServer()
|
||||||
|
{
|
||||||
|
// register a handler for the authentication request we expect from client
|
||||||
|
NetworkServer.RegisterHandler<AuthRequestMessage>(OnAuthRequestMessage, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called on server from StopServer to reset the Authenticator
|
||||||
|
/// <para>Server message handlers should be registered in this method.</para>
|
||||||
|
/// </summary>
|
||||||
|
public override void OnStopServer()
|
||||||
|
{
|
||||||
|
// unregister the handler for the authentication request
|
||||||
|
NetworkServer.UnregisterHandler<AuthRequestMessage>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called on server from OnServerAuthenticateInternal when a client needs to authenticate
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="conn">Connection to client.</param>
|
||||||
|
public override void OnServerAuthenticate(NetworkConnection conn)
|
||||||
|
{
|
||||||
|
// do nothing...wait for AuthRequestMessage from client
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called on server when the client's AuthRequestMessage arrives
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="conn">Connection to client.</param>
|
||||||
|
/// <param name="msg">The message payload</param>
|
||||||
|
public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg)
|
||||||
|
{
|
||||||
|
Debug.Log($"Authentication Request: {msg.authUsername}");
|
||||||
|
|
||||||
|
if (connectionsPendingDisconnect.Contains(conn)) return;
|
||||||
|
|
||||||
|
// check the credentials by calling your web server, database table, playfab api, or any method appropriate.
|
||||||
|
if (!Player.playerNames.Contains(msg.authUsername))
|
||||||
|
{
|
||||||
|
// Add the name to the HashSet
|
||||||
|
Player.playerNames.Add(msg.authUsername);
|
||||||
|
|
||||||
|
// Store username in authenticationData
|
||||||
|
// This will be read in Player.OnStartServer
|
||||||
|
// to set the playerName SyncVar.
|
||||||
|
conn.authenticationData = msg.authUsername;
|
||||||
|
|
||||||
|
// create and send msg to client so it knows to proceed
|
||||||
|
AuthResponseMessage authResponseMessage = new AuthResponseMessage
|
||||||
|
{
|
||||||
|
code = 100,
|
||||||
|
message = "Success"
|
||||||
|
};
|
||||||
|
|
||||||
|
conn.Send(authResponseMessage);
|
||||||
|
|
||||||
|
// Accept the successful authentication
|
||||||
|
ServerAccept(conn);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
connectionsPendingDisconnect.Add(conn);
|
||||||
|
|
||||||
|
// create and send msg to client so it knows to disconnect
|
||||||
|
AuthResponseMessage authResponseMessage = new AuthResponseMessage
|
||||||
|
{
|
||||||
|
code = 200,
|
||||||
|
message = "Username already in use...try again"
|
||||||
|
};
|
||||||
|
|
||||||
|
conn.Send(authResponseMessage);
|
||||||
|
|
||||||
|
// must set NetworkConnection isAuthenticated = false
|
||||||
|
conn.isAuthenticated = false;
|
||||||
|
|
||||||
|
// disconnect the client after 1 second so that response message gets delivered
|
||||||
|
StartCoroutine(DelayedDisconnect(conn, 1f));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator DelayedDisconnect(NetworkConnection conn, float waitTime)
|
||||||
|
{
|
||||||
|
yield return new WaitForSeconds(waitTime);
|
||||||
|
|
||||||
|
// Reject the unsuccessful authentication
|
||||||
|
ServerReject(conn);
|
||||||
|
|
||||||
|
yield return null;
|
||||||
|
|
||||||
|
// remove conn from pending connections
|
||||||
|
connectionsPendingDisconnect.Remove(conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Client
|
||||||
|
|
||||||
|
// Called by UI element Username.OnValueChanged
|
||||||
|
public void SetPlayername(string username)
|
||||||
|
{
|
||||||
|
playerName = username;
|
||||||
|
LoginUI.instance.errorText.text = string.Empty;
|
||||||
|
LoginUI.instance.errorText.gameObject.SetActive(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called on client from StartClient to initialize the Authenticator
|
||||||
|
/// <para>Client message handlers should be registered in this method.</para>
|
||||||
|
/// </summary>
|
||||||
|
public override void OnStartClient()
|
||||||
|
{
|
||||||
|
// register a handler for the authentication response we expect from server
|
||||||
|
NetworkClient.RegisterHandler<AuthResponseMessage>((Action<AuthResponseMessage>)OnAuthResponseMessage, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called on client from StopClient to reset the Authenticator
|
||||||
|
/// <para>Client message handlers should be unregistered in this method.</para>
|
||||||
|
/// </summary>
|
||||||
|
public override void OnStopClient()
|
||||||
|
{
|
||||||
|
// unregister the handler for the authentication response
|
||||||
|
NetworkClient.UnregisterHandler<AuthResponseMessage>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called on client from OnClientAuthenticateInternal when a client needs to authenticate
|
||||||
|
/// </summary>
|
||||||
|
public override void OnClientAuthenticate()
|
||||||
|
{
|
||||||
|
AuthRequestMessage authRequestMessage = new AuthRequestMessage
|
||||||
|
{
|
||||||
|
authUsername = playerName,
|
||||||
|
};
|
||||||
|
|
||||||
|
NetworkClient.connection.Send(authRequestMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called on client when the server's AuthResponseMessage arrives
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="msg">The message payload</param>
|
||||||
|
public void OnAuthResponseMessage(AuthResponseMessage msg)
|
||||||
|
{
|
||||||
|
if (msg.code == 100)
|
||||||
|
{
|
||||||
|
Debug.Log($"Authentication Response: {msg.message}");
|
||||||
|
|
||||||
|
// Authentication has been accepted
|
||||||
|
ClientAccept();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError($"Authentication Response: {msg.message}");
|
||||||
|
|
||||||
|
// Authentication has been rejected
|
||||||
|
// StopHost works for both host client and remote clients
|
||||||
|
NetworkManager.singleton.StopHost();
|
||||||
|
|
||||||
|
// Do this AFTER StopHost so it doesn't get cleared / hidden by OnClientDisconnect
|
||||||
|
LoginUI.instance.errorText.text = msg.message;
|
||||||
|
LoginUI.instance.errorText.gameObject.SetActive(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 6e2e6b40604520d408bef0a5243a58cd
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
36
Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs
Normal file
36
Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Documentation: https://mirror-networking.gitbook.io/docs/components/network-manager
|
||||||
|
API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkManager.html
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Mirror.Examples.Chat
|
||||||
|
{
|
||||||
|
[AddComponentMenu("")]
|
||||||
|
public class ChatNetworkManager : NetworkManager
|
||||||
|
{
|
||||||
|
// Called by UI element NetworkAddressInput.OnValueChanged
|
||||||
|
public void SetHostname(string hostname)
|
||||||
|
{
|
||||||
|
networkAddress = hostname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnServerDisconnect(NetworkConnection conn)
|
||||||
|
{
|
||||||
|
// remove player name from the HashSet
|
||||||
|
if (conn.authenticationData != null)
|
||||||
|
Player.playerNames.Remove((string)conn.authenticationData);
|
||||||
|
|
||||||
|
base.OnServerDisconnect(conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnClientDisconnect()
|
||||||
|
{
|
||||||
|
base.OnClientDisconnect();
|
||||||
|
LoginUI.instance.gameObject.SetActive(true);
|
||||||
|
LoginUI.instance.usernameInput.text = "";
|
||||||
|
LoginUI.instance.usernameInput.ActivateInputField();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d0cd72391a563461f88eb3ddf120efef
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
80
Assets/Mirror/Examples/Chat/Scripts/ChatUI.cs
Normal file
80
Assets/Mirror/Examples/Chat/Scripts/ChatUI.cs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
|
||||||
|
namespace Mirror.Examples.Chat
|
||||||
|
{
|
||||||
|
public class ChatUI : NetworkBehaviour
|
||||||
|
{
|
||||||
|
[Header("UI Elements")]
|
||||||
|
public InputField chatMessage;
|
||||||
|
public Text chatHistory;
|
||||||
|
public Scrollbar scrollbar;
|
||||||
|
|
||||||
|
[Header("Diagnostic - Do Not Edit")]
|
||||||
|
public string localPlayerName;
|
||||||
|
|
||||||
|
Dictionary<NetworkConnectionToClient, string> connNames = new Dictionary<NetworkConnectionToClient, string>();
|
||||||
|
|
||||||
|
public static ChatUI instance;
|
||||||
|
|
||||||
|
void Awake()
|
||||||
|
{
|
||||||
|
instance = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command(requiresAuthority = false)]
|
||||||
|
public void CmdSend(string message, NetworkConnectionToClient sender = null)
|
||||||
|
{
|
||||||
|
if (!connNames.ContainsKey(sender))
|
||||||
|
connNames.Add(sender, sender.identity.GetComponent<Player>().playerName);
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(message))
|
||||||
|
RpcReceive(connNames[sender], message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
[ClientRpc]
|
||||||
|
public void RpcReceive(string playerName, string message)
|
||||||
|
{
|
||||||
|
string prettyMessage = playerName == localPlayerName ?
|
||||||
|
$"<color=red>{playerName}:</color> {message}" :
|
||||||
|
$"<color=blue>{playerName}:</color> {message}";
|
||||||
|
AppendMessage(prettyMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnEndEdit(string input)
|
||||||
|
{
|
||||||
|
if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetButtonDown("Submit"))
|
||||||
|
SendMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Called by OnEndEdit above and UI element SendButton.OnClick
|
||||||
|
public void SendMessage()
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(chatMessage.text))
|
||||||
|
{
|
||||||
|
CmdSend(chatMessage.text.Trim());
|
||||||
|
chatMessage.text = string.Empty;
|
||||||
|
chatMessage.ActivateInputField();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void AppendMessage(string message)
|
||||||
|
{
|
||||||
|
StartCoroutine(AppendAndScroll(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator AppendAndScroll(string message)
|
||||||
|
{
|
||||||
|
chatHistory.text += message + "\n";
|
||||||
|
|
||||||
|
// it takes 2 frames for the UI to update ?!?!
|
||||||
|
yield return null;
|
||||||
|
yield return null;
|
||||||
|
|
||||||
|
// slam the scrollbar down
|
||||||
|
scrollbar.value = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Mirror/Examples/Chat/Scripts/ChatUI.cs.meta
Normal file
11
Assets/Mirror/Examples/Chat/Scripts/ChatUI.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 2c102f62d739545269250f48327d4429
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
29
Assets/Mirror/Examples/Chat/Scripts/LoginUI.cs
Normal file
29
Assets/Mirror/Examples/Chat/Scripts/LoginUI.cs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
|
||||||
|
namespace Mirror.Examples.Chat
|
||||||
|
{
|
||||||
|
public class LoginUI : MonoBehaviour
|
||||||
|
{
|
||||||
|
[Header("UI Elements")]
|
||||||
|
public InputField usernameInput;
|
||||||
|
public Button hostButton;
|
||||||
|
public Button clientButton;
|
||||||
|
public Text errorText;
|
||||||
|
|
||||||
|
public static LoginUI instance;
|
||||||
|
|
||||||
|
void Awake()
|
||||||
|
{
|
||||||
|
instance = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ToggleButtons(string username)
|
||||||
|
{
|
||||||
|
hostButton.interactable = !string.IsNullOrWhiteSpace(username);
|
||||||
|
clientButton.interactable = !string.IsNullOrWhiteSpace(username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Mirror/Examples/Chat/Scripts/LoginUI.cs.meta
Normal file
11
Assets/Mirror/Examples/Chat/Scripts/LoginUI.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7a77ca56c9d91af4b81b73a9907d6112
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
30
Assets/Mirror/Examples/Chat/Scripts/Player.cs
Normal file
30
Assets/Mirror/Examples/Chat/Scripts/Player.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Mirror.Examples.Chat
|
||||||
|
{
|
||||||
|
public class Player : NetworkBehaviour
|
||||||
|
{
|
||||||
|
public static readonly HashSet<string> playerNames = new HashSet<string>();
|
||||||
|
|
||||||
|
[SyncVar(hook = nameof(OnPlayerNameChanged))]
|
||||||
|
public string playerName;
|
||||||
|
|
||||||
|
// RuntimeInitializeOnLoadMethod -> fast playmode without domain reload
|
||||||
|
[UnityEngine.RuntimeInitializeOnLoadMethod]
|
||||||
|
static void ResetStatics()
|
||||||
|
{
|
||||||
|
playerNames.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnPlayerNameChanged(string _, string newName)
|
||||||
|
{
|
||||||
|
ChatUI.instance.localPlayerName = playerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnStartServer()
|
||||||
|
{
|
||||||
|
playerName = (string)connectionToClient.authenticationData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Mirror/Examples/Chat/Scripts/Player.cs.meta
Normal file
11
Assets/Mirror/Examples/Chat/Scripts/Player.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 3addc5ad220944ed6888319897606739
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user