Skip to content

TencentCloud/chat-sdk-unity

Repository files navigation


Tencent Chat Logo

Tencent Cloud Chat SDK


Globally interconnected In-App Chat, user profile and relationship chains and offline push.


More languages: 简体中文 한국어 日本語



This document will guide you through the whole process of intergating Tencent Cloud IM SDK into your Unity project.

Environment Requirements

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.

Supported Platforms

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

Prerequisites

  1. You have signed up for a Tencent Cloud account and completed identity verification.
  2. Create an application by the guide Creating and Upgrading an Application, and keep record of your SDKAppID.

Part 1. Create an IM app

  1. 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.

  1. Click Create Application, enter your app name, and click Confirm.
  2. Select Auxiliary Tools > UserSig Generation and Verification on the left sidebar. Create a UserID and the corresponding UserSig, and copy the signature information for login.

Part 2. Intergrate Tencent Cloud IM SDK into your Unity project

  1. Use Unity to create a project, and record the project directory. Or open your preexisted Unity project.

  2. Open the project with an IDE (such as Visual Studio Code):

  3. 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.

Part 3. Load dependencies

Open the project in the Unity Editor, wait until dependencies are loaded, and confirm the Tencent Cloud IM is successfully loaded.

Part 4. Integrate with your own UI

Prerequisites

You have created an Unity project and loaded Tencent Cloud IM SDK.

Initialize 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.

Login

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)=>{

  });
}

Send Message

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
}

Acquire Conversation List

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:

  1. Listen to the conversation event.
  2. 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.

Get conversaion list from API

TIMResult res = TencentIMSDK.ConvGetConvList((int code, string desc, List<ConvInfo> info_list, string user_data)=>{

});

Listen to the conversation updates

TencentIMSDK.SetConvEventCallback((TIMConvEvent conv_event, List<ConvInfo> conv_list, string user_data)=>{

});

Receive Message

Detailed document for the section

There are two ways to receive messages:

  1. Set message receiver listener.
  2. Call API to retrieve messages.

Pulling Historical One-to-One 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
});

Pulling Historical Group Messages

// 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
});

Set listener for receiving new messages

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:

Part 5. Unity for WebGL

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>

FAQs

What platforms are supported?

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.

What should I do if an error occurs during the first run for an iOS device?

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.

What should I do if Unity v2019.04 reports the following error for iOS?

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.

What should I do if Unity v2019.04 reports the following error for iOS?

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".

Contact Us

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.

Our Website: https://www.tencentcloud.com/products/im?from=pub