当前位置:网站首页>Unity connects to Turing robot
Unity connects to Turing robot
2022-06-26 09:06:00 【LixiSchool】
encapsulation :
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>
/// Get answers
/// </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 Can be said JsonObject{}, It can also express 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)
// take Json Object to Json character string , direct ToString It's easy to make mistakes
// take Json Convert string to byte array
// Conduct a network push
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 = " Yours apiKey";
private string userId = " Your username id";
public InputField SpeakInputText; // What you said
public Text text; // One question and one answer present
private void Update()
{
if (Input.GetKeyDown(KeyCode.A)) // Press A Send a message ,( Move the mouse out of the input box )
{
HttpPostFunc(SpeakInputText.text);
}
}
public void HttpPostFunc(string message)
{
StartCoroutine(HttpPost(message));
}
IEnumerator HttpPost(string message)
{
//JsonData Can be said JsonObject{}, It can also express 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)
// take Json Object to Json character string , direct ToString It's easy to make mistakes
// take Json Convert string to byte array
// Conduct a network push
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 += " I :" + message + "\n" + " robot :" + result + "\n";
}
}边栏推荐
- MySQL在服务里找不到(未卸载)
- Section IV HQL execution process
- Phpcms V9 mobile phone access computer station one-to-one jump to the corresponding mobile phone station page plug-in
- 浅谈一下Type-C接口发展历程
- Data warehouse (3) star model and dimension modeling of data warehouse modeling
- How to set the shelves and windows, and what to pay attention to in the optimization process
- The solution of positioning failure caused by framework jump
- 设置QCheckbox 样式的注意事项
- Polka lines code recurrence
- Detailed explanation of traditional image segmentation methods
猜你喜欢

Segmentation of structured light images using segmentation network

phpcms小程序插件教程网站正式上线

直播回顾 | smardaten李鸿飞解读中国低/无代码行业研究报告:风向变了

Section IV HQL execution process

dedecms小程序插件正式上线,一键安装无需任何php或sql基础

uniapp用uParse实现解析后台的富文本编辑器的内容及修改uParse样式

phpcms v9商城模块(修复自带支付宝接口bug)

Yolov5进阶之一摄像头实时采集识别

Drawing with MATLAB (2) -- color ring

Upgrade phpcms applet plug-in API interface to 4.3 (add batch acquisition interface, search interface, etc.)
随机推荐
PD快充磁吸移动电源方案
Live review | smardaten lihongfei interprets the Research Report on China's low / no code industry: the wind direction has changed
什么是乐观锁,什么是悲观锁
20220213 Cointegration
Unity WebGL发布无法运行问题
20220623 getting started with Adobe Illustrator
Ultrasonic image segmentation
Slider verification - personal test (JD)
如何利用最少的钱,快速打开淘宝流量入口?
[qnx hypervisor 2.2 user manual]12.2 terminology (II)
phpcms v9后台增加阅读量字段,可任意修改阅读量
torch. fft
ThreadLocal
Drawing with MATLAB (2) -- color ring
Practice is the fastest way to become a network engineer
1.Intro_ Math (white board derivation and reprint of station B)
1.21 study gradient descent and normal equation
Phpcms V9 mall module (fix the Alipay interface Bug)
yolov5进阶之零环境快速创建及测试
唯品会工作实践 : Json的deserialization应用