# 定向消息

> Send direct messages between users.

VivoxMessages 可以发送给已登录服务的个人玩家。此功能通常称为定向消息 (DM)。

## 发送定向消息##send-directed-messages

您可以使用 [`VivoxService.Instance.SendDirectTextMessageAsync(string playerId, string message)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_SendDirectTextMessageAsync_System_String_System_String_Unity_Services_Vivox_MessageOptions_) 向玩家发送消息，其中 playerId 是应将消息发送到的玩家的 playerId，而 message 是应发送的消息。

以下代码片段的示例实现了向从托管频道名单传入的玩家发送一条消息，此消息是从专用消息 InputField 中提取的。

```cs
void SendMessageAsync(string playerId)
{
    if (string.IsNullOrEmpty(MessageInputField.text))
    {
        return;
    }

    VivoxService.Instance.SendChannelTextMessageAsync(playerId, MessageInputField.text);
    MessageInputField.text = string.Empty;
}
```

## 接收定向消息##receive-directed-messages

为了接收来自其他用户的消息，必须订阅 [`VivoxService.Instance.DirectedMessageReceived`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DirectedMessageReceived) 事件。

定向 VivoxMessages 与[频道消息](./channel-messages.md)几乎相同，但需要注意的是 `VivoxMessage.ChannelName` 将设置为 null，`VivoxMessage.FromSelf` 将设置为 false。

以下代码片段是订阅以上事件的代码行示例，以及在收到 VivoxMessage 时从中提取所有可用信息的示例。

```cs

//Subscribe to this event at some point in the Vivox initialization process,
//preferably before joining any channels.
. . .
VivoxService.Instance.DirectedMessageReceived += OnDirectedMessageReceived;
. . .

void OnDirectedMessageReceived(VivoxMessage message)
{
    var senderName = message.SenderDisplayName;
    var senderId = message.SenderPlayerId;
    var messageText = message.MessageText;
    var timeReceived = message.ReceivedTime;
    var language = message.Language;
    var messageId = message.MessageId;
}
```

## 文本私信历史记录##direct-text-message-history

Vivox 还允许用户使用 [`VivoxService.Instance.GetDirectTextMessageHistoryAsync(string playerId, int requestSize = 10, ChatHistoryQueryOptions chatHistoryQueryOptions = null)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_GetDirectTextMessageHistoryAsync_System_String_System_Int32_Unity_Services_Vivox_ChatHistoryQueryOptions_) 访问两个玩家之间私信对话的历史记录。其中，playerId 是要请求相应历史记录的玩家的 ID，requestSize 是最多要返回的消息数（默认值为 10），chatHistoryQueryOptions 是一个可选参数，可以执行多种操作，例如指定消息中包含的单词或短语，或前后查询时间。如需了解有关 ChatHistoryQueryOptions 的更多具体信息，请参阅[聊天历史记录查询选项](../../text-chat-guide/chat-history-query-options)页面。

`IReadOnlyCollection` 中返回的消息按从最新消息到最旧消息的顺序列出。

以下代码片段的示例说明如何从设置的 LobbyChannelName 中提取最近 25 条消息，然后按从旧到新的顺序记录发送方的显示名称和消息，而无需使用可选的 `ChatHistoryQueryOptions`。

```cs
void FetchHistoryAsync()
{
    var historyMessages = await VivoxService.Instance.GetDirectTextMessageHistoryAsync(LobbyChannelName, 10);
    //Reversing the messages so they display from oldest to newest
    var reversedMessages = historyMessages.Reverse();
    foreach(VivoxMessage message in reversedMessages)
    {
        Debug.Log($"{message.SenderDisplayName}: {message.MessageText}");
    }
}
```

## 编辑私信##editing-direct-messages

Vivox 允许用户使用 [`VivoxService.Instance.EditDirectTextMessageAsync(string messageId, string newMessage)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_EditDirectTextMessageAsync_System_String_System_String_) 编辑他们发送到频道的消息文本，其中 messageId 是要更改的消息的 ID，newMessage 是更新的消息文本。

当成功编辑消息后，与私信关联的登录玩家将收到一个 [`VivoxService.Instance.DirectedMessageEdited`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DirectedMessageEdited) 操作，其中包含已编辑的 VivoxMessage 以及更新的 MessageText。

```cs
public async void UpdateDirectedMessageAsync(VivoxMessage messageToUpdate, string updatedMessageText)
{
    await VivoxService.Instance.EditDirectTextMessageAsync(messageToUpdate.MessageId, updatedMessageText);
}
```

## 删除私信##deleting-direct-messages

Vivox 允许用户使用 [`VivoxService.Instance.DeleteDirectTextMessageAsync(string messageId)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DeleteDirectTextMessageAsync_System_String_) 删除他们发送的私信，其中 messageId 是要删除的消息的 ID。

当成功删除消息后，与定向消息关联的登录玩家将收到一个 [`VivoxService.Instance.DirectedMessageDeleted`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DirectedMessageDeleted) 操作，其中包含已删除的 VivoxMessage。

```cs
public async void DeleteDirectMessageAsync(VivoxMessage messageToDelete)
{
    await VivoxService.Instance.DeleteDirectTextMessageAsync(messageToDelete.MessageId)
}
```
