当前位置:网站首页>Unity 接入图灵机器人
Unity 接入图灵机器人
2022-06-26 08:41:00 【LixiSchool】
封装:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System;
using System.Text;
public class TuLingMgr : MonoBehaviour {
private string url = "http://openapi.tuling123.com/openapi/api/v2";
private string apiKey = "2801fb36fe654af8b600816dee258338";
private string userId = "198119";
/// <summary>
/// 获取答案
/// </summary>
/// <param name="message"></param>
public void GetAnswerByQuestion(string question,Action<string> finishHandle)
{
StartCoroutine(HttpPost(question,finishHandle));
}
IEnumerator HttpPost(string message, Action<string> finishHandle)
{
//JsonData可以表示JsonObject{},也可以表示JsonArry[]//4e03ee9c4e8cc2af//"1512267543"
JsonData request = new JsonData();
//perception
request["perception"] = new JsonData();
request["perception"]["inputText"] = new JsonData();
request["perception"]["inputText"]["text"] = message;
//userInfo
request["userInfo"] = new JsonData();
request["userInfo"]["apiKey"] = apiKey;
request["userInfo"]["userId"] = userId;
//JsonMapper.ToJson(request)
//将Json对象转为Json字符串,直接ToString容易出错
//将Json字符串转为字节数组
//进行一个网络推送
WWW post = new WWW(url, Encoding.UTF8.GetBytes(JsonMapper.ToJson(request)));
yield return post;
Debug.Log(post.text);
JsonData response = JsonMapper.ToObject(post.text);
string result = response["results"][0]["values"]["text"].ToString();
Debug.Log(result);
if (finishHandle!=null)
{
finishHandle(result);
}
}
private static volatile TuLingMgr instance;
private static GameObject _container;
private static object syncRoot = new object();
public static TuLingMgr Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
TuLingMgr[] instance1 = FindObjectsOfType<TuLingMgr>();
if (instance1 != null)
{
for (var i = 0; i < instance1.Length; i++)
{
Destroy(instance1[i].gameObject);
}
}
}
}
GameObject go = new GameObject(typeof(TuLingMgr).Name);
_container = go;
DontDestroyOnLoad(go);
instance = go.AddComponent<TuLingMgr>();
}
return instance;
}
}
public virtual void Awake()
{
TuLingMgr t = gameObject.GetComponent<TuLingMgr>();
if (t == null)
t = gameObject.AddComponent<TuLingMgr>();
if (instance == null)
{
DontDestroyOnLoad(gameObject);
instance = t;
}
if (instance != t)
{
MonoBehaviour[] monos = gameObject.GetComponents<MonoBehaviour>();
if (monos.Length > 1)
{
Destroy(t);
}
else
{
Destroy(gameObject);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.Text;
using UnityEngine.UI;
public class TuLingTest : MonoBehaviour
{
private string url = "http://openapi.tuling123.com/openapi/api/v2";
private string apiKey = "你的apiKey";
private string userId = "你的用户名id";
public InputField SpeakInputText; //你说的话
public Text text; //一问一答 呈现
private void Update()
{
if (Input.GetKeyDown(KeyCode.A)) //按A发送消息,(鼠标要移出输入框)
{
HttpPostFunc(SpeakInputText.text);
}
}
public void HttpPostFunc(string message)
{
StartCoroutine(HttpPost(message));
}
IEnumerator HttpPost(string message)
{
//JsonData可以表示JsonObject{},也可以表示JsonArry[]//4e03ee9c4e8cc2af//"1512267543"
JsonData request = new JsonData();
//perception
request["perception"] = new JsonData();
request["perception"]["inputText"] = new JsonData();
request["perception"]["inputText"]["text"] = message;
//userInfo
request["userInfo"] = new JsonData();
request["userInfo"]["apiKey"] = apiKey;
request["userInfo"]["userId"] = userId;
//JsonMapper.ToJson(request)
//将Json对象转为Json字符串,直接ToString容易出错
//将Json字符串转为字节数组
//进行一个网络推送
WWW post = new WWW(url, Encoding.UTF8.GetBytes(JsonMapper.ToJson(request)));
yield return post;
Debug.Log(post.text);
JsonData response = JsonMapper.ToObject(post.text);
string result = response["results"][0]["values"]["text"].ToString();
Debug.Log(result);
text.text += "我:" + message + "\n" + "机器人:" + result + "\n";
}
}边栏推荐
- 在哪个软件上开户比较安全
- 1.21 study gradient descent and normal equation
- ThreadLocal
- 【MATLAB GUI】 键盘回调中按键识别符查找表
- In automated testing, there are three commonly used waiting methods: sleep, implicitly\wait, and expected\u conditions
- 攔截器與過濾器的實現代碼
- 拦截器与过滤器的实现代码
- Speckle denoising method for ultrasonic image
- ImportError: ERROR: recursion is detected during loading of “cv2“ binary extensions. Check OpenCV in
- [qnx hypervisor 2.2 user manual]12.1 terminology (I)
猜你喜欢

20220623 getting started with Adobe Illustrator

Regular Expression 正则表达式

Polka lines code recurrence

phpcms v9手机访问电脑站一对一跳转对应手机站页面插件

Slider verification - personal test (JD)

In depth study paper reading target detection (VII) Chinese version: yolov4 optimal speed and accuracy of object detection

设置QCheckbox 样式的注意事项

框架跳转导致定位失败的解决方法

实践是成为网工最快的方法,网络工程师实战项目整理

Live review | smardaten lihongfei interprets the Research Report on China's low / no code industry: the wind direction has changed
随机推荐
dedecms小程序插件正式上线,一键安装无需任何php或sql基础
Nebula diagram_ Object detection and measurement_ nanyangjx
pgsql_ UDF01_ jx
phpcms小程序接口新增万能接口get_diy.php
Yolov5进阶之五GPU环境搭建
【程序的编译和预处理】
[IVI] 15.1.2 system stability optimization (lmkd Ⅱ) psi pressure stall information
Solution to the encoding problem encountered by the crawler when requesting get/post
[QNX Hypervisor 2.2用户手册]12.2 术语(二)
小程序首页加载之前加载其他相关资源或配置(小程序的promise应用)
微信小程序如何转换成百度小程序
[QNX Hypervisor 2.2用户手册]12.1 术语(一)
框架跳转导致定位失败的解决方法
Efficiency thesis Reading 1
Particles and sound effect system in games104 music 12 game engine
cookie session 和 token
HDU - 6225 little boxes (\u int128)
基于SSM的电脑商城
【MATLAB GUI】 键盘回调中按键识别符查找表
Notes on setting qccheckbox style