【狗刨学习网】using UnityEngine;using System.Collections;public class Chat : MonoBehaviour { bool usingChat = false; bool showChat = false; string inputField = ""; Vector2 scrollposition; int width = ; int height = ; 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 initialization void Start () { window = new Rect(Screen.width / 2-width/2,Screen.height-height5,width,height); } void OnConnectedToServer() { playerName = PlayerPrefs.GetString("playerName",""); if(playerName=="") { playerName = "RandomName"Random.Range(1,); } 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,); } ShowChatWindow(); PlayerNode newEntry =new PlayerNode(); 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(); } [RPC] void TellServerOurName(string name,NetworkMessageInfo info)//NetworkMessageInfo 网络数据信息,刚从网络接收的数据的相关信息会被保存到这个结构中。它揭示了从哪里来(数据源),什么时间发送和什么网络视图发送;其中包括:数据源、发送时间、网络视图。 ... orkMessageInfo.html { 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 .f < 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(); 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); } [RPC] 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=; inputField=""; } void addGameChatMessage(string str) { ApplyGlobalChatText(" - ",str); if(Network.connections.Length>0) { networkView.RPC("ApplyGlobalChatText",RPCMode.Others," - ",str); } }}声明:此篇文档时来自于【狗刨学习网】社区,是网友自行发布的Unity3D学习文章,如果有什么内容侵犯了你的相关权益,请与官方沟通,我们会即时处理。更多精彩内容:www.gopedu.com
推荐整理分享unity3d中实现聊天功能(unity 3d教程),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:unity3ds,unity3d操作,unity3d的,unity3ds,unity 3d教程,unity3ds,unity3d documentation,unity3d documentation,内容如对您有帮助,希望把文章链接给更多的朋友!
Unity3D中手势及重力加速度(神庙逃亡操作) 欢迎来到unity学习、unity培训、unity企业培训教育专区,这里有很多U3D资源、U3D培训视频、U3D教程、U3D常见问题、U3D项目源码,【狗刨学习网】unity极致学
Unity 5引擎公布:提供完整功能且不收分成 【狗刨学习网专稿,转载请注明出处】狗刨学习网报道/Unity今天推出了Unity5引擎发布计划,同时宣布将会调整该引擎的业务模式。和此前的产品一样,Un
Unity3d版本控制 【狗刨学习网】官网地址: