feat: Chat example (#1305)

* Add chat example

* Update Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs

Co-Authored-By: MrGadget <chris@clevertech.net>

* Update Assets/Mirror/Examples/Chat/Scripts/Player.cs

Co-Authored-By: MrGadget <chris@clevertech.net>

* Update Assets/Mirror/Examples/Chat/Scripts/ChatWindow.cs

Co-Authored-By: MrGadget <chris@clevertech.net>

* Update Assets/Mirror/Examples/Chat/Scripts/Player.cs

Co-Authored-By: MrGadget <chris@clevertech.net>

* Update Assets/Mirror/Examples/Chat/Scripts/Player.cs

Co-Authored-By: MrGadget <chris@clevertech.net>

* Update Assets/Mirror/Examples/Chat/Scripts/Player.cs

Co-Authored-By: MrGadget <chris@clevertech.net>

* Change package to chat

* Update Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs

Co-Authored-By: MrGadget <chris@clevertech.net>

* Update Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs

Co-Authored-By: MrGadget <chris@clevertech.net>

* Update Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs

Co-Authored-By: MrGadget <chris@clevertech.net>

* Update Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs

Co-Authored-By: MrGadget <chris@clevertech.net>

* Update Assets/Mirror/Examples/Chat/Scripts/ChatWindow.cs

Co-Authored-By: MrGadget <chris@clevertech.net>

* UI FIxes

* Lighting

* Fixed ScrollView

* Fixed username assignment

* removed commented line

* Layout refinement

* remove empty lines

Co-Authored-By: MrGadget <chris@clevertech.net>

* fixed which element was enabled

* Strip unprintable characters

* reverted ASCI filter

Co-authored-by: MrGadget <chris@clevertech.net>
This commit is contained in:
Paul Pacheco 2019-12-21 13:22:02 -06:00 committed by GitHub
parent ed7e509ed6
commit 9926472d98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 3812 additions and 0 deletions

View File

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

View File

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

View File

@ -0,0 +1,61 @@
%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:
--- !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

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: e5905ffa27de84009b346b49d518ba03
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: f4e8d4de4484e44bba666f2d1f66c73e
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

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

View File

@ -0,0 +1,52 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using System;
namespace Mirror.Examples.Chat
{
public class ChatNetworkManager : NetworkManager
{
public string playerName { get; set; }
public void SetHostname(string hostname)
{
this.networkAddress = hostname;
}
public ChatWindow chatWindow;
public class CreatePlayerMessage: MessageBase
{
public string name;
}
public override void OnStartServer()
{
base.OnStartServer();
NetworkServer.RegisterHandler<CreatePlayerMessage>(OnCreatePlayer);
}
public override void OnClientConnect(NetworkConnection conn)
{
base.OnClientConnect(conn);
// tell the server to create a player with this name
conn.Send(new CreatePlayerMessage
{
name = playerName
});
}
private void OnCreatePlayer(NetworkConnection connection, CreatePlayerMessage createPlayerMessage)
{
// create a gameobject using the name supplied by client
GameObject playergo = Instantiate(playerPrefab);
playergo.GetComponent<Player>().playerName = createPlayerMessage.name;
// set it as the player
NetworkServer.AddPlayerForConnection(connection, playergo);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d0cd72391a563461f88eb3ddf120efef
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,45 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Mirror.Examples.Chat
{
public class ChatWindow : MonoBehaviour
{
public InputField chatMessage;
public Text chatHistory;
public Scrollbar scrollbar;
public void OnSend()
{
if (chatMessage.text.Trim() == "") return;
// get our player
Player player = NetworkClient.connection.identity.GetComponent<Player>();
// send a message
player.CmdSend(chatMessage.text.Trim());
chatMessage.text = "";
}
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;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2c102f62d739545269250f48327d4429
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,38 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Mirror.Examples.Chat
{
public class Player : NetworkBehaviour
{
[SyncVar]
public string playerName;
public ChatWindow chatWindow => ((ChatNetworkManager)NetworkManager.singleton).chatWindow;
[Command]
public void CmdSend(string message)
{
if (message.Trim() != "")
RpcReceive(message.Trim());
}
public override void OnStartLocalPlayer()
{
chatWindow.gameObject.SetActive(true);
}
[ClientRpc]
public void RpcReceive(string message)
{
string prettyMessage = isLocalPlayer ?
$"<color=red>{playerName}: </color> {message}" :
$"<color=blue>{playerName}: </color> {message}";
chatWindow.AppendMessage(prettyMessage);
Debug.Log(message);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3addc5ad220944ed6888319897606739
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: