Advertisement
Guest User

Untitled

a guest
Nov 29th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class ChatMaster : MonoBehaviour {
  6.  
  7. class ChatEntry{
  8. public string name = "";
  9. public string message = "";
  10. public string timeTag = "";
  11. }
  12.  
  13. ArrayList entries;
  14. Vector2 currentScrollPos = new Vector2();
  15. string inputField = "";
  16. bool chatInFocus = false;
  17. string inputFieldFocus = "CIFT";
  18. bool absPos = false;
  19.  
  20. void Awake () {
  21. InitializeChat();
  22. }
  23.  
  24. void InitializeChat(){
  25. entries = new ArrayList();
  26. unfocusChat();
  27. }
  28.  
  29. //draw the chat box in size relative to your GUIlayout
  30. public void Draw(){
  31. ChatWindow();
  32. }
  33.  
  34. void ChatWindow(){
  35. GUILayout.BeginVertical();
  36. currentScrollPos = GUILayout.BeginScrollView(currentScrollPos, GUILayout.MaxWidth(1000), GUILayout.MinWidth(1000)); //limits the chat window size to max 1000x1000, remove the restraints if you want
  37.  
  38. foreach(ChatEntry ent in entries){
  39. GUILayout.BeginHorizontal();
  40. GUI.skin.label.wordWrap = true;
  41. GUILayout.Label(ent.timeTag + " "+ ent.name + ": "+ent.message);
  42. GUILayout.EndHorizontal();
  43. GUILayout.Space(3);
  44. }
  45.  
  46. GUILayout.EndScrollView();
  47. if(chatInFocus){
  48. GUI.SetNextControlName(inputFieldFocus);
  49. inputField = GUILayout.TextField(inputField, GUILayout.MaxWidth(1000), GUILayout.MinWidth(1000));
  50. GUI.FocusControl(inputFieldFocus);
  51. }
  52. GUILayout.EndVertical();
  53.  
  54. if(chatInFocus){
  55. HandleNewEntries();
  56. } else {
  57. checkForInput();
  58. }
  59.  
  60. }
  61.  
  62. void unfocusChat(){
  63. //Debug.Log("unfocusing chat");
  64. inputField = "";
  65. chatInFocus = false;
  66. }
  67.  
  68. void checkForInput(){
  69. if(Event.current.type == EventType.KeyDown Event.current.character == '\n' !chatInFocus){
  70. GUI.FocusControl(inputFieldFocus);
  71. chatInFocus = true;
  72. currentScrollPos.y = float.PositiveInfinity;
  73. }
  74. }
  75.  
  76. void HandleNewEntries(){
  77. if(Event.current.type == EventType.KeyDown Event.current.character == '\n'){
  78. if(inputField.Length <= 0){
  79. unfocusChat();
  80. Debug.Log("unfocusing chat (empty entry)");
  81. return;
  82. }
  83. networkView.RPC ("AddChatEntry", RPCMode.All, "Cookie monster", inputField);
  84. //AddChatEntry("Cookie monster", inputField); //for offline testing
  85. unfocusChat();
  86. //Debug.Log("unfocusing chat and entry sent");
  87. }
  88. }
  89.  
  90. [RPC]
  91. void AddChatEntry(string name, string msg){
  92. ChatEntry newEntry = new ChatEntry();
  93. newEntry.name = name;
  94. newEntry.message = msg;
  95. newEntry.timeTag = "["+System.DateTime.Now.Hour.ToString()+":"+System.DateTime.Now.Minute.ToString()+"]";
  96. entries.Add(newEntry);
  97. currentScrollPos.y = float.PositiveInfinity;
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement