Globally interconnected In-App Chat, user profile and relationship chains and offline push.
This document will guide you through the whole process of intergating Tencent Cloud IM SDK into your Unity project.
Platform | Version |
---|---|
Unity | 2019.4.15f1 or above |
Android | Android Studio 3.5 or above; devices with Android 4.1 or above for apps |
iOS | Xcode 11.0 or above. Ensure that your project has a valid developer signature. |
We apply ourselves to the development of full platform coverage Tencent Cloud IM SDK in Unity, assisting you to run on any platform with one set of code.
Platform | IM SDK |
---|---|
iOS | Supported |
Android | Supported |
macOS | Supported |
Windows | Supported |
Web | Supported since v.1.8.1 |
? Build for Web needs excessive stpes, please check on Part 5。
- You have signed up for a Tencent Cloud account and completed identity verification.
- Create an application by the guide Creating and Upgrading an Application, and keep record of your
SDKAppID
.
- Log in to the IM console。
?If you already have an app, record its SDKAppID and obtain key information。 A Tencent Cloud account can create a maximum of 300 IM apps. If you want to create a new app, disable and delete an unwanted app first. Once an app (along with its SDKAppID) is deleted, the service it provides and all its data are lost. Proceed with caution.
- Click Create Application, enter your app name, and click Confirm.
- Select Auxiliary Tools > UserSig Generation and Verification on the left sidebar. Create a
UserID
and the correspondingUserSig
, and copy the signature information for login.
-
Use Unity to create a project, and record the project directory. Or open your preexisted Unity project.
-
Find Packages/manifest.json based on the directory, and modify dependencies as follows:
{
"dependencies":{
"com.tencent.imsdk.unity":"https://github.com/TencentCloud/chat-sdk-unity.git#unity"
}
}
For you to better understand all the APIs within Tencent Cloud IM SDK, we have offered API Example for testing SDK APIs and calling certain APIs on the early stage of development.
Open the project in the Unity Editor, wait until dependencies are loaded, and confirm the Tencent Cloud IM is successfully loaded.
You have created an Unity project and loaded Tencent Cloud IM SDK.
Detailed document for the section
Invoke TencentIMSDK.Init
to initialize SDK, by passing your SDKAppID
.
public static void Init() {
int sdkappid = 0; // Get the `SDKAppID` from the IM console
SdkConfig sdkConfig = new SdkConfig();
sdkConfig.sdk_config_config_file_path = Application.persistentDataPath + "/TIM-Config";
sdkConfig.sdk_config_log_file_path = Application.persistentDataPath + "/TIM-Log";
TIMResult res = TencentIMSDK.Init(long.Parse(sdkappid), sdkConfig);
}
After Init
, you are able to add event listeners to listen to network status changes, user information modifications and so on, check more on here.
Detailed document for the section
Now, you can login the testing account created on the web console.
Invoke TencentIMSDK.Login
to login your account.
When return res.code
equals to 0, login succeeded.
public static void Login() {
if (userid == "" || user_sig == "")
{
return;
}
TIMResult res = TencentIMSDK.Login(userid, user_sig, (int code, string desc, string json_param, string user_data)=>{
});
}
Detailed document for the section
Here is the example code for sending text message:
public static void MsgSendMessage() {
string conv_id = ""; // The conversation ID of a one-to-one message is the `userID`, and that of a group message is the `groupID`.
Message message = new Message
{
message_conv_id = conv_id,
message_conv_type = TIMConvType.kTIMConv_C2C, // It is `TIMConvType.kTIMConv_Group` for a group message.
message_elem_array = new List<Elem>
{
new Elem
{
elem_type = TIMElemType.kTIMElem_Text,
text_elem_content = "This is an ordinary text message"
}
}
};
StringBuilder messageId = new StringBuilder(128);
TIMResult res = TencentIMSDK.MsgSendMessage(conv_id, TIMConvType.kTIMConv_C2C, message, messageId, (int code, string desc, string json_param, string user_data)=>{
// Async message sending result
});
// The message ID `messageId` returned when the message is sent
}
Detailed document for the section
In the last step, you have successfully sent testing messages, and now you can login to another account to retrieve the conversation list.
Two ways to acquire conversation list:
- Listen to the conversation event.
- Call API to get conversation list.
Common scene is:
After init the app, retrieve the conversation list immediately, and then listen to the conversation events.
TIMResult res = TencentIMSDK.ConvGetConvList((int code, string desc, List<ConvInfo> info_list, string user_data)=>{
});
TencentIMSDK.SetConvEventCallback((TIMConvEvent conv_event, List<ConvInfo> conv_list, string user_data)=>{
});
Detailed document for the section
There are two ways to receive messages:
- Set message receiver listener.
- Call API to retrieve messages.
// Pull historical one-to-one messages
// Set `msg_getmsglist_param_last_msg` to `null` for the first pull
// `msg_getmsglist_param_last_msg` can be the last message in the returned message list for the second pull.
var get_message_list_param = new MsgGetMsgListParam
{
msg_getmsglist_param_last_msg = LastMessage
};
TIMResult res = TencentIMSDK.MsgGetMsgList(conv_id, TIMConvType.kTIMConv_C2C, get_message_list_param, (int code, string desc, string user_data) => {
// Process the callback logic
});
// Pull historical group messages
// Set `msg_getmsglist_param_last_msg` to `null` for the first pull
// `msg_getmsglist_param_last_msg` can be the last message in the returned message list for the second pull.
var get_message_list_param = new MsgGetMsgListParam
{
msg_getmsglist_param_last_msg = LastMessage
};
TIMResult res = TencentIMSDK.MsgGetMsgList(conv_id, TIMConvType.kTIMConv_Group, get_message_list_param, (int code, string desc, string user_data) => {
// Process the callback logic
});
TencentIMSDK.AddRecvNewMsgCallback((List<Message> message, string user_data) => {
});
Now, you have tested the base functionality of Tencent Cloud IM.
You can further explore the following features:
Tencent Cloud IM SDK (Unity) supports build for WebGL after version 1.8.1
.
After build WebGL, you need to mannually import Web SDK Script to your build folder.
And you need to import those files in your home page index.html
.
<script src="./tim-js.js"></script>
<script src="./tim-js-friendship.js"></script>
<script src="./tim-upload-plugin.js"></script>
iOS, Android, Windows, macOS and WebGL are supported.
What should I do if clicking Build And Run for an Android device triggers an error, stating no available device is found?
Check that the device is not occupied by other resources. Alternatively, click Build to generate an APK package, drag it to the simulator, and run it.
If an error is reported for an iOS device after the demo configured as above is run, choose Product > Clean to clean the product, and build the demo again. You can also close Xcode and open it again, and then build the demo again.
Library/PackageCache/[email protected]/Editor/UserInterface/Bootstrap.cs(23,20): error CS0117: 'Collab' does not contain a definition for 'ShowChangesWindow' Choose Window > Package Manager on the toolbar of the Editor and downgrade Unity Collaborate to 1.2.16.
Library/PackageCache/[email protected]/Scripts/Editor/TMP_PackageUtilities.cs(453,84): error CS0103: The name 'VersionControlSettings' does not exist in the current context
Open the source code and delete the code snippet of || VersionControlSettings.mode != "Visible Meta Files"
.
Please do not hesitate to contact us in the following place, if you have any further questions or tend to learn more about the use cases.
- Telegram Group: https://t.me/+1doS9AUBmndhNGNl
- WhatsApp Group: https://chat.whatsapp.com/Gfbxk7rQBqc8Rz4pzzP27A
- QQ Group: 764231117, chat in Chinese
Our Website: https://www.tencentcloud.com/products/im?from=pub