Advertisement
Boomsma95

Tutorial Script: Chat manager

Mar 26th, 2013
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class ChatManager : MonoBehaviour
  6. {
  7.     public List<ChatMessage> chatMessages = new List<ChatMessage>();
  8.     public bool IsWriting;
  9.     public string currentText;
  10.  
  11.     void Update()
  12.     {
  13.         if (Input.GetKeyDown(KeyCode.T))
  14.         {
  15.             IsWriting = true;
  16.             currentText = "";
  17.         }
  18.     }
  19.  
  20.     void OnGUI()
  21.     {
  22.         if (IsWriting || chatMessages.Count > 0)
  23.         {
  24.             GUILayout.BeginArea(new Rect(10, Screen.height / 2 - 150, 200, 300), "", "box");
  25.  
  26.             foreach (ChatMessage mes in chatMessages)
  27.             {
  28.                 GUILayout.BeginHorizontal();
  29.                 GUILayout.Label(mes.owner.PlayerName);
  30.                 GUILayout.Label(mes.message);
  31.                 GUILayout.EndHorizontal();
  32.             }
  33.  
  34.             GUILayout.EndArea();
  35.         }
  36.         if (IsWriting)
  37.         {
  38.             currentText = GUI.TextField(new Rect(10, Screen.height / 2 + 130, 200, 30), currentText);
  39.             if (Event.current.Equals (Event.KeyboardEvent ("return")))
  40.             {
  41.                 IsWriting = false;
  42.                 networkView.RPC("GetMessage", RPCMode.All, Network.player, currentText);
  43.             }
  44.         }
  45.     }
  46.  
  47.     [RPC]
  48.     void GetMessage(NetworkPlayer player, string Message)
  49.     {
  50.         ChatMessage tempmessage = new ChatMessage();
  51.         tempmessage.owner = MultiplayerManager.GetMPPlayer(player);
  52.         tempmessage.message = Message;
  53.         chatMessages.Add(tempmessage);
  54.         StartCoroutine(RemoveChatMessage(tempmessage));
  55.     }
  56.  
  57.     IEnumerator RemoveChatMessage(ChatMessage mes)
  58.     {
  59.         yield return new WaitForSeconds(7.5f);
  60.         chatMessages.Remove(mes);
  61.     }
  62. }
  63.  
  64. [System.Serializable]
  65. public class ChatMessage
  66. {
  67.     public MPPlayer owner;
  68.     public ChatForPeople forPeople;
  69.     public string message;
  70. }
  71.  
  72. public enum ChatForPeople
  73. {
  74.     Everybody,
  75.     Team,
  76.     SpecificPerson
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement