Join the group (764231117) of the IM Unity SDK
Download the latest SDK, open the Unity editor, and select Assets->import packages->custom package to import the SDK.
Go to the Tencent Cloud IM console to create an application, and get `sdk_app_id` (used for initializing the SDK) and `secret` (used for creating `usersig`).
Go to the console and select `Auxiliary Tools` to generate `userisg` (for a production environment, generate `usersig` in the backend).
Initialize the SDK
// The sdk_app_id applied for in the first step; long sdk_app_id = 0; SdkConfig sdkConfig = new SdkConfig(); // Specify the storage location of the cached data when using the SDK, pay attention to the file read and write Permission sdkConfig.sdk_config_config_file_path = Application.persistentDataPath + "/TIM-Config"; // Specify the storage location of logs when using the SDK, pay attention to the read and write permissions of the file sdkConfig.sdk_config_log_file_path = Application.persistentDataPath + "/TIM -Log"; TIMResult res = TencentIMSDK.Init(sdk_app_id, sdkConfig);
Register global event listener
// For example, register to receive message callback RecvNewMsgCallback(List<Message> message, string user_data){ } TencentIMSDK.AddRecvNewMsgCallback(RecvNewMsgCallback); // Other callback registration, such as session Callbacks, received friend request callbacks, etc., are all registered here
Log in to the SDK
string user_id = ""; string user_sig = ""; callback(int code, string desc, string json_param, string user_data) { } TIMResult res = TencentIMSDK.Login(user_id,user_sig, callback);
Send message
// Sending a C2C text message is demonstrated here string conv_id = "user_id"; Message message = new Message(); message.message_conv_id = conv_id; message.message_conv_type = TIMConvType.kTIMConv_C2C; List<Elem> messageElems = new List<Elem>(); Elem textMessage = new Elem(); textMessage.elem_type = TIMElemType.kTIMElem_Text; textMessage.text_elem_content = "hello"; messageElems.Add(textMessage); message.message_elem_array = messageElems; StringBuilder messageId = new StringBuilder(128); callback(int code, string desc, string json_param, string user_data) { } TIMResult res = TencentIMSDK.MsgSendMessage(conv_id, TIMConvType. kTIMConv_C2C, message, messageId, callback);
FAQs
- When debugging the SDK with the editor, you must call UnInit during the hot update of the code and stop running the code.
- The editor is set as follows for debugging: unity->preferences->general->script change while `playing` is `stop playing and recompile`.
- If `IMSdkForMac.dylib display is destroyed` appears during the debugging with Mac tools, you can run `sudo xattr -r -d com.apple.quarantine ImSDKForMac.dylib`.