当前位置:网站首页>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";
}
}边栏推荐
猜你喜欢

phpcms小程序插件api接口升级到4.3(新增批量获取接口、搜索接口等)

Nebula diagram_ Object detection and measurement_ nanyangjx

Live review | smardaten lihongfei interprets the Research Report on China's low / no code industry: the wind direction has changed

Matlab drawing checkerboard (camera calibration)

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

Particles and sound effect system in games104 music 12 game engine

MySQL在服务里找不到(未卸载)

20220623 Adobe Illustrator入门

External sorting and heap size knowledge

Srv6---is-is extension
随机推荐
什么是乐观锁,什么是悲观锁
Tensorboard
浅谈一下Type-C接口发展历程
In automated testing, there are three commonly used waiting methods: sleep, implicitly\wait, and expected\u conditions
Machine learning (Part 2)
Clion installation + MinGW configuration + opencv installation
SRv6----IS-IS扩展
Google Chrome collection
Code de mise en œuvre de l'intercepteur et du filtre
phpcms v9商城模块(修复自带支付宝接口bug)
Machine learning (Part 1)
Sqoop merge usage
Data warehouse (3) star model and dimension modeling of data warehouse modeling
远程工作的一些命令
[program compilation and pretreatment]
修复小程序富文本组件不支持video视频封面、autoplay、controls等属性问题
phpcms手机站模块实现自定义伪静态设置
Yolov5 advanced level 2 installation of labelimg
Corn image segmentation count_ nanyangjx
[300+ continuous sharing of selected interview questions from large manufacturers] column on interview questions of big data operation and maintenance (I)