c# - 如何在聊天机器人统一中添加延迟时间
问题描述
有人可以帮我在此代码中添加用户味精和机器人味精之间的延迟时间 2 秒吗
如果有人能提供帮助,我将不胜感激,谢谢
Unity 中的对话机器人,使用设备上和离线机器人开发框架。
using Syn.Bot.Oscova.Attributes;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Message
{
public string Text;
public Text TextObject;
public MessageType MessageType;
}
public enum MessageType
{
User, Bot
}
public class BotDialog: Dialog
{
[Expression("Hello Bot")]
public void Hello(Context context, Result result)
{
result.SendResponse("Hello User!");
}
}
public class GameManager : MonoBehaviour
{
OscovaBot MainBot;
public GameObject chatPanel, textObject;
public InputField chatBox;
public Color UserColor, BotColor;
List<Message> Messages = new List<Message>();
// Start is called before the first frame update
void Start()
{
try
{
MainBot = new OscovaBot();
OscovaBot.Logger.LogReceived += (s, o) =>
{
Debug.Log($"OscovaBot: {o.Log}");
};
MainBot.Dialogs.Add(new BotDialog());
//MainBot.ImportWorkspace("Assets/bot-kb.west");
MainBot.Trainer.StartTraining();
MainBot.MainUser.ResponseReceived += (sender, evt) =>
{
AddMessage($"Bot: {evt.Response.Text}", MessageType.Bot);
};
}
catch (Exception ex)
{
Debug.LogError(ex);
}
}
public void AddMessage(string messageText, MessageType messageType)
{
if (Messages.Count >= 25)
{
//Remove when too much.
Destroy(Messages[0].TextObject.gameObject);
Messages.Remove(Messages[0]);
}
var newMessage = new Message { Text = messageText };
var newText = Instantiate(textObject, chatPanel.transform);
newMessage.TextObject = newText.GetComponent<Text>();
newMessage.TextObject.text = messageText;
newMessage.TextObject.color = messageType == MessageType.User ? UserColor : BotColor;
Messages.Add(newMessage);
}
public void SendMessageToBot()
{
var userMessage = chatBox.text;
if (!string.IsNullOrEmpty(userMessage))
{
Debug.Log($"OscovaBot:[USER] {userMessage}");
AddMessage($"User: {userMessage}", MessageType.User);
var request = MainBot.MainUser.CreateRequest(userMessage);
var evaluationResult = MainBot.Evaluate(request);
evaluationResult.Invoke();
chatBox.Select();
chatBox.text = "";
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Return))
{
SendMessageToBot();
}
}
}
有关此代码的更多信息:https ://medium.com/@BotDeveloper/creating-a-bot-in-unity-in-c-6a595d51a8fa
解决方案
您好,您可以使用 2 种方式来完成。调用(字符串函数名称,浮点时间)或协程。您可以使用 and Invoke("Functionname", 2f); 在从用户获取消息 2 秒后执行该函数的延迟
PD:为什么要投反对票?我的回答是对的。
推荐阅读
- angular - 添加新元素时,角度更新现有 FormArray 元素的有效性
- python - sklearn KNeighborsClassifier 评分方法如何工作?
- php - 我正在尝试使用 php file() 并从另一个目录中提取并在文件名中使用 *
- javascript - 如何使用swift3从alamofire响应中获取字典数组的值
- optaplanner - 任务分配示例的领域模型推理不清楚
- node.js - bundle.js 没有被创建
- c# - 在 Visual Studio 中导入正确的模块 - Powershell cmdlet
- angular - 在 Angular 教程中出现无法找到名称“HEROES”错误
- c# - 如何从每个内核上的程序确定 CPU 使用率?
- macos - 如何在mac中使用grpcurl?