Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- using ExitGames.Client.Photon.Chat;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Assets.Scripts.Network
- {
- public class ChatLobby : MonoBehaviour, IChatClientListener
- {
- [SerializeField] private Text chat;
- [SerializeField] private InputField chatInputField;
- private Text closeOpenText;
- private readonly string[] lobbyChanel = { "lobby" };
- private ChatClient chatClient;
- private static string messsages;
- private readonly List<string> messageses = new List<string>();
- public void Initiate(Text playerName)
- {
- chatClient = new ChatClient(this);
- chatClient.Connect("XXX", NetworkController.gameVersion, new ExitGames.Client.Photon.Chat.AuthenticationValues(playerName.text));
- }
- private void Update()
- {
- if (chatClient != null)
- chatClient.Service();
- }
- public void OnDisconnected()
- {
- }
- public void OnConnected()
- {
- print("ChatLobby->OnConnected()");
- chatClient.Subscribe(lobbyChanel);
- }
- public void OnChatStateChange(ChatState state)
- {
- }
- public void OnGetMessages(string channelName, string[] senders, object[] messages)
- {
- if (messageses.Count >= 9)
- {
- messsages = messsages.Replace(messageses[0], null);
- messageses.RemoveAt(0);
- }
- for (int i = 0; i < senders.Length; i++)
- {
- messsages += (messageses != null ? "\n" : "") + senders[i] + ": " + messages[i];
- messageses.Add((messageses != null ? "\n" : "") + senders[i] + ": " + messages[i]);
- }
- chat.text = messsages;
- }
- public void OnPrivateMessage(string sender, object message, string channelName)
- {
- }
- public void OnSubscribed(string[] channels, bool[] results)
- {
- }
- public void OnUnsubscribed(string[] channels)
- {
- }
- public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
- {
- }
- public void SendPublicMessage(Text message)
- {
- if (message.text == null || IsNullOrWhiteSpace(message.text))
- return;
- chatClient.PublishMessage(lobbyChanel[0], message.text);
- chatInputField.text = "";
- }
- public void DebugReturn(ExitGames.Client.Photon.DebugLevel level, string message)
- {
- }
- #region Chat Window
- public void OpenOrCloseWindow(Animation anim)
- {
- if (!closeOpenText)
- closeOpenText = anim.transform.FindChild("Window_Image/CloseOpen_Button/Button_Text").GetComponent<Text>();
- if (anim.gameObject.GetComponent<RectTransform>().anchoredPosition.y < 0)
- {
- anim.Play("OpenChatWindow");
- closeOpenText.text = "CLOSE CHAT";
- }
- else
- {
- anim.Play("CloseChatWindow");
- closeOpenText.text = "OPEN CHAT";
- }
- }
- #endregion
- private void OnApplicationQuit()
- {
- if (chatClient != null)
- chatClient.Disconnect();
- }
- public static bool IsNullOrWhiteSpace(string value)
- {
- return value == null || value.All(char.IsWhiteSpace);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement