纳金网
标题:
unity3d中实现聊天功能的C#代码_教程
[打印本页]
作者:
会飞的鱼
时间:
2011-11-3 14:43
标题:
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 initialization
void 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 =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 网络数据信息,刚从网络接收的数据的相关信息会被保存到这个结构中。它揭示了从哪里来(数据源),什么时间发送和什么网络视图发送;其中包括:数据源、发送时间、网络视图。
http://3d.ceeger.com/Script/NetworkMessageInfo/NetworkMessageInfo.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 = ***e;
inputField = "";
chatEntries = new ArrayList();
}
void OnGUI () {
if (!showChat) return;
if(Event.current.type==EventType.keyDown && Event.current.character=='
' & inputField.Length<=0)
{
if(lastUnfocusTime + .25f < Time.time)
{
usingChat = ***e;
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=='
' & 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('
',' ');
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=1000000;
inputField="";
}
void addGameChatMessage(string str)
{
ApplyGlobalChatText(" - ",str);
if(Network.connections.Length>0)
{
networkView.RPC("ApplyGlobalChatText",RPCMode.Others," - ",str);
}
}
}
作者:
tc
时间:
2012-2-3 23:24
非常感谢,管理员设置了需要对新回复进行审核,您的帖子通过审核后将被显示出来,现在将转入主题
作者:
菜刀吻电线
时间:
2012-2-24 23:28
这么后现代
作者:
tc
时间:
2012-4-8 23:23
真不错,全存下来了.
作者:
C.R.CAN
时间:
2012-4-13 23:21
佩服,好多阿 ,哈哈
作者:
C.R.CAN
时间:
2012-4-16 23:19
都闪开,介个帖子,偶来顶
作者:
C.R.CAN
时间:
2012-4-16 23:19
佩服,好多阿 ,哈哈
作者:
C.R.CAN
时间:
2012-6-23 23:26
很经典,很实用,学习了!
作者:
tc
时间:
2012-7-25 23:21
不错哦,顶一下......
作者:
晃晃
时间:
2012-7-27 23:26
心中有爱,爱咋咋地
作者:
菜刀吻电线
时间:
2012-8-19 23:39
响应天帅号召,顶
作者:
tc
时间:
2013-3-7 23:20
你们都躲开,我来顶
欢迎光临 纳金网 (http://course.narkii.com/club/)
Powered by Discuz! X2.5