별도로 챗용 에셋을 다운받을 필요없이 PUN API에서도 동작한다
UI Text를 깔고 ContentSizeFitter를 넣는다
PUN코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Chat;
using ExitGames.Client.Photon;
public class PhotonChatTest : MonoBehaviour, IChatClientListener
{
public string serverName = "Default0001";
ChatClient chatClient;
public string userName="TestName";
List<string> messageList = new List<string>();
readonly int messageFromHistory = 10;
public Text currentChannelText;
public void Connect()
{
chatClient = new ChatClient(this);
chatClient.UseBackgroundWorkerForSending = true;
chatClient.Connect(PhotonNetwork.PhotonServerSettings.ChatAppID, Application.version, new Photon.Chat.AuthenticationValues(userName));
Debug.Log("Connect");
}
public void OnApplicationQuit()
{
chatClient.Disconnect();
}
public void AddLine(string message)
{
messageList.Add(message);
if (messageList.Count> messageFromHistory)
{
messageList.RemoveAt(0);
}
currentChannelText.text = string.Join("\n", messageList);
}
public void DebugReturn(DebugLevel level, string message)
{
switch (level)
{
case DebugLevel.ERROR:
Debug.LogError(message);
break;
case DebugLevel.WARNING:
Debug.LogWarning(message);
break;
default:
Debug.Log(message);
break;
}
}
public void OnConnected()
{
Debug.Log("OnConnected");
chatClient.Subscribe(new string[] { serverName }, messageFromHistory);
}
public void OnDisconnected()
{
Debug.Log( "OnDisconnected");
}
public void OnChatStateChange(ChatState state)
{
Debug.Log($"OnChatStateChange {state}");
}
public void OnSubscribed(string[] channels, bool[] results)
{
Debug.Log( $"OnSubscribed : {string.Join(", ", channels)}");
}
public void OnUnsubscribed(string[] channels)
{
Debug.Log( $"OnUnsubscribed : {string.Join(", ", channels)}");
}
public void OnGetMessages(string channelName, string[] senders, object[] messages)
{
for (int i = 0; i < messages.Length; i++)
{
AddLine($"{senders[i]}:{ messages[i]}");
}
}
void Start()
{
Application.runInBackground = true;
if (Application.platform==RuntimePlatform.WindowsEditor)
{
userName += SystemInfo.deviceUniqueIdentifier.Substring(0,4);
}
Connect();
currentChannelText.text = "";
}
void Update()
{
chatClient.Service();
}
public void SendChatMessage(string message)
{
if (string.IsNullOrWhiteSpace(message))
{
return;
}
message = message.Trim();
chatClient.PublishMessage(serverName, message);
}
public void OnUserSubscribed(string channel, string user)
{
Debug.Log($"OnUserSubscribed :channel {channel},user {user}");
}
public void OnUserUnsubscribed(string channel, string user)
{
Debug.Log($"OnDisconnected :channel {channel},user {user}");
}
public void OnPrivateMessage(string sender, object message, string channelName)
{
Debug.Log($"OnDisconnected :sender {sender},message {message},channelName {channelName}");
}
public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
{
Debug.Log($"OnDisconnected :user {user},status {status},gotMessage {gotMessage},message {message}");
}
}
PUN2코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Chat;
using ExitGames.Client.Photon;
public class PhotonChatTest : MonoBehaviour, IChatClientListener
{
public string serverName = "Default0001";
ChatClient chatClient;
public string userName="TestName";
List<string> messageList = new List<string>();
readonly int messagesFromHistory = 10;
public Text currentChannelText;
public void Connect()
{
chatClient = new ChatClient(this);
chatClient.UseBackgroundWorkerForSending = true;
chatClient.Connect(Photon.Pun.PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat, Application.version, new AuthenticationValues(userName));
Debug.Log("Connect");
}
public void OnApplicationQuit()
{
chatClient.Disconnect();
}
public void AddLine(string message)
{
messageList.Add(message);
if (messageList.Count> messagesFromHistory)
{
messageList.RemoveAt(0);
}
currentChannelText.text = string.Join("\n", messageList);
}
public void DebugReturn(DebugLevel level, string message)
{
switch (level)
{
case DebugLevel.ERROR:
Debug.LogError(message);
break;
case DebugLevel.WARNING:
Debug.LogWarning(message);
break;
default:
Debug.Log(message);
break;
}
}
public void OnConnected()
{
Debug.Log("OnConnected");
chatClient.Subscribe(new string[] { serverName }, messagesFromHistory);
}
public void OnDisconnected()
{
Debug.Log( "OnDisconnected");
}
public void OnChatStateChange(ChatState state)
{
Debug.Log($"OnChatStateChange {state}");
}
public void OnSubscribed(string[] channels, bool[] results)
{
Debug.Log( $"OnSubscribed : {string.Join(", ", channels)}");
}
public void OnUnsubscribed(string[] channels)
{
Debug.Log( $"OnUnsubscribed : {string.Join(", ", channels)}");
}
public void OnGetMessages(string channelName, string[] senders, object[] messages)
{
for (int i = 0; i < messages.Length; i++)
{
AddLine($"{senders[i]}:{ messages[i]}");
}
}
void Start()
{
Application.runInBackground = true;
if (Application.platform==RuntimePlatform.WindowsEditor)
{
userName += SystemInfo.deviceUniqueIdentifier.Substring(0,4);
}
Connect();
currentChannelText.text = "";
}
void Update()
{
chatClient.Service();
}
public void SendChatMessage(string message)
{
if (string.IsNullOrWhiteSpace(message))
{
return;
}
message = message.Trim();
chatClient.PublishMessage(serverName, message);
}
public void OnUserSubscribed(string channel, string user)
{
Debug.Log($"OnUserSubscribed :channel {channel},user {user}");
}
public void OnUserUnsubscribed(string channel, string user)
{
Debug.Log($"OnUserUnsubscribed :channel {channel},user {user}");
}
public void OnPrivateMessage(string sender, object message, string channelName)
{
Debug.Log($"OnPrivateMessage :sender {sender},message {message},channelName {channelName}");
}
public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
{
Debug.Log($"OnStatusUpdate :user {user},status {status},gotMessage {gotMessage},message {message}");
}
}
챗 관리 오브젝트를 만들어서 위 코드를 붙여넣고 아까만든 UI Text를 할당한다
InputField를 만들어서 다음과 같이 할당한다
끝
작동중인모습
'Unity > Photon' 카테고리의 다른 글
포톤 보이스 (0) | 2021.07.10 |
---|---|
포톤 볼트 (0) | 2021.07.06 |
포톤 커스텀 플레이어 만들기 (0) | 2021.07.04 |