# Overview
# Unity
The VchatCloud SDK
helps you easily implement a chat client.
Use the VchatCloud SDK
in Unity to enable fast and efficient real-time chat.
The quick guide provides a brief overview of the structure and features of the VchatCloud SDK
,
and each section offers detailed explanations and guides for applying it to real-world projects.
# Enter chat room
# Enter chat room
# Chatting
# Check the implementation code
# Join chatting room
// UnityVChat.cs
public class UnityVChat : MonoBehaviour
{
[SerializeField]
private TextField chatInput;
[SerializeField]
private Button sendButton;
[SerializeField]
private ScrollView chatScrollView;
public string deviceUuid;
private string room_id;
private string nick_name;
private string profile;
private static JObject userInfo = null;
private static Channel channel = null;
private ChannelOptions options;
private async void OnEnable()
{
var root = GetComponent<UIDocument>().rootVisualElement;
chatInput = root.Q<TextField>("chatInput");
sendButton = root.Q<Button>("sendButton");
chatScrollView = root.Q<ScrollView>("chatScrollView");
sendButton.clicked += OnSendButtonClicked;
await ConnectAndJoinRoomAsync();
}
private async Task ConnectAndJoinRoomAsync()
{
room_id = "NpWXoXqzSG-ofk6E5p1xA-20240716140303";
nick_name = "U-User";
profile = DateTime.Now.ToString("HHmmss");
Guid originalGuid = Guid.NewGuid();
deviceUuid = originalGuid.ToString("D").Substring(0, 8);
userInfo = new JObject();
userInfo["profile"] = profile;
options = new ChannelOptions();
options.SetChannelKey(room_id).SetClientKey(deviceUuid).SetNickName(nick_name).SetUserInfo(userInfo);
SocketManager.Instance.OnConnectionOpened += () => ReceiveOpenEventAsync().GetAwaiter();
SocketManager.Instance.OnConnectionFailed += ex => UnityEngine.Debug.Log("Connect fail " + ex.Message);
SocketManager.Instance.OnConnectionClosed += () => UnityEngine.Debug.Log("Connect close");
SocketManager.Instance.OnMessageReceived += async message => await ReceiveJoinMessagesAsync(message);
VChatCloud.GetInstance().SetSocketStatus(SocketSet.CLOSED);
try
{
SocketManager.Instance.Connect(StringSet.SERVER);
}
catch (Exception ex)
{
UnityEngine.Debug.Log(ex.ToString());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71