Unity3D中实现聊天功能的C#代码
Unity3D中实现聊天功能的C#代码如下:using UnityEngine;using System.Collections;public class Chat : MonoBehaviour {bool usingChat = false;bool showChat = false;string inputField = "";Vector2 scrollposition;int width = 500;int height = 200;string playerName;float lastUnfocusTime = 0;Rect window;ArrayList playerList = new ArrayList();class PlayerNode{public string playerName;public NetworkPlayer player;//NetworkPlayer是一个数据结构,保存着你可以从网络定位的另一位玩家的信息。比如,基于NetworkPlayer你可以向另外一个玩家发送消息。}ArrayList chatEntries=new ArrayList();class ChatEntry{public string name="";public string text="";}// Use this for initializationvoid Start () {window = new Rect(Screen.width / 2-width/2,Screen.height-height+5,width,height);}void OnConnectedToServer(){playerName = PlayerPrefs.GetString("playerName","");if(playerName==""){playerName = "RandomName"+Random.Range(1,999);}ShowChatWindow();networkView.RPC("TellServerOurName",RPCMode.Server,playerName);//在所有连接端调用一个RPC函数。addGameChatMessage(playerName+" hase just joined the chat!");}void OnServerInitialized(){playerName = PlayerPrefs.GetString("playerName","");if(playerName==""){playerName = "RandomName"+Random.Range(1,999);}ShowChatWindow();PlayerNode newEntry =newPlayerNode();newEntry.playerName = playerName;newEntry.player = Network.player;playerList.Add(newEntry);addGameChatMessage(playerName+" hase just joined the chat!");}PlayerNode GetPlayerNode(NetworkPlayer netPlay){foreach(PlayerNode entry in playerList){if(entry.player==netPlay){return entry;}}Debug.LogError("GetPlayNode:Requested a playernode of non-existing player!");return null;}void OnPlayerDisconnected(NetworkPlayer netPlayer)//当一个玩家从服务器上断开时在服务器端调用。{addGameChatMessage("A Player has discinnected");playerList.Remove(GetPlayerNode(netPlayer));}void OnDisconnectedFromServer(){CloseChatWindow();}void TellServerOurName(string name,NetworkMessageInfo info)//NetworkMessageInfo 网络数据信息,刚从网络接收的数据的相关信息会被保存到这个结构中。它揭示了从哪里来(数据源),什么时间发送和什么网络视图发送;其中包括:数据源、发送时间、网络视图。{PlayerNode newEntry = new PlayerNode();newEntry.playerName = playerName;newEntry.player = Network.player;playerList.Add(newEntry);addGameChatMessage(playerName+" has just joined the chat!");}void CloseChatWindow(){showChat = false;inputField = "";chatEntries = new ArrayList();}void ShowChatWindow(){showChat = true;inputField = "";chatEntries = new ArrayList();}void OnGUI () {if (!showChat) return;if(Event.current.type==EventType.keyDown && Event.current.character=='\n' & inputField.Length<=0){if(lastUnfocusTime + .25f < Time.time){usingChat = true;GUI.FocusWindow(5);GUI.FocusControl("Chat input field");}}window = GUI.Window(5,window,GlobalChatWindow,"");}void GlobalChatWindow(int id){GUILayout.BeginVertical();GUILayout.Space(10);GUILayout.EndVertical();scrollposition = GUILayout.BeginScrollView(scrollposition);foreach(ChatEntry entry in chatEntries){GUILayout.BeginHorizontal();if (entry.name == " - "){GUILayout.Label(entry.name + entry.text);}else{GUILayout.Label(entry.name+": "+entry.text);}GUILayout.EndHorizontal();GUILayout.Space(2);}GUILayout.EndScrollView();if(Event.current.type==EventType.keyDown && Event.current.character=='\n' & inputField.Length>0){HitEnter(inputField);}GUI.SetNextControlName("Chat input field");inputField = GUILayout.TextField(inputField);if(Input.GetKeyDown("mouse 0")){if(usingChat){usingChat = false;GUI.UnfocusWindow();lastUnfocusTime = Time.time;}}}void HitEnter(string msg){msg = msg.Replace('\n',' ');networkView.RPC("ApplyGlobalChatText",RPCMode.All,playerName,msg);}void ApplyGlobalChatText(string name,string msg){ChatEntry entry=new ChatEntry();entry.name=name;entry.text=msg;chatEntries.Add(entry);if(chatEntries.Count>4){chatEntries.RemoveAt(0);}scrollposition.y=1000000;inputField="";}void addGameChatMessage(string str){ApplyGlobalChatText(" - ",str);if(Network.connections.Length>0){networkView.RPC("ApplyGlobalChatText",RPCMode.Others," - ",str);}}}{:5_408:}{:5_408:}{:5_408:}
我很懒,只想回复看看,另感谢楼主分享{:soso__16915934313317769624_2:}
不错 不错 不错{:soso__3922851084632044791_6:}
不错 不错 不错{:soso__3922851084632044791_6:} 还没试,先谢谢楼主 收藏了,先谢谢楼主 不错哦,学习了。就不知道在高频率刷新聊天的时候效率是否过关 楼主是超人 真心顶
页:
[1]