Well I found this script here on my computer and decided to use it but it has a bug that does not know what it is, does anyone could help?
Script:
using UnityEngine;
using System.Collections;
public class Chat : MonoBehaviour {
bool usingChat = false;
bool showChat = false;
string inputField = "";
Vector2 ScrollPosition;
int width = 500;
int height = 100;
string playerName;
float LastOnFocusedtime = 0;
Rect window;
ArrayList playerList = new ArrayList ();
class PlayerNode
{
public string PlayerName;
public NetworkPlayer player;
}
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);
playerName = PlayerPrefs.SetString("Playername", "");
if(playerName == "") playerName = "RandomName" + Random.Range(1, 999);
}
void OnConnectedToServer()
{
ShowChatWindow();
networkView.RPC("TellServerOurName", RPCMode.Server, playerName);
addGameChatMessage(playerName + " Have just joined the chat");
}
void OnServerInitialized()
{
showChatWindow();
PlayerNode newEntry = new PlayerNode();
newEntry.PlayerName = playerName;
newEntry.player = Network.player;
playerList.Add(newEntry);
addGameChatMessage(playerName + " Have just joined the chat");
}
PlayerNode GetPlayerNode(NetworkPlayer netPlay)
{
foreach (PlayerNode entry in playerList)
{
if(entry.player == netPlay)
return entry;
}
Debug.LogError("GetPlayerNode: Requested a playernode of non-existing player!");
return null;
}
void OnPlayerDisconnected(NetworkPlayer netplayer)
{
addGameChatMessage("A player has disconected");
playerList.Remove(GetPlayerNode(netplayer));
}
void OnDisconectedFromServer()
{
CloseChatWindow();
}
[RPC]
void TellServerOurName(String name, NetworkMessageInfo info)
{
PlayerNode newEntry = new PlayerNode();
newEntry.PlayerName = playerName;
newEntry.player = Network.player;
playerList.Add(newEntry);
addGameChatMessage(playerName + "has 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(LastOnFocusedtime + .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();
LastOnFocusedtime = Time.time;
}
}
void reload()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);
}
}
Error :
Assets/Scripts/Chat.cs(159,27): error CS1547: Keyword `void' cannot be used in this context
And:
Assets/Scripts/Chat.cs(159,28): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='
↧