Advertisement
Guest User

ChatLobby

a guest
Dec 27th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using ExitGames.Client.Photon.Chat;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. namespace Assets.Scripts.Network
  8. {
  9.     public class ChatLobby : MonoBehaviour, IChatClientListener
  10.     {
  11.         [SerializeField] private Text chat;
  12.         [SerializeField] private InputField chatInputField;
  13.         private Text closeOpenText;
  14.  
  15.         private readonly string[] lobbyChanel = { "lobby" };
  16.         private ChatClient chatClient;
  17.  
  18.         private static string messsages;
  19.         private readonly List<string> messageses = new List<string>();
  20.  
  21.         public void Initiate(Text playerName)
  22.         {
  23.             chatClient = new ChatClient(this);
  24.             chatClient.Connect("XXX", NetworkController.gameVersion, new ExitGames.Client.Photon.Chat.AuthenticationValues(playerName.text));
  25.         }
  26.  
  27.         private void Update()
  28.         {
  29.             if (chatClient != null)
  30.                 chatClient.Service();
  31.         }
  32.  
  33.         public void OnDisconnected()
  34.         {
  35.         }
  36.         public void OnConnected()
  37.         {
  38.             print("ChatLobby->OnConnected()");
  39.             chatClient.Subscribe(lobbyChanel);
  40.         }
  41.         public void OnChatStateChange(ChatState state)
  42.         {
  43.         }
  44.         public void OnGetMessages(string channelName, string[] senders, object[] messages)
  45.         {
  46.             if (messageses.Count >= 9)
  47.             {
  48.                 messsages = messsages.Replace(messageses[0], null);
  49.                 messageses.RemoveAt(0);
  50.             }
  51.  
  52.             for (int i = 0; i < senders.Length; i++)
  53.             {
  54.                 messsages += (messageses != null ? "\n" : "") + senders[i] + ": " + messages[i];
  55.                 messageses.Add((messageses != null ? "\n" : "") + senders[i] + ": " + messages[i]);
  56.             }
  57.  
  58.             chat.text = messsages;
  59.         }
  60.         public void OnPrivateMessage(string sender, object message, string channelName)
  61.         {
  62.         }
  63.         public void OnSubscribed(string[] channels, bool[] results)
  64.         {
  65.         }
  66.         public void OnUnsubscribed(string[] channels)
  67.         {
  68.         }
  69.         public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
  70.         {
  71.         }
  72.         public void SendPublicMessage(Text message)
  73.         {
  74.             if (message.text == null || IsNullOrWhiteSpace(message.text))
  75.                 return;
  76.  
  77.             chatClient.PublishMessage(lobbyChanel[0], message.text);
  78.             chatInputField.text = "";
  79.         }
  80.         public void DebugReturn(ExitGames.Client.Photon.DebugLevel level, string message)
  81.         {
  82.         }
  83.  
  84.         #region Chat Window
  85.         public void OpenOrCloseWindow(Animation anim)
  86.         {
  87.             if (!closeOpenText)
  88.                 closeOpenText = anim.transform.FindChild("Window_Image/CloseOpen_Button/Button_Text").GetComponent<Text>();
  89.  
  90.             if (anim.gameObject.GetComponent<RectTransform>().anchoredPosition.y < 0)
  91.             {
  92.                 anim.Play("OpenChatWindow");
  93.                 closeOpenText.text = "CLOSE CHAT";
  94.             }
  95.             else
  96.             {
  97.                 anim.Play("CloseChatWindow");
  98.                 closeOpenText.text = "OPEN CHAT";
  99.             }
  100.         }
  101.         #endregion
  102.  
  103.         private void OnApplicationQuit()
  104.         {
  105.             if (chatClient != null)
  106.                 chatClient.Disconnect();
  107.         }
  108.  
  109.         public static bool IsNullOrWhiteSpace(string value)
  110.         {
  111.             return value == null || value.All(char.IsWhiteSpace);
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement