MsgSendMessage
功能介绍
发送消息
发送新消息,单聊消息和群消息的发送均采用此接口。
发送单聊消息时 conv_id 为对方的UserID, conv_type 为 kTIMConv_C2C
发送群聊消息时 conv_id 为群ID, conv_type 为 kTIMConv_Group 。
发送消息时不能发送 kTIMElem_GroupTips 、 kTIMElem_GroupReport ,他们由为后台下发,用于更新(通知)群的信息。可以的发送消息内元素
消息类型如下:
文本消息 kTIMElem_Text
自定义消息 kTIMElem_Custom
图片消息 kTIMElem_Image
音频消息 kTIMElem_Sound
视频消息 kTIMElem_Video
文件消息 kTIMElem_File
位置消息 kTIMElem_Location
表情消息 kTIMElem_Face
关系链变更消息元素 kTIMElem_FriendChange
资料变更消息元素 kTIMElem_ProfileChange
群组系统消息元素 kTIMElem_GroupTips
群组系统通知元素 kTIMElem_GroupReport
合并消息元素 kTIMElem_Merge
参数详解
重载1
参数名称 | 参数类型 | 是否必填 | 描述 |
---|---|---|---|
conv_id | string | 是 | 会话ID |
conv_type | TIMConvType | 是 | 会话类型 |
message | Message | 是 | 消息体 |
message_id | StringBuilder | 是 | 承接消息ID的StringBuilder |
callback | ValueCallback< Message > | 是 | 异步回调 返回值的json_param的格式为json |
重载2
参数名称 | 参数类型 | 是否必填 | 描述 |
---|---|---|---|
conv_id | string | 是 | 会话ID |
conv_type | TIMConvType | 是 | 会话类型 |
message | Message | 是 | 消息体 |
message_id | StringBuilder | 是 | 承接消息ID的StringBuilder |
callback | ValueCallback< string > | 是 | 异步回调 返回值的json_param的格式为string |
返回模板
TIMResult
调用成功后回调函数参数:
code:int
desc:string
json_param:
'{
"message_id" : "14400111550110000_1551446728_45598731"
"message_client_time" : 1551446728,
"message_elem_array" : [
{
"elem_type" : 0,
"text_elem_content" : "send text"
}
],
"message_sender" : "user1",
"message_server_time" : 1551446728
}'
user_data:string
返回值详解
名称 | 数值类型 | 描述 |
---|---|---|
TIMResult | TIMResult | 调用接口的返回值 |
code | int | 值为ERR_SUCC表示成功,其他值表示失败。详情请参考 错误码 |
desc | string | 错误描述字符串 |
json_param | string | Json字符串,不同的接口,Json字符串不一样 |
user_data | string | ImSDK负责透传的用户自定义数据,未做任何处理 |
代码示例
SendCustomMessage的案例
using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using System;
using com.tencent.im.unity.demo.utils;
using EasyUI.Toast;
using System.Collections;
using System.Text;
using System.Collections.Generic;
public class SendCustomMessage : MonoBehaviour
{
string[] Labels = new string[] { "DataLabel", "DescLabel", "ExtLabel", "SelectFriendLabel", "SelectGroupLabel", "SelectPriorityLabel", "IsOnlineLabel", "IsUnreadLabel" };
public Text Header;
public InputField InputData;
public InputField InputDesc;
public InputField InputExtension;
public Dropdown SelectedFriend;
public Dropdown SelectedGroup;
public Dropdown SelectedPriority;
public Toggle IsOnline;
public Toggle IsUnread;
public Text Result;
public Button Submit;
public Button Copy;
private List<string> GroupList;
private List<string> FriendList;
void Start()
{
foreach (string label in Labels)
{
GameObject.Find(label).GetComponent<Text>().text = Utils.t(label);
}
GroupGetJoinedGroupListSDK();
FriendshipGetFriendProfileListSDK();
Header = GameObject.Find("HeaderText").GetComponent<Text>();
InputData = GameObject.Find("Data").GetComponent<InputField>();
InputDesc = GameObject.Find("Desc").GetComponent<InputField>();
InputExtension = GameObject.Find("Extension").GetComponent<InputField>();
SelectedFriend = GameObject.Find("Friend").GetComponent<Dropdown>();
SelectedGroup = GameObject.Find("Group").GetComponent<Dropdown>();
SelectedPriority = GameObject.Find("Priority").GetComponent<Dropdown>();
foreach (string name in Enum.GetNames(typeof(TIMMsgPriority)))
{
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = name;
SelectedPriority.options.Add(option);
}
SelectedGroup.onValueChanged.AddListener(delegate
{
GroupDropdownValueChanged(SelectedGroup);
});
SelectedFriend.onValueChanged.AddListener(delegate
{
FriendDropdownValueChanged(SelectedFriend);
});
IsOnline = GameObject.Find("Online").GetComponent<Toggle>();
IsUnread = GameObject.Find("Unread").GetComponent<Toggle>();
Result = GameObject.Find("ResultText").GetComponent<Text>();
Submit = GameObject.Find("Submit").GetComponent<Button>();
Copy = GameObject.Find("Copy").GetComponent<Button>();
Submit.onClick.AddListener(SendCustomMessageSDK);
Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
Copy.onClick.AddListener(CopyText);
if (CurrentSceneInfo.info != null)
{
Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
}
}
void GroupDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedFriend.value = 0;
}
}
void FriendDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedGroup.value = 0;
}
}
void GetGroupList(params object[] parameters)
{
try
{
GroupList = new List<string>();
SelectedGroup.ClearOptions();
string text = (string)parameters[1];
List<GroupBaseInfo> List = Utils.FromJson<List<GroupBaseInfo>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
GroupList.Add("");
option.text = "";
SelectedGroup.options.Add(option);
foreach (GroupBaseInfo item in List)
{
print(item.group_base_info_group_id);
GroupList.Add(item.group_base_info_group_id);
option = new Dropdown.OptionData();
option.text = item.group_base_info_group_id;
SelectedGroup.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupListFailed"));
}
}
void GetFriendList(params object[] parameters)
{
try
{
FriendList = new List<string>();
SelectedFriend.ClearOptions();
string text = (string)parameters[1];
List<FriendProfile> List = Utils.FromJson<List<FriendProfile>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
FriendList.Add("");
option.text = "";
SelectedFriend.options.Add(option);
foreach (FriendProfile item in List)
{
print(item.friend_profile_identifier);
FriendList.Add(item.friend_profile_identifier);
option = new Dropdown.OptionData();
option.text = item.friend_profile_identifier;
SelectedFriend.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getFriendListFailed"));
}
}
void GroupGetJoinedGroupListSDK()
{
TIMResult res = TencentIMSDK.GroupGetJoinedGroupList(Utils.addAsyncStringDataToScreen(GetGroupList));
print($"GroupGetJoinedGroupListSDK {res}");
}
void FriendshipGetFriendProfileListSDK()
{
TIMResult res = TencentIMSDK.FriendshipGetFriendProfileList(Utils.addAsyncStringDataToScreen(GetFriendList));
print($"FriendshipGetFriendProfileListSDK {res}");
}
void SendCustomMessageSDK()
{
var message = new Message
{
message_conv_type = TIMConvType.kTIMConv_Group,
message_cloud_custom_str = "unity local custom data",
message_elem_array = new List<Elem>{new Elem
{
elem_type = TIMElemType.kTIMElem_Custom,
custom_elem_data = InputData.text,
custom_elem_desc = InputDesc.text,
custom_elem_ext = InputExtension.text,
}},
message_need_read_receipt = false,
message_priority = (TIMMsgPriority)SelectedPriority.value,
message_is_excluded_from_unread_count = IsUnread.isOn,
message_is_online_msg = IsOnline.isOn
};
StringBuilder messageId = new StringBuilder(128);
if (SelectedGroup.value > 0)
{
print(GroupList[SelectedGroup.value]);
message.message_conv_id = GroupList[SelectedGroup.value];
message.message_conv_type = TIMConvType.kTIMConv_Group;
TIMResult res = TencentIMSDK.MsgSendMessage(GroupList[SelectedGroup.value], TIMConvType.kTIMConv_Group, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
else if (SelectedFriend.value > 0)
{
print(FriendList[SelectedFriend.value]);
message.message_conv_id = FriendList[SelectedFriend.value];
message.message_conv_type = TIMConvType.kTIMConv_C2C;
TIMResult res = TencentIMSDK.MsgSendMessage(FriendList[SelectedFriend.value], TIMConvType.kTIMConv_C2C, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
print(IsOnline.isOn);
print(IsUnread.isOn);
}
void GetResult(params object[] parameters)
{
Result.text += (string)parameters[0];
}
void CopyText()
{
Utils.Copy(Result.text);
}
void OnApplicationQuit()
{
TencentIMSDK.Uninit();
}
}
SendFaceMessage的案例
using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using System;
using com.tencent.im.unity.demo.utils;
using EasyUI.Toast;
using System.Collections;
using System.Text;
using System.Collections.Generic;
public class SendFaceMessage : MonoBehaviour
{
string[] Labels = new string[] { "FaceIndexLabel", "FaceBufLabel", "SelectFriendLabel", "SelectGroupLabel", "SelectPriorityLabel", "IsOnlineLabel", "IsUnreadLabel", "needReceiptLabel" };
public Text Header;
public InputField FaceIndex;
public InputField FaceBuf;
public Dropdown SelectedFriend;
public Dropdown SelectedGroup;
public Dropdown SelectedPriority;
public Toggle IsOnline;
public Toggle IsUnread;
public Toggle Receipt;
public Text Result;
public Button Submit;
public Button Copy;
private List<string> GroupList;
private List<string> FriendList;
void Start()
{
foreach (string label in Labels)
{
GameObject.Find(label).GetComponent<Text>().text = Utils.t(label);
}
GroupGetJoinedGroupListSDK();
FriendshipGetFriendProfileListSDK();
Header = GameObject.Find("HeaderText").GetComponent<Text>();
FaceIndex = GameObject.Find("FaceIndex").GetComponent<InputField>();
FaceBuf = GameObject.Find("FaceBuf").GetComponent<InputField>();
SelectedFriend = GameObject.Find("Friend").GetComponent<Dropdown>();
SelectedGroup = GameObject.Find("Group").GetComponent<Dropdown>();
SelectedPriority = GameObject.Find("Priority").GetComponent<Dropdown>();
foreach (string name in Enum.GetNames(typeof(TIMMsgPriority)))
{
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = name;
SelectedPriority.options.Add(option);
}
SelectedGroup.onValueChanged.AddListener(delegate
{
GroupDropdownValueChanged(SelectedGroup);
});
SelectedFriend.onValueChanged.AddListener(delegate
{
FriendDropdownValueChanged(SelectedFriend);
});
IsOnline = GameObject.Find("Online").GetComponent<Toggle>();
IsUnread = GameObject.Find("Unread").GetComponent<Toggle>();
Receipt = GameObject.Find("Receipt").GetComponent<Toggle>();
Result = GameObject.Find("ResultText").GetComponent<Text>();
Submit = GameObject.Find("Submit").GetComponent<Button>();
Copy = GameObject.Find("Copy").GetComponent<Button>();
Submit.onClick.AddListener(SendFaceMessageSDK);
Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
Copy.onClick.AddListener(CopyText);
if (CurrentSceneInfo.info != null)
{
Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
}
}
void GroupDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedFriend.value = 0;
}
}
void FriendDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedGroup.value = 0;
}
}
void GetGroupList(params object[] parameters)
{
try
{
GroupList = new List<string>();
SelectedGroup.ClearOptions();
string text = (string)parameters[1];
List<GroupBaseInfo> List = Utils.FromJson<List<GroupBaseInfo>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
GroupList.Add("");
option.text = "";
SelectedGroup.options.Add(option);
foreach (GroupBaseInfo item in List)
{
print(item.group_base_info_group_id);
GroupList.Add(item.group_base_info_group_id);
option = new Dropdown.OptionData();
option.text = item.group_base_info_group_id;
SelectedGroup.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupListFailed"));
}
}
void GetFriendList(params object[] parameters)
{
try
{
FriendList = new List<string>();
SelectedFriend.ClearOptions();
string text = (string)parameters[1];
List<FriendProfile> List = Utils.FromJson<List<FriendProfile>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
FriendList.Add("");
option.text = "";
SelectedFriend.options.Add(option);
foreach (FriendProfile item in List)
{
print(item.friend_profile_identifier);
FriendList.Add(item.friend_profile_identifier);
option = new Dropdown.OptionData();
option.text = item.friend_profile_identifier;
SelectedFriend.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getFriendListFailed"));
}
}
void GroupGetJoinedGroupListSDK()
{
TIMResult res = TencentIMSDK.GroupGetJoinedGroupList(Utils.addAsyncStringDataToScreen(GetGroupList));
print($"GroupGetJoinedGroupListSDK {res}");
}
void FriendshipGetFriendProfileListSDK()
{
TIMResult res = TencentIMSDK.FriendshipGetFriendProfileList(Utils.addAsyncStringDataToScreen(GetFriendList));
print($"FriendshipGetFriendProfileListSDK {res}");
}
void SendFaceMessageSDK()
{
int index;
bool isNumber = int.TryParse(FaceIndex.text, out index);
if (!isNumber)
{
return;
}
var message = new Message
{
message_conv_type = TIMConvType.kTIMConv_Group,
message_cloud_custom_str = "unity local face data",
message_elem_array = new List<Elem>{new Elem
{
elem_type = TIMElemType.kTIMElem_Face,
face_elem_index = index,
face_elem_buf = FaceBuf.text,
}},
message_need_read_receipt = Receipt.isOn,
message_priority = (TIMMsgPriority)SelectedPriority.value,
message_is_excluded_from_unread_count = IsUnread.isOn,
message_is_online_msg = IsOnline.isOn
};
StringBuilder messageId = new StringBuilder(128);
if (SelectedGroup.value > 0)
{
print(GroupList[SelectedGroup.value]);
message.message_conv_id = GroupList[SelectedGroup.value];
message.message_conv_type = TIMConvType.kTIMConv_Group;
TIMResult res = TencentIMSDK.MsgSendMessage(GroupList[SelectedGroup.value], TIMConvType.kTIMConv_Group, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
else if (SelectedFriend.value > 0)
{
print(FriendList[SelectedFriend.value]);
message.message_conv_id = FriendList[SelectedFriend.value];
message.message_conv_type = TIMConvType.kTIMConv_C2C;
TIMResult res = TencentIMSDK.MsgSendMessage(FriendList[SelectedFriend.value], TIMConvType.kTIMConv_C2C, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
print(IsOnline.isOn);
print(IsUnread.isOn);
}
void GetResult(params object[] parameters)
{
Result.text += (string)parameters[0];
}
void CopyText()
{
Utils.Copy(Result.text);
}
void OnApplicationQuit()
{
TencentIMSDK.Uninit();
}
}
SendFileMessage的案例
using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using System;
using com.tencent.im.unity.demo.utils;
using EasyUI.Toast;
using System.Collections;
using System.Text;
using System.Collections.Generic;
public class SendFileMessage : MonoBehaviour
{
string[] Labels = new string[] {"FileMessageLabel", "SelectFriendLabel", "SelectGroupLabel", "SelectPriorityLabel", "IsOnlineLabel", "IsUnreadLabel"};
public Text Header;
public Button PickFileButton;
public Text PathText;
public Dropdown SelectedFriend;
public Dropdown SelectedGroup;
public Dropdown SelectedPriority;
public Toggle IsOnline;
public Toggle IsUnread;
public Text Result;
public Button Submit;
public Button Copy;
private List<string> GroupList;
private List<string> FriendList;
private string path;
void Start()
{
foreach (string label in Labels)
{
GameObject.Find(label).GetComponent<Text>().text = Utils.t(label);
}
GroupGetJoinedGroupListSDK();
FriendshipGetFriendProfileListSDK();
Header = GameObject.Find("HeaderText").GetComponent<Text>();
PickFileButton = GameObject.Find("PickFileButton").GetComponent<Button>();
PickFileButton.onClick.AddListener(OnPickFile);
PathText = GameObject.Find("Path").GetComponent<Text>();
SelectedFriend = GameObject.Find("Friend").GetComponent<Dropdown>();
SelectedGroup = GameObject.Find("Group").GetComponent<Dropdown>();
SelectedPriority = GameObject.Find("Priority").GetComponent<Dropdown>();
foreach (string name in Enum.GetNames(typeof(TIMMsgPriority)))
{
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = name;
SelectedPriority.options.Add(option);
}
SelectedGroup.onValueChanged.AddListener(delegate
{
GroupDropdownValueChanged(SelectedGroup);
});
SelectedFriend.onValueChanged.AddListener(delegate
{
FriendDropdownValueChanged(SelectedFriend);
});
IsOnline = GameObject.Find("Online").GetComponent<Toggle>();
IsUnread = GameObject.Find("Unread").GetComponent<Toggle>();
Result = GameObject.Find("ResultText").GetComponent<Text>();
Submit = GameObject.Find("Submit").GetComponent<Button>();
Copy = GameObject.Find("Copy").GetComponent<Button>();
Submit.onClick.AddListener(SendFileMessageSDK);
Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
Copy.onClick.AddListener(CopyText);
if (CurrentSceneInfo.info != null)
{
Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
}
}
public void OnPickFile()
{
Utils.PickFile((string pathStr) => {
path = pathStr;
PathText.text = pathStr;
});
}
void GroupDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedFriend.value = 0;
}
}
void FriendDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedGroup.value = 0;
}
}
void GetGroupList(params object[] parameters)
{
try
{
GroupList = new List<string>();
SelectedGroup.ClearOptions();
string text = (string)parameters[1];
List<GroupBaseInfo> List = Utils.FromJson<List<GroupBaseInfo>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
GroupList.Add("");
option.text = "";
SelectedGroup.options.Add(option);
foreach (GroupBaseInfo item in List)
{
print(item.group_base_info_group_id);
GroupList.Add(item.group_base_info_group_id);
option = new Dropdown.OptionData();
option.text = item.group_base_info_group_id;
SelectedGroup.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupListFailed"));
}
}
void GetFriendList(params object[] parameters)
{
try
{
FriendList = new List<string>();
SelectedFriend.ClearOptions();
string text = (string)parameters[1];
List<FriendProfile> List = Utils.FromJson<List<FriendProfile>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
FriendList.Add("");
option.text = "";
SelectedFriend.options.Add(option);
foreach (FriendProfile item in List)
{
print(item.friend_profile_identifier);
FriendList.Add(item.friend_profile_identifier);
option = new Dropdown.OptionData();
option.text = item.friend_profile_identifier;
SelectedFriend.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getFriendListFailed"));
}
}
void GroupGetJoinedGroupListSDK()
{
TIMResult res = TencentIMSDK.GroupGetJoinedGroupList(Utils.addAsyncStringDataToScreen(GetGroupList));
print($"GroupGetJoinedGroupListSDK {res}");
}
void FriendshipGetFriendProfileListSDK()
{
TIMResult res = TencentIMSDK.FriendshipGetFriendProfileList(Utils.addAsyncStringDataToScreen(GetFriendList));
print($"FriendshipGetFriendProfileListSDK {res}");
}
void SendFileMessageSDK()
{
var message = new Message
{
message_cloud_custom_str = "unity local file data",
message_elem_array = new List<Elem>{new Elem
{
elem_type = TIMElemType.kTIMElem_File,
file_elem_file_path = path,
}},
message_need_read_receipt = false,
message_priority = (TIMMsgPriority)SelectedPriority.value,
message_is_excluded_from_unread_count = IsUnread.isOn,
message_is_online_msg = IsOnline.isOn
};
StringBuilder messageId = new StringBuilder(128);
if (SelectedGroup.value > 0)
{
print(GroupList[SelectedGroup.value]);
message.message_conv_id = GroupList[SelectedGroup.value];
message.message_conv_type = TIMConvType.kTIMConv_Group;
TIMResult res = TencentIMSDK.MsgSendMessage(GroupList[SelectedGroup.value], TIMConvType.kTIMConv_Group, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
else if (SelectedFriend.value > 0)
{
print(FriendList[SelectedFriend.value]);
message.message_conv_id = FriendList[SelectedFriend.value];
message.message_conv_type = TIMConvType.kTIMConv_C2C;
TIMResult res = TencentIMSDK.MsgSendMessage(FriendList[SelectedFriend.value], TIMConvType.kTIMConv_C2C, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
print(IsOnline.isOn);
print(IsUnread.isOn);
}
void GetResult(params object[] parameters)
{
Result.text += (string)parameters[0];
}
void CopyText()
{
Utils.Copy(Result.text);
}
void OnApplicationQuit()
{
TencentIMSDK.Uninit();
}
}
SendForwardMessage的案例
using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using System;
using com.tencent.im.unity.demo.utils;
using EasyUI.Toast;
using System.Collections;
using System.Text;
using System.Collections.Generic;
public class SendForwardMessage : MonoBehaviour
{
string[] Labels = new string[] { "SelectConvLabel", "SelectMsgLabel", "SelectFriendLabel", "SelectGroupLabel", "SelectPriorityLabel", "IsOnlineLabel", "IsUnreadLabel", "needReceiptLabel" };
public Text Header;
public Dropdown SelectedConv;
public Dropdown SelectedMsg;
public Dropdown SelectedFriend;
public Dropdown SelectedGroup;
public Dropdown SelectedPriority;
public Toggle IsOnline;
public Toggle IsUnread;
public Toggle Receipt;
public Text Result;
public Button Submit;
public Button Copy;
private List<string> GroupList;
private List<string> FriendList;
private List<ConvInfo> ConvList;
private List<Message> MsgList;
void Start()
{
foreach (string label in Labels)
{
GameObject.Find(label).GetComponent<Text>().text = Utils.t(label);
}
GroupGetJoinedGroupListSDK();
FriendshipGetFriendProfileListSDK();
Header = GameObject.Find("HeaderText").GetComponent<Text>();
SelectedConv = GameObject.Find("ConvDropdown").GetComponent<Dropdown>();
SelectedMsg = GameObject.Find("MsgDropdown").GetComponent<Dropdown>();
SelectedConv.interactable = true;
SelectedConv.onValueChanged.AddListener(delegate
{
ConvDropdownValueChanged(SelectedConv);
});
SelectedFriend = GameObject.Find("Friend").GetComponent<Dropdown>();
SelectedGroup = GameObject.Find("Group").GetComponent<Dropdown>();
SelectedPriority = GameObject.Find("Priority").GetComponent<Dropdown>();
foreach (string name in Enum.GetNames(typeof(TIMMsgPriority)))
{
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = name;
SelectedPriority.options.Add(option);
}
SelectedGroup.onValueChanged.AddListener(delegate
{
GroupDropdownValueChanged(SelectedGroup);
});
SelectedFriend.onValueChanged.AddListener(delegate
{
FriendDropdownValueChanged(SelectedFriend);
});
IsOnline = GameObject.Find("Online").GetComponent<Toggle>();
IsUnread = GameObject.Find("Unread").GetComponent<Toggle>();
Receipt = GameObject.Find("Receipt").GetComponent<Toggle>();
Result = GameObject.Find("ResultText").GetComponent<Text>();
Submit = GameObject.Find("Submit").GetComponent<Button>();
Copy = GameObject.Find("Copy").GetComponent<Button>();
Submit.onClick.AddListener(SendForwardMessageSDK);
Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
Copy.onClick.AddListener(CopyText);
if (CurrentSceneInfo.info != null)
{
Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
}
ConvGetConvListSDK();
}
void MsgGetMsgListSDK(string conv_id, TIMConvType conv_type)
{
var get_message_list_param = new MsgGetMsgListParam
{
msg_getmsglist_param_count = 20
};
print(conv_id + conv_type);
TIMResult res = TencentIMSDK.MsgGetMsgList(conv_id, conv_type, get_message_list_param, Utils.addAsyncStringDataToScreen(GetMsgList));
}
void ConvDropdownValueChanged(Dropdown change)
{
SelectedMsg.captionText.text = "";
SelectedMsg.ClearOptions();
SelectedMsg.value = 0;
MsgList = new List<Message>();
if (ConvList.Count > 0)
{
string conv_id = ConvList[change.value].conv_id;
TIMConvType conv_type = ConvList[change.value].conv_type;
MsgGetMsgListSDK(conv_id, conv_type);
}
}
void GetMsgList(params object[] parameters)
{
try
{
string text = (string)parameters[1];
List<Message> ListRes = Utils.FromJson<List<Message>>(text);
foreach (Message item in ListRes)
{
print(item.message_msg_id);
MsgList.Add(item);
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = item.message_msg_id;
SelectedMsg.options.Add(option);
}
if (ListRes.Count > 0)
{
SelectedMsg.captionText.text = ListRes[SelectedMsg.value].message_msg_id;
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getMsgListFailed"));
}
}
void GetConvList(params object[] parameters)
{
try
{
ConvList = new List<ConvInfo>();
SelectedConv.ClearOptions();
string text = (string)parameters[1];
List<ConvInfo> List = Utils.FromJson<List<ConvInfo>>(text);
foreach (ConvInfo item in List)
{
print(item.conv_id);
ConvList.Add(item);
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = item.conv_id;
SelectedConv.options.Add(option);
}
if (List.Count > 0)
{
SelectedConv.captionText.text = List[SelectedConv.value].conv_id;
ConvDropdownValueChanged(SelectedConv);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getConvListFailed"));
}
}
void ConvGetConvListSDK()
{
TIMResult res = TencentIMSDK.ConvGetConvList(Utils.addAsyncStringDataToScreen(GetConvList));
print($"ConvGetConvListSDK {res}");
}
void GroupDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedFriend.value = 0;
}
}
void FriendDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedGroup.value = 0;
}
}
void GetGroupList(params object[] parameters)
{
try
{
GroupList = new List<string>();
SelectedGroup.ClearOptions();
string text = (string)parameters[1];
List<GroupBaseInfo> List = Utils.FromJson<List<GroupBaseInfo>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
GroupList.Add("");
option.text = "";
SelectedGroup.options.Add(option);
foreach (GroupBaseInfo item in List)
{
print(item.group_base_info_group_id);
GroupList.Add(item.group_base_info_group_id);
option = new Dropdown.OptionData();
option.text = item.group_base_info_group_id;
SelectedGroup.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupListFailed"));
}
}
void GetFriendList(params object[] parameters)
{
try
{
FriendList = new List<string>();
SelectedFriend.ClearOptions();
string text = (string)parameters[1];
List<FriendProfile> List = Utils.FromJson<List<FriendProfile>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
FriendList.Add("");
option.text = "";
SelectedFriend.options.Add(option);
foreach (FriendProfile item in List)
{
print(item.friend_profile_identifier);
FriendList.Add(item.friend_profile_identifier);
option = new Dropdown.OptionData();
option.text = item.friend_profile_identifier;
SelectedFriend.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getFriendListFailed"));
}
}
void GroupGetJoinedGroupListSDK()
{
TIMResult res = TencentIMSDK.GroupGetJoinedGroupList(Utils.addAsyncStringDataToScreen(GetGroupList));
print($"GroupGetJoinedGroupListSDK {res}");
}
void FriendshipGetFriendProfileListSDK()
{
TIMResult res = TencentIMSDK.FriendshipGetFriendProfileList(Utils.addAsyncStringDataToScreen(GetFriendList));
print($"FriendshipGetFriendProfileListSDK {res}");
}
void SendForwardMessageSDK()
{
if (ConvList.Count < 1 || MsgList.Count < 1) return;
Message targetMsg = MsgList[SelectedMsg.value];
var message = new Message
{
message_conv_type = TIMConvType.kTIMConv_Group,
message_cloud_custom_str = "unity local forward data",
message_elem_array = targetMsg.message_elem_array,
message_need_read_receipt = Receipt.isOn,
message_priority = (TIMMsgPriority)SelectedPriority.value,
message_is_excluded_from_unread_count = IsUnread.isOn,
message_is_online_msg = IsOnline.isOn
};
StringBuilder messageId = new StringBuilder(128);
if (SelectedGroup.value > 0)
{
print(GroupList[SelectedGroup.value]);
message.message_conv_id = GroupList[SelectedGroup.value];
message.message_conv_type = TIMConvType.kTIMConv_Group;
TIMResult res = TencentIMSDK.MsgSendMessage(GroupList[SelectedGroup.value], TIMConvType.kTIMConv_Group, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
else if (SelectedFriend.value > 0)
{
print(FriendList[SelectedFriend.value]);
message.message_conv_id = FriendList[SelectedFriend.value];
message.message_conv_type = TIMConvType.kTIMConv_C2C;
TIMResult res = TencentIMSDK.MsgSendMessage(FriendList[SelectedFriend.value], TIMConvType.kTIMConv_C2C, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
print(IsOnline.isOn);
print(IsUnread.isOn);
}
void GetResult(params object[] parameters)
{
Result.text += (string)parameters[0];
}
void CopyText()
{
Utils.Copy(Result.text);
}
void OnApplicationQuit()
{
TencentIMSDK.Uninit();
}
}
SendImageMessage的案例
using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using System;
using com.tencent.im.unity.demo.utils;
using EasyUI.Toast;
using System.Collections;
using System.Text;
using System.Collections.Generic;
public class SendImageMessage : MonoBehaviour
{
string[] Labels = new string[] {"ImageMessageLabel", "SelectFriendLabel", "SelectGroupLabel", "SelectPriorityLabel", "IsOnlineLabel", "IsUnreadLabel"};
public Text Header;
public Button PickFileButton;
public Text PathText;
public Dropdown SelectedFriend;
public Dropdown SelectedGroup;
public Dropdown SelectedPriority;
public Toggle IsOnline;
public Toggle IsUnread;
public Text Result;
public Button Submit;
public Button Copy;
private List<string> GroupList;
private List<string> FriendList;
private string path;
void Start()
{
foreach (string label in Labels)
{
GameObject.Find(label).GetComponent<Text>().text = Utils.t(label);
}
GroupGetJoinedGroupListSDK();
FriendshipGetFriendProfileListSDK();
Header = GameObject.Find("HeaderText").GetComponent<Text>();
PickFileButton = GameObject.Find("PickFileButton").GetComponent<Button>();
PickFileButton.onClick.AddListener(OnPickFile);
PathText = GameObject.Find("Path").GetComponent<Text>();
SelectedFriend = GameObject.Find("Friend").GetComponent<Dropdown>();
SelectedGroup = GameObject.Find("Group").GetComponent<Dropdown>();
SelectedPriority = GameObject.Find("Priority").GetComponent<Dropdown>();
foreach (string name in Enum.GetNames(typeof(TIMMsgPriority)))
{
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = name;
SelectedPriority.options.Add(option);
}
SelectedGroup.onValueChanged.AddListener(delegate
{
GroupDropdownValueChanged(SelectedGroup);
});
SelectedFriend.onValueChanged.AddListener(delegate
{
FriendDropdownValueChanged(SelectedFriend);
});
IsOnline = GameObject.Find("Online").GetComponent<Toggle>();
IsUnread = GameObject.Find("Unread").GetComponent<Toggle>();
Result = GameObject.Find("ResultText").GetComponent<Text>();
Submit = GameObject.Find("Submit").GetComponent<Button>();
Copy = GameObject.Find("Copy").GetComponent<Button>();
Submit.onClick.AddListener(SendImageMessageSDK);
Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
Copy.onClick.AddListener(CopyText);
if (CurrentSceneInfo.info != null)
{
Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
}
}
public void OnPickFile()
{
Utils.PickImage((string pathStr) => {
path = pathStr;
PathText.text = pathStr;
});
}
void GroupDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedFriend.value = 0;
}
}
void FriendDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedGroup.value = 0;
}
}
void GetGroupList(params object[] parameters)
{
try
{
GroupList = new List<string>();
SelectedGroup.ClearOptions();
string text = (string)parameters[1];
List<GroupBaseInfo> List = Utils.FromJson<List<GroupBaseInfo>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
GroupList.Add("");
option.text = "";
SelectedGroup.options.Add(option);
foreach (GroupBaseInfo item in List)
{
print(item.group_base_info_group_id);
GroupList.Add(item.group_base_info_group_id);
option = new Dropdown.OptionData();
option.text = item.group_base_info_group_id;
SelectedGroup.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupListFailed"));
}
}
void GetFriendList(params object[] parameters)
{
try
{
FriendList = new List<string>();
SelectedFriend.ClearOptions();
string text = (string)parameters[1];
List<FriendProfile> List = Utils.FromJson<List<FriendProfile>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
FriendList.Add("");
option.text = "";
SelectedFriend.options.Add(option);
foreach (FriendProfile item in List)
{
print(item.friend_profile_identifier);
FriendList.Add(item.friend_profile_identifier);
option = new Dropdown.OptionData();
option.text = item.friend_profile_identifier;
SelectedFriend.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getFriendListFailed"));
}
}
void GroupGetJoinedGroupListSDK()
{
TIMResult res = TencentIMSDK.GroupGetJoinedGroupList(Utils.addAsyncStringDataToScreen(GetGroupList));
print($"GroupGetJoinedGroupListSDK {res}");
}
void FriendshipGetFriendProfileListSDK()
{
TIMResult res = TencentIMSDK.FriendshipGetFriendProfileList(Utils.addAsyncStringDataToScreen(GetFriendList));
print($"FriendshipGetFriendProfileListSDK {res}");
}
void SendImageMessageSDK()
{
var message = new Message
{
message_cloud_custom_str = "unity local image data",
message_elem_array = new List<Elem>{new Elem
{
elem_type = TIMElemType.kTIMElem_Image,
image_elem_orig_path = path,
image_elem_level = TIMImageLevel.kTIMImageLevel_Orig // 原图发送
}},
message_need_read_receipt = false,
message_priority = (TIMMsgPriority)SelectedPriority.value,
message_is_excluded_from_unread_count = IsUnread.isOn,
message_is_online_msg = IsOnline.isOn
};
StringBuilder messageId = new StringBuilder(128);
if (SelectedGroup.value > 0)
{
print(GroupList[SelectedGroup.value]);
message.message_conv_id = GroupList[SelectedGroup.value];
message.message_conv_type = TIMConvType.kTIMConv_Group;
TIMResult res = TencentIMSDK.MsgSendMessage(GroupList[SelectedGroup.value], TIMConvType.kTIMConv_Group, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
else if (SelectedFriend.value > 0)
{
print(FriendList[SelectedFriend.value]);
message.message_conv_id = FriendList[SelectedFriend.value];
message.message_conv_type = TIMConvType.kTIMConv_C2C;
TIMResult res = TencentIMSDK.MsgSendMessage(FriendList[SelectedFriend.value], TIMConvType.kTIMConv_C2C, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
print(IsOnline.isOn);
print(IsUnread.isOn);
}
void GetResult(params object[] parameters)
{
Result.text += (string)parameters[0];
}
void CopyText()
{
Utils.Copy(Result.text);
}
void OnApplicationQuit()
{
TencentIMSDK.Uninit();
}
}
SendLocationMessage的案例
using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using System;
using com.tencent.im.unity.demo.utils;
using EasyUI.Toast;
using System.Collections;
using System.Text;
using System.Collections.Generic;
public class SendLocationMessage : MonoBehaviour
{
string[] Labels = new string[] { "LongtitudeLabel", "LatitudeLabel", "LocationDescLabel", "SelectFriendLabel", "SelectGroupLabel", "SelectPriorityLabel", "IsOnlineLabel", "IsUnreadLabel", "needReceiptLabel" };
public Text Header;
public InputField Longtitude;
public InputField Latitude;
public InputField Desc;
public Dropdown SelectedFriend;
public Dropdown SelectedGroup;
public Dropdown SelectedPriority;
public Toggle IsOnline;
public Toggle IsUnread;
public Toggle Receipt;
public Text Result;
public Button Submit;
public Button Copy;
private List<string> GroupList;
private List<string> FriendList;
void Start()
{
foreach (string label in Labels)
{
GameObject.Find(label).GetComponent<Text>().text = Utils.t(label);
}
GroupGetJoinedGroupListSDK();
FriendshipGetFriendProfileListSDK();
Header = GameObject.Find("HeaderText").GetComponent<Text>();
Longtitude = GameObject.Find("Longtitude").GetComponent<InputField>();
Latitude = GameObject.Find("Latitude").GetComponent<InputField>();
Desc = GameObject.Find("LocationDesc").GetComponent<InputField>();
SelectedFriend = GameObject.Find("Friend").GetComponent<Dropdown>();
SelectedGroup = GameObject.Find("Group").GetComponent<Dropdown>();
SelectedPriority = GameObject.Find("Priority").GetComponent<Dropdown>();
foreach (string name in Enum.GetNames(typeof(TIMMsgPriority)))
{
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = name;
SelectedPriority.options.Add(option);
}
SelectedGroup.onValueChanged.AddListener(delegate
{
GroupDropdownValueChanged(SelectedGroup);
});
SelectedFriend.onValueChanged.AddListener(delegate
{
FriendDropdownValueChanged(SelectedFriend);
});
IsOnline = GameObject.Find("Online").GetComponent<Toggle>();
IsUnread = GameObject.Find("Unread").GetComponent<Toggle>();
Receipt = GameObject.Find("Receipt").GetComponent<Toggle>();
Result = GameObject.Find("ResultText").GetComponent<Text>();
Submit = GameObject.Find("Submit").GetComponent<Button>();
Copy = GameObject.Find("Copy").GetComponent<Button>();
Submit.onClick.AddListener(SendLocationMessageSDK);
Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
Copy.onClick.AddListener(CopyText);
if (CurrentSceneInfo.info != null)
{
Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
}
}
void GroupDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedFriend.value = 0;
}
}
void FriendDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedGroup.value = 0;
}
}
void GetGroupList(params object[] parameters)
{
try
{
GroupList = new List<string>();
SelectedGroup.ClearOptions();
string text = (string)parameters[1];
List<GroupBaseInfo> List = Utils.FromJson<List<GroupBaseInfo>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
GroupList.Add("");
option.text = "";
SelectedGroup.options.Add(option);
foreach (GroupBaseInfo item in List)
{
print(item.group_base_info_group_id);
GroupList.Add(item.group_base_info_group_id);
option = new Dropdown.OptionData();
option.text = item.group_base_info_group_id;
SelectedGroup.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupListFailed"));
}
}
void GetFriendList(params object[] parameters)
{
try
{
FriendList = new List<string>();
SelectedFriend.ClearOptions();
string text = (string)parameters[1];
List<FriendProfile> List = Utils.FromJson<List<FriendProfile>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
FriendList.Add("");
option.text = "";
SelectedFriend.options.Add(option);
foreach (FriendProfile item in List)
{
print(item.friend_profile_identifier);
FriendList.Add(item.friend_profile_identifier);
option = new Dropdown.OptionData();
option.text = item.friend_profile_identifier;
SelectedFriend.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getFriendListFailed"));
}
}
void GroupGetJoinedGroupListSDK()
{
TIMResult res = TencentIMSDK.GroupGetJoinedGroupList(Utils.addAsyncStringDataToScreen(GetGroupList));
print($"GroupGetJoinedGroupListSDK {res}");
}
void FriendshipGetFriendProfileListSDK()
{
TIMResult res = TencentIMSDK.FriendshipGetFriendProfileList(Utils.addAsyncStringDataToScreen(GetFriendList));
print($"FriendshipGetFriendProfileListSDK {res}");
}
void SendLocationMessageSDK()
{
double latitude;
double lontitude;
bool isLatNumber = double.TryParse(Latitude.text, out latitude);
bool isLongNumber = double.TryParse(Longtitude.text, out lontitude);
if (!isLatNumber || !isLongNumber)
{
return;
}
var message = new Message
{
message_conv_type = TIMConvType.kTIMConv_Group,
message_cloud_custom_str = "unity local face data",
message_elem_array = new List<Elem>{new Elem
{
elem_type = TIMElemType.kTIMElem_Location,
location_elem_latitude = latitude,
location_elem_longitude = lontitude,
location_elem_desc = Desc.text,
}},
message_need_read_receipt = Receipt.isOn,
message_priority = (TIMMsgPriority)SelectedPriority.value,
message_is_excluded_from_unread_count = IsUnread.isOn,
message_is_online_msg = IsOnline.isOn
};
StringBuilder messageId = new StringBuilder(128);
if (SelectedGroup.value > 0)
{
print(GroupList[SelectedGroup.value]);
message.message_conv_id = GroupList[SelectedGroup.value];
message.message_conv_type = TIMConvType.kTIMConv_Group;
TIMResult res = TencentIMSDK.MsgSendMessage(GroupList[SelectedGroup.value], TIMConvType.kTIMConv_Group, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
else if (SelectedFriend.value > 0)
{
print(FriendList[SelectedFriend.value]);
message.message_conv_id = FriendList[SelectedFriend.value];
message.message_conv_type = TIMConvType.kTIMConv_C2C;
TIMResult res = TencentIMSDK.MsgSendMessage(FriendList[SelectedFriend.value], TIMConvType.kTIMConv_C2C, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
print(IsOnline.isOn);
print(IsUnread.isOn);
}
void GetResult(params object[] parameters)
{
Result.text += (string)parameters[0];
}
void CopyText()
{
Utils.Copy(Result.text);
}
void OnApplicationQuit()
{
TencentIMSDK.Uninit();
}
}
SendMergerMessage的案例
using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using System;
using com.tencent.im.unity.demo.utils;
using EasyUI.Toast;
using System.Collections;
using System.Text;
using System.Collections.Generic;
public class SendMergerMessage : MonoBehaviour
{
string[] Labels = new string[] { "SelectConvLabel", "SelectMsgLabel", "SelectFriendLabel", "SelectGroupLabel", "SelectPriorityLabel", "IsOnlineLabel", "IsUnreadLabel", "needReceiptLabel" };
public Text Header;
public Dropdown SelectedConv;
public Dropdown SelectedFriend;
public Dropdown SelectedGroup;
public Dropdown SelectedPriority;
public Toggle IsOnline;
public Toggle IsUnread;
public Toggle Receipt;
public Text Result;
public Button Submit;
public Button Copy;
private List<string> GroupList;
private List<string> FriendList;
private List<ConvInfo> ConvList;
private List<Message> MsgList;
public HashSet<string> SelectedMsg;
void Start()
{
foreach (string label in Labels)
{
GameObject.Find(label).GetComponent<Text>().text = Utils.t(label);
}
GroupGetJoinedGroupListSDK();
FriendshipGetFriendProfileListSDK();
Header = GameObject.Find("HeaderText").GetComponent<Text>();
SelectedConv = GameObject.Find("ConvDropdown").GetComponent<Dropdown>();
SelectedConv.interactable = true;
SelectedConv.onValueChanged.AddListener(delegate
{
ConvDropdownValueChanged(SelectedConv);
});
SelectedFriend = GameObject.Find("Friend").GetComponent<Dropdown>();
SelectedGroup = GameObject.Find("Group").GetComponent<Dropdown>();
SelectedPriority = GameObject.Find("Priority").GetComponent<Dropdown>();
foreach (string name in Enum.GetNames(typeof(TIMMsgPriority)))
{
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = name;
SelectedPriority.options.Add(option);
}
SelectedGroup.onValueChanged.AddListener(delegate
{
GroupDropdownValueChanged(SelectedGroup);
});
SelectedFriend.onValueChanged.AddListener(delegate
{
FriendDropdownValueChanged(SelectedFriend);
});
IsOnline = GameObject.Find("Online").GetComponent<Toggle>();
IsUnread = GameObject.Find("Unread").GetComponent<Toggle>();
Receipt = GameObject.Find("Receipt").GetComponent<Toggle>();
Result = GameObject.Find("ResultText").GetComponent<Text>();
Submit = GameObject.Find("Submit").GetComponent<Button>();
Copy = GameObject.Find("Copy").GetComponent<Button>();
Submit.onClick.AddListener(SendMergerMessageSDK);
Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
Copy.onClick.AddListener(CopyText);
if (CurrentSceneInfo.info != null)
{
Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
}
ConvGetConvListSDK();
}
void MsgGetMsgListSDK(string conv_id, TIMConvType conv_type)
{
var get_message_list_param = new MsgGetMsgListParam
{
msg_getmsglist_param_count = 20
};
print(conv_id + conv_type);
TIMResult res = TencentIMSDK.MsgGetMsgList(conv_id, conv_type, get_message_list_param, Utils.addAsyncStringDataToScreen(GetMsgList));
}
void ConvDropdownValueChanged(Dropdown change)
{
MsgList = new List<Message>();
if (ConvList.Count > 0)
{
string conv_id = ConvList[change.value].conv_id;
TIMConvType conv_type = ConvList[change.value].conv_type;
MsgGetMsgListSDK(conv_id, conv_type);
}
}
void ToggleValueChanged(Toggle change)
{
string msgID = change.GetComponentInChildren<Text>().text.Split(':')[1];
if (change.isOn)
{
SelectedMsg.Add(msgID);
}
else
{
SelectedMsg.Remove(msgID);
}
}
void GenerateToggle()
{
var Parent = GameObject.Find("ToggleContent");
foreach (Transform child in Parent.transform)
{
GameObject.Destroy(child.gameObject);
}
var Toggler = GameObject.Find("Toggler").GetComponent<Toggle>();
foreach (Message msg in MsgList)
{
var obj = Instantiate(Toggler, Parent.transform);
obj.GetComponentInChildren<Text>().text = "msgID:" + msg.message_msg_id;
obj.isOn = false;
obj.onValueChanged.AddListener(delegate
{
ToggleValueChanged(obj);
});
}
}
void GetMsgList(params object[] parameters)
{
try
{
string text = (string)parameters[1];
List<Message> ListRes = Utils.FromJson<List<Message>>(text);
MsgList = new List<Message>();
SelectedMsg = new HashSet<string>();
foreach (Message item in ListRes)
{
print(item.message_msg_id);
MsgList.Add(item);
}
GenerateToggle();
}
catch (Exception ex)
{
Toast.Show(Utils.t("getMsgListFailed"));
}
}
void GetConvList(params object[] parameters)
{
try
{
ConvList = new List<ConvInfo>();
SelectedConv.ClearOptions();
string text = (string)parameters[1];
List<ConvInfo> List = Utils.FromJson<List<ConvInfo>>(text);
foreach (ConvInfo item in List)
{
print(item.conv_id);
ConvList.Add(item);
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = item.conv_id;
SelectedConv.options.Add(option);
}
if (List.Count > 0)
{
SelectedConv.captionText.text = List[SelectedConv.value].conv_id;
ConvDropdownValueChanged(SelectedConv);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getConvListFailed"));
}
}
void ConvGetConvListSDK()
{
TIMResult res = TencentIMSDK.ConvGetConvList(Utils.addAsyncStringDataToScreen(GetConvList));
print($"ConvGetConvListSDK {res}");
}
void GroupDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedFriend.value = 0;
}
}
void FriendDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedGroup.value = 0;
}
}
void GetGroupList(params object[] parameters)
{
try
{
GroupList = new List<string>();
SelectedGroup.ClearOptions();
string text = (string)parameters[1];
List<GroupBaseInfo> List = Utils.FromJson<List<GroupBaseInfo>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
GroupList.Add("");
option.text = "";
SelectedGroup.options.Add(option);
foreach (GroupBaseInfo item in List)
{
print(item.group_base_info_group_id);
GroupList.Add(item.group_base_info_group_id);
option = new Dropdown.OptionData();
option.text = item.group_base_info_group_id;
SelectedGroup.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupListFailed"));
}
}
void GetFriendList(params object[] parameters)
{
try
{
FriendList = new List<string>();
SelectedFriend.ClearOptions();
string text = (string)parameters[1];
List<FriendProfile> List = Utils.FromJson<List<FriendProfile>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
FriendList.Add("");
option.text = "";
SelectedFriend.options.Add(option);
foreach (FriendProfile item in List)
{
print(item.friend_profile_identifier);
FriendList.Add(item.friend_profile_identifier);
option = new Dropdown.OptionData();
option.text = item.friend_profile_identifier;
SelectedFriend.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getFriendListFailed"));
}
}
void GroupGetJoinedGroupListSDK()
{
TIMResult res = TencentIMSDK.GroupGetJoinedGroupList(Utils.addAsyncStringDataToScreen(GetGroupList));
print($"GroupGetJoinedGroupListSDK {res}");
}
void FriendshipGetFriendProfileListSDK()
{
TIMResult res = TencentIMSDK.FriendshipGetFriendProfileList(Utils.addAsyncStringDataToScreen(GetFriendList));
print($"FriendshipGetFriendProfileListSDK {res}");
}
void SendMergerMessageSDK()
{
if (ConvList.Count < 1 || MsgList.Count < 1) return;
List<Message> merged_msg = new List<Message>();
foreach (Message msg in MsgList)
{
if (SelectedMsg.Contains(msg.message_msg_id))
{
merged_msg.Add(msg);
}
}
var message = new Message
{
message_conv_type = TIMConvType.kTIMConv_Group,
message_cloud_custom_str = "unity local merger data",
message_elem_array = new List<Elem>
{
new Elem
{
elem_type = TIMElemType.kTIMElem_Merge,
merge_elem_title = "unity merged msg title",
merge_elem_abstract_array = new List<string>
{
"unity abstract array"
},
merge_elem_message_array = merged_msg,
merge_elem_compatible_text = "unity merged msg"
}
},
message_need_read_receipt = Receipt.isOn,
message_priority = (TIMMsgPriority)SelectedPriority.value,
message_is_excluded_from_unread_count = IsUnread.isOn,
message_is_online_msg = IsOnline.isOn
};
StringBuilder messageId = new StringBuilder(128);
if (SelectedGroup.value > 0)
{
print(GroupList[SelectedGroup.value]);
message.message_conv_id = GroupList[SelectedGroup.value];
message.message_conv_type = TIMConvType.kTIMConv_Group;
TIMResult res = TencentIMSDK.MsgSendMessage(GroupList[SelectedGroup.value], TIMConvType.kTIMConv_Group, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
else if (SelectedFriend.value > 0)
{
print(FriendList[SelectedFriend.value]);
message.message_conv_id = FriendList[SelectedFriend.value];
message.message_conv_type = TIMConvType.kTIMConv_C2C;
TIMResult res = TencentIMSDK.MsgSendMessage(FriendList[SelectedFriend.value], TIMConvType.kTIMConv_C2C, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
print(IsOnline.isOn);
print(IsUnread.isOn);
}
void GetResult(params object[] parameters)
{
Result.text += (string)parameters[0];
}
void CopyText()
{
Utils.Copy(Result.text);
}
void OnApplicationQuit()
{
TencentIMSDK.Uninit();
}
}
SendSoundMessage的案例
using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using System;
using com.tencent.im.unity.demo.utils;
using EasyUI.Toast;
using System.Collections;
using System.Text;
using System.Collections.Generic;
using System.IO;
public class SendSoundMessage : MonoBehaviour
{
string[] Labels = new string[] { "SoundMessageLabel", "SelectFriendLabel", "SelectGroupLabel", "SelectPriorityLabel", "IsOnlineLabel", "IsUnreadLabel" };
public Text Header;
public Button PickFileButton;
public Text PathText;
public Dropdown SelectedFriend;
public Dropdown SelectedGroup;
public Dropdown SelectedPriority;
public Toggle IsOnline;
public Toggle IsUnread;
public Text Result;
public Button Submit;
public Button Copy;
private List<string> GroupList;
private List<string> FriendList;
private string path;
private bool micConnected = false;//麦克风是否连接
private int minFreq, maxFreq;//最小和最大频率
public AudioClip RecordedClip;//录音
private string fileName;//保存的文件名
private bool isRecording = false;
private byte[] data;
void Start()
{
Application.RequestUserAuthorization(UserAuthorization.Microphone);
if (Microphone.devices.Length <= 0)
{
Debug.LogError("缺少麦克风设备!");
}
else
{
Debug.Log("设备名称为:" + Microphone.devices[0].ToString() + "请点击Start开始录音!");
micConnected = true;
Microphone.GetDeviceCaps(null, out minFreq, out maxFreq);
if (minFreq == 0 && maxFreq == 0)
{
maxFreq = 44100;
}
}
foreach (string label in Labels)
{
GameObject.Find(label).GetComponent<Text>().text = Utils.t(label);
}
GroupGetJoinedGroupListSDK();
FriendshipGetFriendProfileListSDK();
Header = GameObject.Find("HeaderText").GetComponent<Text>();
PickFileButton = GameObject.Find("PickFileButton").GetComponent<Button>();
PickFileButton.onClick.AddListener(OnPickFile);
PathText = GameObject.Find("Path").GetComponent<Text>();
SelectedFriend = GameObject.Find("Friend").GetComponent<Dropdown>();
SelectedGroup = GameObject.Find("Group").GetComponent<Dropdown>();
SelectedPriority = GameObject.Find("Priority").GetComponent<Dropdown>();
foreach (string name in Enum.GetNames(typeof(TIMMsgPriority)))
{
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = name;
SelectedPriority.options.Add(option);
}
SelectedGroup.onValueChanged.AddListener(delegate
{
GroupDropdownValueChanged(SelectedGroup);
});
SelectedFriend.onValueChanged.AddListener(delegate
{
FriendDropdownValueChanged(SelectedFriend);
});
IsOnline = GameObject.Find("Online").GetComponent<Toggle>();
IsUnread = GameObject.Find("Unread").GetComponent<Toggle>();
Result = GameObject.Find("ResultText").GetComponent<Text>();
Submit = GameObject.Find("Submit").GetComponent<Button>();
Copy = GameObject.Find("Copy").GetComponent<Button>();
Submit.onClick.AddListener(SendSoundMessageSDK);
Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
Copy.onClick.AddListener(CopyText);
if (CurrentSceneInfo.info != null)
{
Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
}
}
/// <summary>
/// 开始录音
/// </summary>
void Begin()
{
if (micConnected)
{
if (!Microphone.IsRecording(null))
{
RecordedClip = Microphone.Start(null, false, 60, maxFreq);
isRecording = true;
PathText.text = "";
path = "";
PickFileButton.GetComponentInChildren<Text>().text = Utils.t("SoundFinMessageLabel");
}
}
else
{
Debug.LogError("缺少麦克风设备!");
}
}
/// <summary>
/// 停止录音
/// </summary>
void Stop(bool shouldSave = true)
{
Microphone.End(null);
if (!shouldSave) return;
data = GetRealAudio(ref RecordedClip);
isRecording = false;
PickFileButton.GetComponentInChildren<Text>().text = Utils.t("SoundMessageLabel");
Save();
}
/// <summary>
/// 保存录音
/// </summary>
public void Save()
{
fileName = "sound_" + DateTime.Now.ToString("ffff");
if (!fileName.ToLower().EndsWith(".wav"))
{//如果不是“.wav”格式的,加上后缀
fileName += ".wav";
}
path = Path.Combine(Application.persistentDataPath, fileName);//录音保存路径
PathText.text = path;
using (FileStream fs = CreateEmpty(path))
{
fs.Write(data, 0, data.Length);
WriteHeader(fs, RecordedClip); //wav文件头
}
}
/// <summary>
/// 创建wav格式文件头
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
private FileStream CreateEmpty(string filepath)
{
FileStream fileStream = new FileStream(filepath, FileMode.Create);
byte emptyByte = new byte();
for (int i = 0; i < 44; i++) //为wav文件头留出空间
{
fileStream.WriteByte(emptyByte);
}
return fileStream;
}
/// <summary>
/// 获取真正大小的录音
/// </summary>
/// <param name="recordedClip"></param>
/// <returns></returns>
public static byte[] GetRealAudio(ref AudioClip recordedClip)
{
int position = Microphone.GetPosition(null);
if (position <= 0 || position > recordedClip.samples)
{
position = recordedClip.samples;
}
float[] soundata = new float[position * recordedClip.channels];
recordedClip.GetData(soundata, 0);
recordedClip = AudioClip.Create(recordedClip.name, position,
recordedClip.channels, recordedClip.frequency, false);
recordedClip.SetData(soundata, 0);
int rescaleFactor = 32767;
byte[] outData = new byte[soundata.Length * 2];
for (int i = 0; i < soundata.Length; i++)
{
short temshort = (short)(soundata[i] * rescaleFactor);
byte[] temdata = BitConverter.GetBytes(temshort);
outData[i * 2] = temdata[0];
outData[i * 2 + 1] = temdata[1];
}
Debug.Log("position=" + position + " outData.leng=" + outData.Length);
return outData;
}
/// <summary>
/// 写文件头
/// </summary>
/// <param name="stream"></param>
/// <param name="clip"></param>
public static void WriteHeader(FileStream stream, AudioClip clip)
{
int hz = clip.frequency;
int channels = clip.channels;
int samples = clip.samples;
stream.Seek(0, SeekOrigin.Begin);
Byte[] riff = System.Text.Encoding.UTF8.GetBytes("RIFF");
stream.Write(riff, 0, 4);
Byte[] chunkSize = BitConverter.GetBytes(stream.Length - 8);
stream.Write(chunkSize, 0, 4);
Byte[] wave = System.Text.Encoding.UTF8.GetBytes("WAVE");
stream.Write(wave, 0, 4);
Byte[] fmt = System.Text.Encoding.UTF8.GetBytes("fmt ");
stream.Write(fmt, 0, 4);
Byte[] subChunk1 = BitConverter.GetBytes(16);
stream.Write(subChunk1, 0, 4);
UInt16 one = 1;
Byte[] audioFormat = BitConverter.GetBytes(one);
stream.Write(audioFormat, 0, 2);
Byte[] numChannels = BitConverter.GetBytes(channels);
stream.Write(numChannels, 0, 2);
Byte[] sampleRate = BitConverter.GetBytes(hz);
stream.Write(sampleRate, 0, 4);
Byte[] byteRate = BitConverter.GetBytes(hz * channels * 2);
stream.Write(byteRate, 0, 4);
UInt16 blockAlign = (ushort)(channels * 2);
stream.Write(BitConverter.GetBytes(blockAlign), 0, 2);
UInt16 bps = 16;
Byte[] bitsPerSample = BitConverter.GetBytes(bps);
stream.Write(bitsPerSample, 0, 2);
Byte[] datastring = System.Text.Encoding.UTF8.GetBytes("data");
stream.Write(datastring, 0, 4);
Byte[] subChunk2 = BitConverter.GetBytes(samples * channels * 2);
stream.Write(subChunk2, 0, 4);
}
public void OnPickFile()
{
if (!Application.HasUserAuthorization(UserAuthorization.Microphone)) return;
if (isRecording)
{
Stop();
}
else
{
Begin();
}
}
void GroupDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedFriend.value = 0;
}
}
void FriendDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedGroup.value = 0;
}
}
void GetGroupList(params object[] parameters)
{
try
{
GroupList = new List<string>();
SelectedGroup.ClearOptions();
string text = (string)parameters[1];
List<GroupBaseInfo> List = Utils.FromJson<List<GroupBaseInfo>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
GroupList.Add("");
option.text = "";
SelectedGroup.options.Add(option);
foreach (GroupBaseInfo item in List)
{
print(item.group_base_info_group_id);
GroupList.Add(item.group_base_info_group_id);
option = new Dropdown.OptionData();
option.text = item.group_base_info_group_id;
SelectedGroup.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupListFailed"));
}
}
void GetFriendList(params object[] parameters)
{
try
{
FriendList = new List<string>();
SelectedFriend.ClearOptions();
string text = (string)parameters[1];
List<FriendProfile> List = Utils.FromJson<List<FriendProfile>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
FriendList.Add("");
option.text = "";
SelectedFriend.options.Add(option);
foreach (FriendProfile item in List)
{
print(item.friend_profile_identifier);
FriendList.Add(item.friend_profile_identifier);
option = new Dropdown.OptionData();
option.text = item.friend_profile_identifier;
SelectedFriend.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getFriendListFailed"));
}
}
void GroupGetJoinedGroupListSDK()
{
TIMResult res = TencentIMSDK.GroupGetJoinedGroupList(Utils.addAsyncStringDataToScreen(GetGroupList));
print($"GroupGetJoinedGroupListSDK {res}");
}
void FriendshipGetFriendProfileListSDK()
{
TIMResult res = TencentIMSDK.FriendshipGetFriendProfileList(Utils.addAsyncStringDataToScreen(GetFriendList));
print($"FriendshipGetFriendProfileListSDK {res}");
}
void SendSoundMessageSDK()
{
var message = new Message
{
message_cloud_custom_str = "unity local sound data",
message_elem_array = new List<Elem>{new Elem
{
elem_type = TIMElemType.kTIMElem_Sound,
sound_elem_file_path = path
}},
message_need_read_receipt = false,
message_priority = (TIMMsgPriority)SelectedPriority.value,
message_is_excluded_from_unread_count = IsUnread.isOn,
message_is_online_msg = IsOnline.isOn
};
StringBuilder messageId = new StringBuilder(128);
if (SelectedGroup.value > 0)
{
print(GroupList[SelectedGroup.value]);
message.message_conv_id = GroupList[SelectedGroup.value];
message.message_conv_type = TIMConvType.kTIMConv_Group;
TIMResult res = TencentIMSDK.MsgSendMessage(GroupList[SelectedGroup.value], TIMConvType.kTIMConv_Group, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
else if (SelectedFriend.value > 0)
{
print(FriendList[SelectedFriend.value]);
message.message_conv_id = FriendList[SelectedFriend.value];
message.message_conv_type = TIMConvType.kTIMConv_C2C;
TIMResult res = TencentIMSDK.MsgSendMessage(FriendList[SelectedFriend.value], TIMConvType.kTIMConv_C2C, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
print(IsOnline.isOn);
print(IsUnread.isOn);
}
void GetResult(params object[] parameters)
{
Result.text += (string)parameters[0];
}
void CopyText()
{
Utils.Copy(Result.text);
}
void OnApplicationQuit()
{
Stop(false);
TencentIMSDK.Uninit();
}
}
SendTextAtMessage的案例
using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using System;
using com.tencent.im.unity.demo.utils;
using EasyUI.Toast;
using System.Collections;
using System.Text;
using System.Collections.Generic;
public class SendTextAtMessage : MonoBehaviour
{
string[] Labels = new string[] { "MessageLabel", "SelectGroupLabel", "SelectGroupMemberLabel", "SelectPriorityLabel", "IsOnlineLabel", "IsUnreadLabel", "needReceiptLabel" };
public Text Header;
public InputField Input;
public Dropdown SelectedGroup;
public Dropdown SelectedGroupMember;
public Dropdown SelectedPriority;
public Toggle IsOnline;
public Toggle IsUnread;
public Toggle Receipt;
public Text Result;
public Button Submit;
public Button Copy;
public List<string> UserList = new List<string>();
public HashSet<string> SelectedUser = new HashSet<string>();
private List<string> GroupList;
private List<string> FriendList;
void Start()
{
foreach (string label in Labels)
{
GameObject.Find(label).GetComponent<Text>().text = Utils.t(label);
}
GroupGetJoinedGroupListSDK();
Header = GameObject.Find("HeaderText").GetComponent<Text>();
Input = GameObject.Find("Message").GetComponent<InputField>();
SelectedGroup = GameObject.Find("Group").GetComponent<Dropdown>();
SelectedGroup.onValueChanged.AddListener(delegate
{
GroupDropdownValueChanged(SelectedGroup);
});
SelectedPriority = GameObject.Find("Priority").GetComponent<Dropdown>();
foreach (string name in Enum.GetNames(typeof(TIMMsgPriority)))
{
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = name;
SelectedPriority.options.Add(option);
}
IsOnline = GameObject.Find("Online").GetComponent<Toggle>();
IsUnread = GameObject.Find("Unread").GetComponent<Toggle>();
Receipt = GameObject.Find("Receipt").GetComponent<Toggle>();
Result = GameObject.Find("ResultText").GetComponent<Text>();
Submit = GameObject.Find("Submit").GetComponent<Button>();
Copy = GameObject.Find("Copy").GetComponent<Button>();
Submit.onClick.AddListener(SendTextAtMessageSDK);
Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
Copy.onClick.AddListener(CopyText);
if (CurrentSceneInfo.info != null)
{
Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
}
}
void GroupDropdownValueChanged(Dropdown change)
{
GroupGetMemberInfoListSDK();
}
void ToggleValueChanged(Toggle change)
{
string userID = change.GetComponentInChildren<Text>().text.Split(':')[1];
if (change.isOn)
{
SelectedUser.Add(userID);
}
else
{
SelectedUser.Remove(userID);
}
}
void GenerateToggle()
{
var Parent = GameObject.Find("ToggleContent");
foreach (Transform child in Parent.transform)
{
GameObject.Destroy(child.gameObject);
}
var Toggler = GameObject.Find("Toggler").GetComponent<Toggle>();
foreach (string user_id in UserList)
{
var obj = Instantiate(Toggler, Parent.transform);
obj.GetComponentInChildren<Text>().text = "userID:" + user_id;
obj.isOn = false;
obj.onValueChanged.AddListener(delegate
{
ToggleValueChanged(obj);
});
}
}
void GetGroupList(params object[] parameters)
{
try
{
GroupList = new List<string>();
SelectedGroup.ClearOptions();
string text = (string)parameters[1];
List<GroupBaseInfo> List = Utils.FromJson<List<GroupBaseInfo>>(text);
foreach (GroupBaseInfo item in List)
{
print(item.group_base_info_group_id);
GroupList.Add(item.group_base_info_group_id);
var option = new Dropdown.OptionData();
option.text = item.group_base_info_group_id;
SelectedGroup.options.Add(option);
}
if (List.Count > 0)
{
SelectedGroup.captionText.text = List[SelectedGroup.value].group_base_info_group_id;
GroupGetMemberInfoListSDK();
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupListFailed"));
}
}
void GroupGetJoinedGroupListSDK()
{
TIMResult res = TencentIMSDK.GroupGetJoinedGroupList(Utils.addAsyncStringDataToScreen(GetGroupList));
print($"GroupGetJoinedGroupListSDK {res}");
}
void GroupGetMemberInfoListSDK()
{
if (GroupList.Count < 1) return;
string group_id = GroupList[SelectedGroup.value];
GroupGetMemberInfoListParam param = new GroupGetMemberInfoListParam
{
group_get_members_info_list_param_group_id = group_id
};
TIMResult res = TencentIMSDK.GroupGetMemberInfoList(param, Utils.addAsyncStringDataToScreen(GetGroupMemberList));
print($"GroupGetMemberInfoListSDK {res}");
}
void GetGroupMemberList(params object[] parameters)
{
try
{
string text = (string)parameters[1];
List<GroupMemberInfo> List = Utils.FromJson<GroupGetMemberInfoListResult>(text).group_get_memeber_info_list_result_info_array;
UserList = new List<string>();
SelectedUser = new HashSet<string>();
foreach (GroupMemberInfo item in List)
{
print(item.group_member_info_identifier);
UserList.Add(item.group_member_info_identifier);
}
GenerateToggle();
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupMemberListFailed"));
}
}
void SendTextAtMessageSDK()
{
List<string> user_list = new List<string>(SelectedUser);
var message = new Message
{
message_conv_type = TIMConvType.kTIMConv_Group,
message_cloud_custom_str = "unity local text at data",
message_elem_array = new List<Elem>{new Elem
{
elem_type = TIMElemType.kTIMElem_Text,
text_elem_content = Input.text
}},
message_need_read_receipt = Receipt.isOn,
message_priority = (TIMMsgPriority)SelectedPriority.value,
message_is_excluded_from_unread_count = IsUnread.isOn,
message_is_online_msg = IsOnline.isOn,
message_group_at_user_array = user_list
};
StringBuilder messageId = new StringBuilder(128);
print(GroupList[SelectedGroup.value]);
message.message_conv_id = GroupList[SelectedGroup.value];
message.message_conv_type = TIMConvType.kTIMConv_Group;
TIMResult res = TencentIMSDK.MsgSendMessage(GroupList[SelectedGroup.value], TIMConvType.kTIMConv_Group, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
print(IsOnline.isOn);
print(IsUnread.isOn);
}
void GetResult(params object[] parameters)
{
Result.text += (string)parameters[0];
}
void CopyText()
{
Utils.Copy(Result.text);
}
void OnApplicationQuit()
{
TencentIMSDK.Uninit();
}
}
SendTextMessage的案例
using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using System;
using com.tencent.im.unity.demo.utils;
using EasyUI.Toast;
using System.Collections;
using System.Text;
using System.Collections.Generic;
public class SendTextMessage : MonoBehaviour
{
string[] Labels = new string[] {"MessageLabel", "SelectFriendLabel", "SelectGroupLabel", "SelectPriorityLabel", "IsOnlineLabel", "IsUnreadLabel", "needReceiptLabel"};
public Text Header;
public InputField Input;
public Dropdown SelectedFriend;
public Dropdown SelectedGroup;
public Dropdown SelectedPriority;
public Toggle IsOnline;
public Toggle IsUnread;
public Toggle Receipt;
public Text Result;
public Button Submit;
public Button Copy;
private List<string> GroupList;
private List<string> FriendList;
void Start()
{
foreach (string label in Labels)
{
GameObject.Find(label).GetComponent<Text>().text = Utils.t(label);
}
GroupGetJoinedGroupListSDK();
FriendshipGetFriendProfileListSDK();
Header = GameObject.Find("HeaderText").GetComponent<Text>();
Input = GameObject.Find("Message").GetComponent<InputField>();
SelectedFriend = GameObject.Find("Friend").GetComponent<Dropdown>();
SelectedGroup = GameObject.Find("Group").GetComponent<Dropdown>();
SelectedPriority = GameObject.Find("Priority").GetComponent<Dropdown>();
foreach (string name in Enum.GetNames(typeof(TIMMsgPriority)))
{
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = name;
SelectedPriority.options.Add(option);
}
SelectedGroup.onValueChanged.AddListener(delegate
{
GroupDropdownValueChanged(SelectedGroup);
});
SelectedFriend.onValueChanged.AddListener(delegate
{
FriendDropdownValueChanged(SelectedFriend);
});
IsOnline = GameObject.Find("Online").GetComponent<Toggle>();
IsUnread = GameObject.Find("Unread").GetComponent<Toggle>();
Receipt = GameObject.Find("Receipt").GetComponent<Toggle>();
Result = GameObject.Find("ResultText").GetComponent<Text>();
Submit = GameObject.Find("Submit").GetComponent<Button>();
Copy = GameObject.Find("Copy").GetComponent<Button>();
Submit.onClick.AddListener(SendTextMessageSDK);
Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
Copy.onClick.AddListener(CopyText);
if (CurrentSceneInfo.info != null)
{
Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
}
}
void GroupDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedFriend.value = 0;
}
}
void FriendDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedGroup.value = 0;
}
}
void GetGroupList(params object[] parameters)
{
try
{
GroupList = new List<string>();
SelectedGroup.ClearOptions();
string text = (string)parameters[1];
List<GroupBaseInfo> List = Utils.FromJson<List<GroupBaseInfo>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
GroupList.Add("");
option.text = "";
SelectedGroup.options.Add(option);
foreach (GroupBaseInfo item in List)
{
print(item.group_base_info_group_id);
GroupList.Add(item.group_base_info_group_id);
option = new Dropdown.OptionData();
option.text = item.group_base_info_group_id;
SelectedGroup.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupListFailed"));
}
}
void GetFriendList(params object[] parameters)
{
try
{
FriendList = new List<string>();
SelectedFriend.ClearOptions();
string text = (string)parameters[1];
List<FriendProfile> List = Utils.FromJson<List<FriendProfile>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
FriendList.Add("");
option.text = "";
SelectedFriend.options.Add(option);
foreach (FriendProfile item in List)
{
print(item.friend_profile_identifier);
FriendList.Add(item.friend_profile_identifier);
option = new Dropdown.OptionData();
option.text = item.friend_profile_identifier;
SelectedFriend.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getFriendListFailed"));
}
}
void GroupGetJoinedGroupListSDK()
{
TIMResult res = TencentIMSDK.GroupGetJoinedGroupList(Utils.addAsyncStringDataToScreen(GetGroupList));
print($"GroupGetJoinedGroupListSDK {res}");
}
void FriendshipGetFriendProfileListSDK()
{
TIMResult res = TencentIMSDK.FriendshipGetFriendProfileList(Utils.addAsyncStringDataToScreen(GetFriendList));
print($"FriendshipGetFriendProfileListSDK {res}");
}
void SendTextMessageSDK()
{
var message = new Message
{
message_conv_type = TIMConvType.kTIMConv_Group,
message_cloud_custom_str = "unity local text data",
message_elem_array = new List<Elem>{new Elem
{
elem_type = TIMElemType.kTIMElem_Text,
text_elem_content = Input.text
}},
message_need_read_receipt = Receipt.isOn,
message_priority = (TIMMsgPriority)SelectedPriority.value,
message_is_excluded_from_unread_count = IsUnread.isOn,
message_is_online_msg = IsOnline.isOn
};
StringBuilder messageId = new StringBuilder(128);
if (SelectedGroup.value > 0)
{
print(GroupList[SelectedGroup.value]);
message.message_conv_id = GroupList[SelectedGroup.value];
message.message_conv_type = TIMConvType.kTIMConv_Group;
TIMResult res = TencentIMSDK.MsgSendMessage(GroupList[SelectedGroup.value], TIMConvType.kTIMConv_Group, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
else if (SelectedFriend.value > 0)
{
print(FriendList[SelectedFriend.value]);
message.message_conv_id = FriendList[SelectedFriend.value];
message.message_conv_type = TIMConvType.kTIMConv_C2C;
TIMResult res = TencentIMSDK.MsgSendMessage(FriendList[SelectedFriend.value], TIMConvType.kTIMConv_C2C, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
print(IsOnline.isOn);
print(IsUnread.isOn);
}
void GetResult(params object[] parameters)
{
Result.text += (string)parameters[0];
}
void CopyText()
{
Utils.Copy(Result.text);
}
void OnApplicationQuit()
{
TencentIMSDK.Uninit();
}
}
SendVideoMessage的案例
using UnityEngine;
using UnityEngine.UI;
using com.tencent.im.unity.demo.types;
using com.tencent.imsdk.unity;
using com.tencent.imsdk.unity.types;
using com.tencent.imsdk.unity.enums;
using System;
using com.tencent.im.unity.demo.utils;
using EasyUI.Toast;
using System.Collections;
using System.Text;
using System.Collections.Generic;
public class SendVideoMessage : MonoBehaviour
{
string[] Labels = new string[] { "VideoMessageLabel", "ScreenshotMessageLabel", "SelectFriendLabel", "SelectGroupLabel", "SelectPriorityLabel", "IsOnlineLabel", "IsUnreadLabel" };
public Text Header;
public Button PickFileButton;
public Text PathText;
public Button PickScreenshotButton;
public Text ScreenshotPathText;
public Dropdown SelectedFriend;
public Dropdown SelectedGroup;
public Dropdown SelectedPriority;
public Toggle IsOnline;
public Toggle IsUnread;
public Text Result;
public Button Submit;
public Button Copy;
private List<string> GroupList;
private List<string> FriendList;
private string path;
private string screenshotPath;
void Start()
{
foreach (string label in Labels)
{
GameObject.Find(label).GetComponent<Text>().text = Utils.t(label);
}
GroupGetJoinedGroupListSDK();
FriendshipGetFriendProfileListSDK();
Header = GameObject.Find("HeaderText").GetComponent<Text>();
PickFileButton = GameObject.Find("PickFileButton").GetComponent<Button>();
PickFileButton.onClick.AddListener(OnPickFile);
PathText = GameObject.Find("Path").GetComponent<Text>();
PickScreenshotButton = GameObject.Find("PickScreenshotButton").GetComponent<Button>();
PickScreenshotButton.onClick.AddListener(OnPickScreenshot);
ScreenshotPathText = GameObject.Find("ScreenshotPath").GetComponent<Text>();
SelectedFriend = GameObject.Find("Friend").GetComponent<Dropdown>();
SelectedGroup = GameObject.Find("Group").GetComponent<Dropdown>();
SelectedPriority = GameObject.Find("Priority").GetComponent<Dropdown>();
foreach (string name in Enum.GetNames(typeof(TIMMsgPriority)))
{
Dropdown.OptionData option = new Dropdown.OptionData();
option.text = name;
SelectedPriority.options.Add(option);
}
SelectedGroup.onValueChanged.AddListener(delegate
{
GroupDropdownValueChanged(SelectedGroup);
});
SelectedFriend.onValueChanged.AddListener(delegate
{
FriendDropdownValueChanged(SelectedFriend);
});
IsOnline = GameObject.Find("Online").GetComponent<Toggle>();
IsUnread = GameObject.Find("Unread").GetComponent<Toggle>();
Result = GameObject.Find("ResultText").GetComponent<Text>();
Submit = GameObject.Find("Submit").GetComponent<Button>();
Copy = GameObject.Find("Copy").GetComponent<Button>();
Submit.onClick.AddListener(SendVideoMessageSDK);
Copy.GetComponentInChildren<Text>().text = Utils.t("Copy");
Copy.onClick.AddListener(CopyText);
if (CurrentSceneInfo.info != null)
{
Header.text = Utils.IsCn() ? CurrentSceneInfo.info.apiText + " " + CurrentSceneInfo.info.apiName : CurrentSceneInfo.info.apiName;
Submit.GetComponentInChildren<Text>().text = CurrentSceneInfo.info.apiName;
}
}
public void OnPickFile()
{
Utils.PickVideo((string pathStr) =>
{
path = pathStr;
PathText.text = pathStr;
});
}
public void OnPickScreenshot()
{
Utils.PickImage((string pathStr) =>
{
screenshotPath = pathStr;
ScreenshotPathText.text = pathStr;
});
}
void GroupDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedFriend.value = 0;
}
}
void FriendDropdownValueChanged(Dropdown change)
{
if (change.value > 0)
{
SelectedGroup.value = 0;
}
}
void GetGroupList(params object[] parameters)
{
try
{
GroupList = new List<string>();
SelectedGroup.ClearOptions();
string text = (string)parameters[1];
List<GroupBaseInfo> List = Utils.FromJson<List<GroupBaseInfo>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
GroupList.Add("");
option.text = "";
SelectedGroup.options.Add(option);
foreach (GroupBaseInfo item in List)
{
print(item.group_base_info_group_id);
GroupList.Add(item.group_base_info_group_id);
option = new Dropdown.OptionData();
option.text = item.group_base_info_group_id;
SelectedGroup.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getGroupListFailed"));
}
}
void GetFriendList(params object[] parameters)
{
try
{
FriendList = new List<string>();
SelectedFriend.ClearOptions();
string text = (string)parameters[1];
List<FriendProfile> List = Utils.FromJson<List<FriendProfile>>(text);
Dropdown.OptionData option = new Dropdown.OptionData();
FriendList.Add("");
option.text = "";
SelectedFriend.options.Add(option);
foreach (FriendProfile item in List)
{
print(item.friend_profile_identifier);
FriendList.Add(item.friend_profile_identifier);
option = new Dropdown.OptionData();
option.text = item.friend_profile_identifier;
SelectedFriend.options.Add(option);
}
}
catch (Exception ex)
{
Toast.Show(Utils.t("getFriendListFailed"));
}
}
void GroupGetJoinedGroupListSDK()
{
TIMResult res = TencentIMSDK.GroupGetJoinedGroupList(Utils.addAsyncStringDataToScreen(GetGroupList));
print($"GroupGetJoinedGroupListSDK {res}");
}
void FriendshipGetFriendProfileListSDK()
{
TIMResult res = TencentIMSDK.FriendshipGetFriendProfileList(Utils.addAsyncStringDataToScreen(GetFriendList));
print($"FriendshipGetFriendProfileListSDK {res}");
}
void SendVideoMessageSDK()
{
var message = new Message
{
message_cloud_custom_str = "unity local video data",
message_elem_array = new List<Elem>{new Elem
{
elem_type = TIMElemType.kTIMElem_Video,
video_elem_video_type = path.Split('.')[path.Split('.').Length - 1],
video_elem_video_path = path,
video_elem_image_path = screenshotPath,
video_elem_image_type = screenshotPath.Split('.')[screenshotPath.Split('.').Length - 1], // 视频截图文件类型
}},
message_need_read_receipt = false,
message_priority = (TIMMsgPriority)SelectedPriority.value,
message_is_excluded_from_unread_count = IsUnread.isOn,
message_is_online_msg = IsOnline.isOn
};
StringBuilder messageId = new StringBuilder(128);
if (SelectedGroup.value > 0)
{
print(GroupList[SelectedGroup.value]);
message.message_conv_id = GroupList[SelectedGroup.value];
message.message_conv_type = TIMConvType.kTIMConv_Group;
TIMResult res = TencentIMSDK.MsgSendMessage(GroupList[SelectedGroup.value], TIMConvType.kTIMConv_Group, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
else if (SelectedFriend.value > 0)
{
print(FriendList[SelectedFriend.value]);
message.message_conv_id = FriendList[SelectedFriend.value];
message.message_conv_type = TIMConvType.kTIMConv_C2C;
TIMResult res = TencentIMSDK.MsgSendMessage(FriendList[SelectedFriend.value], TIMConvType.kTIMConv_C2C, message, messageId, Utils.addAsyncStringDataToScreen(GetResult));
Result.text = Utils.SynchronizeResult(res);
}
print(IsOnline.isOn);
print(IsUnread.isOn);
}
void GetResult(params object[] parameters)
{
Result.text += (string)parameters[0];
}
void CopyText()
{
Utils.Copy(Result.text);
}
void OnApplicationQuit()
{
TencentIMSDK.Uninit();
}
}