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 網絡數據信息,剛從網絡接收的數據的相關信息會被保存到這個結構中。它揭示了從哪裏來(數據源),什麼時間發送和什麼網絡視圖發送;其中包括:數據源、發送時間、網絡視圖。

{

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);

}

[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);

}

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章