聊天室代码

private var chatWindow = Rect(350,50,200,300);
private var scrollPosition : Vector2;
private var inputField = "";
private var entries = ArrayList();

class ChatEntry
{
   var text = "";
   var sender = "";
}

function OnGUI()
{
  chatWindow = GUI.Window(1,chatWindow,ChatWindowGUI,"Chat");
}

function ChatWindowGUI(id: int)
{
   scrollPosition = GUILayout.BeginScrollView(scrollPosition);
  // GUILayout.Label("Test");
   for(var entry:ChatEntry in entries)
   {
     GUILayout.Label(entry.sender+":"+entry.text);
   }
   GUILayout.EndScrollView();
  
   if(Event.current.type == EventType.keyDown && Event.current.character == "\n" && inputField.Length >0)
   {
     ApplyTextLocal(inputField);
     networkView.RPC("ApplyTextGlobal",RPCMode.Others,inputField);
     inputField = "";
   }
  
   inputField = GUILayout.TextField(inputField);
  
   GUI.DragWindow();
}

function ApplyTextLocal(newText : String)
{
   var entry = new ChatEntry();
   entry.text = newText;
   entry.sender = "Me";
  
   entries.Add(entry);
   scrollPosition.y = 10000;
}

@RPC
function ApplyTextGlobal(newText : String,msg :NetworkMessageInfo)
{
   var entry = new ChatEntry();
   entry.text = newText;
   entry.sender = msg.sender.ToString();;
  
   entries.Add(entry);
   scrollPosition.y = 10000;
}

你可能感兴趣的:(聊天室)