Unity 接入图灵机器人

封装:

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";
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值