当前位置:网站首页>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";
}
}边栏推荐
- 【MATLAB GUI】 键盘回调中按键识别符查找表
- Checkerboard generation + camera calibration + stereo matching
- PD快充磁吸移动电源方案
- 关于小程序tabbar不支持传参的处理办法
- In depth study paper reading target detection (VII) Chinese version: yolov4 optimal speed and accuracy of object detection
- [QNX Hypervisor 2.2用户手册]12.1 术语(一)
- How to convert wechat applet into Baidu applet
- Detailed explanation of traditional image segmentation methods
- 攔截器與過濾器的實現代碼
- Solution to the encoding problem encountered by the crawler when requesting get/post
猜你喜欢
![[300+ continuous sharing of selected interview questions from large manufacturers] column on interview questions of big data operation and maintenance (I)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300+ continuous sharing of selected interview questions from large manufacturers] column on interview questions of big data operation and maintenance (I)

Graduation thesis management system based on SSM

XSS cross site scripting attack
![[cloud primordial | kubernetes chapter] go deep into the foundation of all things - container (V)](/img/67/26508edc451139cd0f4c9511ca1ed2.png)
[cloud primordial | kubernetes chapter] go deep into the foundation of all things - container (V)

phpcms v9后台文章列表增加一键推送到百度功能

Games104 Lecture 12 游戏引擎中的粒子和声效系统

phpcms手机站模块实现自定义伪静态设置

isinstance()函数用法

phpcms v9去掉phpsso模块

Detailed explanation of traditional image segmentation methods
随机推荐
Clion installation + MinGW configuration + opencv installation
基于SSM的毕业论文管理系统
Efficiency thesis Reading 1
设置QCheckbox 样式的注意事项
1.23 neural network
编程训练7-日期转换问题
自动化测试中,三种常用的等待方式,强制式(sleep) 、 隐式 ( implicitly_wait ) 、显式(expected_conditions)
力扣399【除法求值】【并查集】
Load other related resources or configurations (promise application of the applet) before loading the homepage of the applet
phpcms v9手机访问电脑站一对一跳转对应手机站页面插件
Yolov5 advanced 4 train your own data set
Use of PCL
拦截器与过滤器的实现代码
phpcms v9后台增加阅读量字段,可任意修改阅读量
1.Intro_ Math (white board derivation and reprint of station B)
2021 software university ranking crawler program
远程工作的一些命令
Phpcms V9 remove the phpsso module
Yolov5进阶之五GPU环境搭建
phpcms小程序接口新增万能接口get_diy.php