Skip to content

Commit 7e250e1

Browse files
committed
增加图灵聊天机器人
默认语言改为中文:Microsoft Huihui Desktop
1 parent ca5a199 commit 7e250e1

23 files changed

Lines changed: 237 additions & 43 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ Unity3d使用SAPI语音在Windows上进行文本发音.docx
5252
/Speech/bin
5353
/Speech/obj
5454
/Speech/.vs
55+
/SApiClient/Output

SApiClient/Assets/ChatRobot.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SApiClient/Assets/ChatRobot/TuLing.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
14.9 KB
Binary file not shown.

SApiClient/Assets/ChatRobot/TuLing/TuLing.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class TuLingChatRobot : MonoBehaviour
6+
{
7+
public string Url = "http://www.tuling123.com/openapi/api";
8+
public string Key = "7c4071624723cdd32213619dd69fd163";
9+
public delegate void GetResponse(string res);
10+
11+
public GetResponse OnGetResponse;
12+
13+
public void Require(string req)
14+
{
15+
StartCoroutine(_Require(req));
16+
}
17+
18+
public IEnumerator _Require(string req)
19+
{
20+
var pf = new WWWForm();
21+
pf.AddField("key", Key);
22+
pf.AddField("info", req);
23+
var www = new WWW(Url, pf);
24+
yield return www;
25+
if (www.error != null)
26+
{
27+
Debug.Log(www.error);
28+
yield break;
29+
}
30+
var text = www.text;
31+
Debug.Log(text);
32+
if (OnGetResponse != null)
33+
OnGetResponse.Invoke(text);
34+
}
35+
}
36+
37+
public class TuLingResponseBase
38+
{
39+
public int code;
40+
public string text;
41+
}
42+
43+
public class TuLingUrlResponse: TuLingResponseBase
44+
{
45+
public string url;
46+
}
47+
48+
public class TuLingNewsResponse: TuLingUrlResponse
49+
{
50+
public List<NewsData> list;
51+
52+
public class NewsData
53+
{
54+
public string article;
55+
public string source;
56+
public string icon;
57+
public string detailurl;
58+
}
59+
}
60+
61+
public class TuLingCookBookResponse : TuLingResponseBase
62+
{
63+
public List<CookBookData> list;
64+
65+
public class CookBookData
66+
{
67+
public string name;
68+
public string info;
69+
public string icon;
70+
public string detailurl;
71+
}
72+
}

SApiClient/Assets/ChatRobot/TuLing/TuLingChatRobot.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using UnityEngine;
2+
3+
public class TuLingChatRobotManager : SpeecherManager
4+
{
5+
public override void OnGUI()
6+
{
7+
base.OnGUI();
8+
9+
if (GUILayout.Button("Tuling"))
10+
{
11+
var robot = GetComponent<TuLingChatRobot>();
12+
Speecher.OnGetRecognize = robot.Require;
13+
robot.OnGetResponse = OnGetResponse;
14+
}
15+
}
16+
17+
private void OnGetResponse(string res)
18+
{
19+
var response = JsonUtility.FromJson<TuLingResponseBase>(res);
20+
Speecher.Speech(response.text);
21+
}
22+
}

SApiClient/Assets/ChatRobot/TuLing/TuLingChatRobotManager.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SApiClient/Assets/Sapi/Speecher.cs

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System.Collections;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using Stardust;
45
using UnityEngine;
6+
using UnityEngine.Events;
57

68
public class Speecher : MonoBehaviour, INetComponent
79
{
@@ -26,37 +28,11 @@ protected void Awake()
2628
m_process.Start();
2729
}
2830

29-
/***测试代码,可删除Start***/
30-
31-
public void OnGUI()
32-
{
33-
if (GUILayout.Button("Connect"))
34-
{
35-
StartCoroutine(Connect());
36-
}
37-
if (GUILayout.Button("InitServer"))
38-
{
39-
StartCoroutine(InitServer());
40-
}
41-
42-
if (GUILayout.Button("Speak"))
43-
{
44-
Speak("hello world");
45-
}
46-
47-
if (GUILayout.Button("Recognize Start"))
48-
{
49-
Recognize(true);
50-
}
51-
if (GUILayout.Button("Recognize End"))
52-
{
53-
Recognize(false);
54-
}
55-
}
56-
57-
/***测试代码,可删除End***/
58-
59-
private void Recognize(bool tf)
31+
/// <summary>
32+
/// 开始或者结束语音识别功能
33+
/// </summary>
34+
/// <param name="tf"></param>
35+
public void Recognize(bool tf)
6036
{
6137
var arg = new ByteInArg();
6238
arg.Write(3);
@@ -94,14 +70,14 @@ protected void OnDestroy()
9470
m_process = null;
9571
}
9672

97-
public static Speecher Ins;
73+
public static Speecher Ins { get; private set; }
9874

99-
public static void Speak(string str)
100-
{
101-
#if UNITY_EDITOR||UNITY_STANDALONE_WIN
102-
Ins.Speech(str);
103-
#endif
104-
}
75+
// public void Speak(string str)
76+
// {
77+
//#if UNITY_EDITOR||UNITY_STANDALONE_WIN
78+
// Ins.Speech(str);
79+
//#endif
80+
// }
10581

10682
public void Speech(string str)
10783
{
@@ -119,12 +95,30 @@ public bool NetReciveMsg(byte[] recivebuffer, int netID)
11995
{
12096
var arg = new ByteOutArg(recivebuffer);
12197
var str = arg.ReadString();
122-
UnityEngine.Debug.Log(str);
98+
ResList.Enqueue(str);
99+
//UnityEngine.Debug.Log(str);
123100
return true;
124101
}
125102

103+
public Queue<string> ResList = new Queue<string>();
104+
105+
public void Update()
106+
{
107+
if (ResList.Count > 0)
108+
{
109+
var str = ResList.Dequeue();
110+
if (OnGetRecognize != null)
111+
OnGetRecognize.Invoke(str);
112+
}
113+
}
114+
126115
public bool NetSendMsg(byte[] sendbuffer)
127116
{
128117
return m_socket.SendMsg(sendbuffer);
129118
}
119+
120+
public delegate void GetRecognize(string msg);
121+
public GetRecognize OnGetRecognize;
122+
123+
public UnityEvent<string> OnGetRecognizeAct;
130124
}

0 commit comments

Comments
 (0)