当前位置:网站首页>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";
    }
}

原网站

版权声明
本文为[LixiSchool]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260841060500.html