平台 | 版本 |
Unity | 2019.4.15f1 及以上版本。 |
Android | Android Studio 3.5及以上版本,App 要求 Android 4.1及以上版本设备。 |
iOS | Xcode 11.0及以上版本,请确保您的项目已设置有效的开发者签名。 |
SDKAppID 及 SDKSecretKey 并直接跳转到下一节。


SDKAppID、SDKSecretKey,后续运行 Demo 时需要用到这两个信息。

{"dependencies":{"com.tencent.imsdk.unity":"https://github.com/TencentCloud/chat-sdk-unity.git#unity"}}

public static void Init() {int sdkappid = 0; // 从即时通信 IM 控制台获取应用 SDKAppID。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);}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)=>{// 处理登录回调逻辑});
using com.tencent.imsdk.unity.uikit;// demo 登录逻辑如下public static void Init() {Core.SetConfig(sdkappid, userId, sdkUserSig);Core.Init();Core.Login();}// 若直接跑unity demo,推荐通过 Main 页面的手机号/验证码输入框登录。可以保证监听回调等正常运行。// 由于电脑端unity不支持手机号登录,请将 userid输入到手机号输入框,userSig输入到验证码输入框。// Assets/Example/Scripts/Main.csprivate void Login(){// 将之前的Login函数内容注释掉,添加以下内容loginButton.interactable = false;Core.SetConfig(Config.sdkappid, phoneNumber.text, captcha.text);Core.Init();Core.Login(HandleAfterLogin);loginButton.interactable = true;}

Main.cs(登录界面) 和 Chat.cs(聊天界面)。Prefabs 里的组件构成,可以通过修改 Prefabs 修改显示内容和样式。ConvMessagePanelConversationNamePanelMessageContentPanelActionPanelOverlayPanelCloseButton
Script/Components/Conversation.cs 里。每个会话的样式在 ConversationItem.prefabs 里。FriendPanelSearchPanelConversationListPanel
Script/Components/Chat.cs 里。MessageSenderPanelMessageContentPanelSenderNamePanelMessageSenderIcon 和 TextPanelusing com.tencent.imsdk.unity.uikit;// demo 登录逻辑如下public static void Init() {Core.SetConfig(sdkappid, userId, sdkUserSig);Core.Init();Core.Login();// 可传递函数// Core.Login(HandleAfterLogin);}// 若直接跑unity demo,推荐通过 Main页面的手机号/验证码输入框登录。可以保证监听回调等正常运行。// 由于电脑端unity不支持手机号登录,请将 userid输入到手机号输入框,userSig输入到验证码输入框。// Assets/Example/Scripts/Main.csprivate void Login(){// 将之前的Login函数内容注释掉,添加以下内容loginButton.interactable = false;Core.SetConfig(Config.sdkappid, phoneNumber.text, captcha.text);Core.Init();Core.Login(HandleAfterLogin);loginButton.interactable = true;}
好友 频道显示 C2C 会话和已添加的好友的列表,点击某个会话可开始聊天。Assets/Example/Scripts/Config/Config.cs,将创建的群组的群组 ID 填入 communityID(社群),channelID(频道),groupID(组队)。joinGroup 即可实现登录后进入相应群组,并可以在群内发送消息。UserProfileCustemStringInfo teer = new UserProfileCustemStringInfo{user_profile_custom_string_info_key:"段位",user_profile_custom_string_info_value:"teer"}List<UserProfileCustemStringInfo> customArray = new List<UserProfileCustemStringInfo>();customArray.Add(teer);TencentIMSDK.ProfileModifySelfUserProfile(new UserProfileItem{user_profile_item_custom_string_array:customArray;});
completeConvList 中补充获取到的段位信息。最终显示的好友会话信息在 friendProfiles 列表中。Conversation.cs 中的 GenerateList(会话列表渲染)中修改渲染的图标和头像。Chat.cs 的 RenderMessageForScroll 中获取消息发送者的信息中的段位信息(若需要修改其他显示内容,也可以从这里获取)MsgItem.cs 中修改显示的样式等细节内容。StickerPanel 显示在 Chat.cs 里的 OverlayPanel 中。您可以导入自己的表情包使用。(需要您提前导入自己的表情包)Assets/Resources 文件夹内导入所用的表情包图片:
Texture Type 为 Sprite (2D and UI),并根据图片尺寸修改 Pixels Per Unit:
// 生成表情包列表,StickerPackage 为一组表情包List<StickerPackage> stickers = new List<StickerPackage> {new StickerPackage {name = "4350",baseUrl = "custom_sticker_resource/4350", //Resource 文件夹内相对路径menuItem = new StickerItem { // 表情栏表情项目name = "menu@2x",index = 0,},stickerList = new List<StickerItem> { // 表情包项目组new StickerItem { // 具体表情包数据name = "menu@2x",index = 0 // 表情包顺序},}}};
using com.tencent.imsdk.unity.uikit;Core.SetStickerPackageList(Config.stickers);
Resources/LanguageTxt 里。现在包含 Chinese.txt(简体中文) 和 English.txt(英文)。若需要其他语言,可以添加对应的 txt 文件。//English.txtKey:Value//Chinese.txtKey:值
LanguageDataManager.cs 中的 Language 中添加新的语言,并在 LanguageTextName 中增加对应的词条的 Key。private Dictionary<string,string> EnglishDictionary = new Dictionary<string,string>();LoadLanguageTxt(Language.English);
LanguageUIText(Script) component,将需要显示的词的 Key 选中。改显示的 Key 对应 LanguageTextName 中的 enum 和词汇文件里的 Key。
SetCurrentLanguageValue。若要固定语言,可直接在 LanguageDataManager.cs 对 currentLanguage 赋值(可当成默认语言)。该 Demo 根据系统语言判断并赋值。若需要修改的组件不仅为静态组件,则简单的方法为将现在使用的语言保存到 config 中(在 Demo 中保存到了 Core)在代码中判断显示。sdkappid, userid 以及 usersig。using com.tencent.imsdk.unity.uikit;Core.SetConfig(sdkappid, userid, usersig);
AddRecvNewMsgCallback 和 SetConvEventCallback 回调。using com.tencent.imsdk.unity.uikit;Core.Init();
SetStickerPackageList 设定表情包列表。using com.tencent.imsdk.unity.uikit;Core.SetStickerPackageList(Config.stickers);
Login 登录账号,登录完成后执行绑定的回调函数。using com.tencent.imsdk.unity.uikit;Core.Login((params string[] args) => {// 处理Login回调});
OnMsgListChanged 事件。using com.tencent.imsdk.unity.uikit;Core.SetMessageList(currentConvID, newMsgList, isFinished);
OnCurrentConvChanged 事件。using com.tencent.imsdk.unity.uikit;Core.SetMessageList(convID, convType);
OnCurrentStickerIndexChanged 事件。using com.tencent.imsdk.unity.uikit;Core.SetMessageList(stickerIndex);
using com.tencent.imsdk.unity.uikit;Core.Logout((string[] parameters) => {// 处理Logout回调});
TencentIMSDK 来获取其他即时通信的相关功能。例如通过 TencentIMSDK 来获取用户资料。using com.tencent.imsdk.unity;// 获取个人资料FriendShipGetProfileListParam param = new FriendShipGetProfileListParam{friendship_getprofilelist_param_identifier_array = new List<string>{"self_userid"}};TIMResult res = TencentIMSDK.ProfileGetUserProfileList(param, (int code, string desc, List<UserProfile> profile, string user_data)=>{// 处理异步逻辑});
文档反馈