728x90
https://guide.ncloud-docs.com/docs/gamechat-gamechatunity
1. 채팅 -> 채널 추가
2. 설정 -> 금칙어
3. 설정 -> 사용자 설정에서 관리자
using System;
using System.Collections;
using System.Collections.Generic;
using GameChatUnity;
using UnityEngine;
using UnityEngine.UI;
public class Main : MonoBehaviour
{
private const string PROJECT_ID = "your project id";
private const string CHANNEL_ID = "your project channel id";
[Header("UI")]
[SerializeField] private UGUI_GameChatText m_Item;
[SerializeField] private Transform m_Root;
[SerializeField] private Button m_Button_Send;
[SerializeField] private Button m_Button_Refresh;
[SerializeField] private Text[] m_Text_Info;
//'message' Event에 대한, callback
public delegate void onMessageReceivedCallback(Message message);
public onMessageReceivedCallback onMessageReceived;
//'error' Event에 대한, callback
public delegate void onErrorReceivedCallback(string result, GameChatException exception);
public onErrorReceivedCallback onErrorReceived;
private void Awake()
{
m_Button_Send.onClick.AddListener(OnSend);
m_Button_Refresh.onClick.AddListener(OnRefresh);
GameChat.setRegion("kr");
GameChat.initialize(PROJECT_ID);
GameChat.dispatcher.onErrorReceived += OnErrorReceived;
GameChat.dispatcher.onMessageReceived += OnMessageReceived;
GameChat.connect("neive", (Member user, GameChatException exception) =>
{
// 연결 실패
if (exception != null)
{
Debug.Log(exception.message);
return;
}
// 연결 성공
var Adid = GameChat.getAdid();
m_Text_Info[0].text = $"Adid : {Adid}";
var MemberId = GameChat.getMemberId();
m_Text_Info[1].text = $"MemberId : {MemberId}";
var NickName = GameChat.getNickName();
m_Text_Info[2].text = $"NickName : {NickName}";
var ProfileUrl = GameChat.getProfileUrl();
m_Text_Info[3].text = $"ProfileUrl : {ProfileUrl}";
var Token = GameChat.getToken();
m_Text_Info[4].text = $"Token : {Token}";
var deviceInfo = GameChat.getDeviceInfo();
var deviceModel = deviceInfo.DeviceModel;
m_Text_Info[5].text = $"DeviceModel : {deviceModel}";
var deviceOS = deviceInfo.DeviceOSVersion;
m_Text_Info[6].text = $"DeviceOSVersion : {deviceOS}";
var networkType = deviceInfo.NetworkType;
m_Text_Info[7].text = $"NetworkType : {networkType}";
UpdateInfo();
//GameChat.subscribe(CHANNEL_ID);
// 여기서 이걸 사용하려고 하면
// A WebSocket connection isn't established. 이런 애러가 뜸
});
}
// Start is called before the first frame update
IEnumerator Start()
{
// 바로 GameChat.subscribe(CHANNEL_ID); 을 하려고 하면 애러 떠서 0.5초 딜레이
yield return new WaitForSeconds(0.5f);
OnRefresh();
}
// Update is called once per frame
void Update()
{
}
private void OnMessageReceived(Message message)
{
Add(message);
}
private void OnErrorReceived(string payload, GameChatException exception)
{
Debug.LogError($"Error - {exception.ToJson()}");
}
private void UpdateInfo()
{
GameChat.getSubscriptions(CHANNEL_ID, 0, 1, (List<Subscription> subscriptions, GameChatException exception) =>
{
if (exception != null)
{
Debug.Log(exception.message);
return;
}
foreach (var subscription in subscriptions)
{
Debug.Log($"subscription : {subscription.channel_id}");
}
});
GameChat.getChannels(0, 10, (List<Channel> channels, GameChatException exception) =>
{
if (exception != null)
{
Debug.Log(exception.message);
return;
}
foreach (var channel in channels)
{
Debug.Log($"channel : {channel.id} {channel.name}");
}
});
GameChat.getChannel(CHANNEL_ID, "all", (Channel channel, GameChatException exception) => {
if(exception != null)
{
// Error 핸들링
return;
}
Debug.Log($"channel : {channel.id} {channel.name}");
});
}
void UpdateChat()
{
GameChat.getMessages(CHANNEL_ID, 0, 10, "", "", "desc", (List<Message> messages, GameChatException exception) =>
{
if (exception != null)
{
Debug.Log(exception.message);
return;
}
foreach (var message in messages)
Add(message);
});
}
private void Add(Message message)
{
var item = Instantiate(m_Item, m_Root);
item.SetText(message.content);
item.color = Color.green;
item.isHyperLinked = true;
}
private void OnRefresh()
{
GameChat.subscribe(CHANNEL_ID);
UpdateChat();
}
private void OnSend()
{
GameChat.sendMessage(CHANNEL_ID, "Hello World!");
}
IEnumerator GetTexture(string tex_url, System.Action<Texture> callback)
{
if (!string.IsNullOrEmpty(tex_url))
{
UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequestTexture.GetTexture(tex_url);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
if (callback != null)
callback(null);
}
else
{
Texture myTexture = ((UnityEngine.Networking.DownloadHandlerTexture)www.downloadHandler).texture;
if (callback != null)
callback(myTexture);
}
}
else
{
if (callback != null)
callback(null);
}
}
}
728x90
'프로그래밍 > Unity' 카테고리의 다른 글
유니티 네트워크 상태 얻기 (0) | 2022.08.12 |
---|---|
유니티 - 슬립 모드 (0) | 2022.08.10 |
유니티 콜백 팝업 예제 (0) | 2022.07.18 |
어드레서블 (Addressable) (0) | 2022.07.14 |
구글 Firebase 리얼타임 디비 (0) | 2022.07.08 |
댓글