ERNIEBotManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using static TestFile;
  6. using LitJson;
  7. using UnityEngine.Android;
  8. public class ERNIEBotManager : MonoBehaviour
  9. {
  10. public bool isInit;
  11. public Text backt;
  12. public Text backfinalt;
  13. public Text myt;
  14. public RectTransform scrollRect;
  15. public GameObject ui;
  16. public Transform Irobot;
  17. // private Animator animator;
  18. public Transform player;
  19. private bool isCollect;
  20. private float times;
  21. private float times2;
  22. private float times3;
  23. private float backtTimes;
  24. private string oldMyt;
  25. private string oldback;
  26. private float interval;
  27. private const string RECORD_AUDIO_PERMISSION = "android.permission.RECORD_AUDIO";
  28. private bool hasRecordAudioPermission = false;
  29. private bool isWakeup = false;
  30. private Vector3 irobotPos;
  31. private void Awake()
  32. {
  33. //animator = Irobot.GetComponent<Animator>();
  34. }
  35. private void OnEnable()
  36. {
  37. // transform.LookAt(API_GSXR_Slam.GSXR_Get_Head());
  38. // StartCoroutine(PlayAnimator("Waving", 1f));
  39. }
  40. void Start()
  41. {
  42. // 检查是否已经授权了麦克风权限
  43. hasRecordAudioPermission = Permission.HasUserAuthorizedPermission(RECORD_AUDIO_PERMISSION);
  44. if (!hasRecordAudioPermission)
  45. {
  46. // 请求麦克风权限
  47. Permission.RequestUserPermission(RECORD_AUDIO_PERMISSION);
  48. }
  49. isInit = true;
  50. isCollect = false;
  51. times = 0;
  52. oldMyt = null;
  53. oldback = null;
  54. backtTimes = 0;
  55. interval = 0.2f;
  56. irobotPos = new Vector3(0, -1, 6);
  57. HttpTool.Instance.Get("https://fat1.qulivr.com/mr-navigator/v1/", "", (string getInterval) => {
  58. Debug.Log(getInterval);
  59. if (getInterval == null)
  60. Debug.LogError(" HttpTool Get Interval Error!!! ");
  61. JsonData data = JsonMapper.ToObject(getInterval);
  62. Debug.Log(data["code"]);
  63. if ((int)data["code"] == 200)
  64. {
  65. Debug.Log(data["rate"].ToString());
  66. interval = float.Parse(data["rate"].ToString());
  67. float z = float.Parse(data["defaultZAxisValue"].ToString());
  68. float y = float.Parse(data["defaultHeight"].ToString());
  69. z = (string.IsNullOrEmpty(data["defaultZAxisValue"].ToString())) ? 1 : z;
  70. y = (string.IsNullOrEmpty(data["defaultHeight"].ToString())) ? 2 : y;
  71. irobotPos = new Vector3(0, y, z);
  72. }
  73. else
  74. Debug.LogError(" HttpTool Get Interval Error!!! ");
  75. });
  76. YiYanBack _setAndrodCallback = new YiYanBack();
  77. #if UNITY_ANDROID
  78. UnityToJava.initSpeek(_setAndrodCallback);
  79. isInit = true;
  80. #endif
  81. // StartCoroutine(OpenSpeek(3f));
  82. }
  83. private IEnumerator Speek(float times)
  84. {
  85. yield return new WaitForSeconds(times);
  86. // UnityToJava.yiYanClass.CallStatic("SpeekOff", "HelloWord1234568999");
  87. }
  88. private void Update()
  89. {
  90. backtTimes += Time.deltaTime;
  91. if(backtTimes> interval && YiYanBack.BackText.Count>0)
  92. {
  93. backt.text += YiYanBack.BackText.Dequeue();
  94. backtTimes = 0;
  95. times2 = 0;
  96. oldback = backt.text;
  97. }
  98. if (YiYanBack.BackTextlist.Count > 0)
  99. {
  100. //backt.text += YiYanBack.BackTextlist.Dequeue();
  101. YiYanBack.BackTextlist.Dequeue();
  102. //times2 = 0;
  103. //Debug.Log(backt.text);
  104. //oldback = backt.text;
  105. // StartCoroutine(PlayAnimator("Point_left", 1f));
  106. }
  107. if (YiYanBack.BackFINALTextlist.Count > 0)
  108. {
  109. backfinalt.text = YiYanBack.BackFINALTextlist.Dequeue();
  110. send(backfinalt.text);
  111. }
  112. if (YiYanBack.MyTextlist.Count > 0)
  113. {
  114. myt.text = YiYanBack.MyTextlist.Dequeue();
  115. oldMyt = myt.text;
  116. //HttpsSendLog.Instance.SendLog("MYT", myt.text);
  117. if (myt.text.Contains("唤醒成功") && myt.text.Contains("小度小度"))
  118. {
  119. myt.text = "";
  120. if (!Irobot.gameObject.activeSelf)
  121. {
  122. transform.position = player.position + player.forward * irobotPos.z;
  123. transform.position = new Vector3(transform.position.x, irobotPos.y, transform.position.z);
  124. transform.LookAt(player);
  125. transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
  126. Irobot.gameObject.SetActive(true);
  127. isWakeup = true;
  128. }
  129. StartCoroutine(AudioIconSetting(0, true));
  130. YiYanBack.BackText.Clear();
  131. YiYanBack.BackText = new Queue<char>();
  132. backt.text = "";
  133. }
  134. else
  135. isWakeup = false;
  136. times2 = 0;
  137. times3 = 0;
  138. backt.text = "";
  139. HttpsSendLog.Instance.SendLog("MYT", oldMyt+" "+ myt.text);
  140. }
  141. if(oldback!=null &&oldback == backt.text)
  142. {
  143. times2 += Time.deltaTime;
  144. if (times2>5f)
  145. {
  146. times2 = 0;
  147. isWakeup = false;
  148. Irobot.gameObject.SetActive(false);
  149. }
  150. }
  151. if (oldMyt != null && myt.text == oldMyt)
  152. {
  153. HttpsSendLog.Instance.SendLog("MYT", " 计时开始 " + times);
  154. times += Time.deltaTime;
  155. if (times > 3f)
  156. {
  157. send(myt.text);
  158. oldMyt = null;
  159. times = 0;
  160. }
  161. }
  162. if (isWakeup)
  163. {
  164. times3 += Time.deltaTime;
  165. if (times3 > 10f)
  166. {
  167. times3 = 0;
  168. isWakeup = false;
  169. Irobot.gameObject.SetActive(false);
  170. }
  171. }
  172. #region old
  173. /*
  174. //if (oldback != null && oldback == backt.text)
  175. //{
  176. // times2 += Time.deltaTime;
  177. // if (times2 > 3f)
  178. // {
  179. // oldback = null;
  180. // times2 = 0;
  181. // StartSpeek();
  182. // // StartCoroutine(OpenSpeek(1f));
  183. // }
  184. //}
  185. //else
  186. // times2 = 0;
  187. //if (oldMyt!=null && myt.text == oldMyt)
  188. //{
  189. // if(!isWakeup&&myt.text.Contains("小度小度"))
  190. // {
  191. // isWakeup = true;
  192. // transform.position = player.position + player.forward * irobotPos.z;
  193. // transform.position = new Vector3(transform.position.x, irobotPos.y, transform.position.z);
  194. // transform.LookAt(player);
  195. // transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
  196. // Irobot.gameObject.SetActive(true);
  197. // myt.text = "";
  198. // backfinalt.text = "";
  199. // backt.text = "";
  200. // // StartCoroutine(OpenSpeek(0.3f));
  201. // StartSpeek();
  202. // }
  203. // times += Time.deltaTime;
  204. // if(times>1f)
  205. // StartCoroutine(AudioIconSetting(0, false));
  206. // if (times > 3f)
  207. // {
  208. // oldMyt = null;
  209. // times = 0;
  210. // send(myt.text);
  211. // }
  212. //}else
  213. //{
  214. // times = 0;
  215. //}
  216. */
  217. #endregion
  218. }
  219. private void OnApplicationPause(bool pause)
  220. {
  221. if (isInit)
  222. {
  223. if (pause)
  224. {
  225. UnityToJava.yiYanClass.CallStatic("onPauseJava");
  226. }
  227. else
  228. {
  229. UnityToJava.yiYanClass.CallStatic("onResumeJava");
  230. }
  231. }
  232. }
  233. private void OnDestroy()
  234. {
  235. UnityToJava.yiYanClass.CallStatic("onDestroyJava");
  236. }
  237. public void StopSpeek()
  238. {
  239. UnityToJava.yiYanClass.CallStatic("StopSpeek");
  240. Debug.Log("StopSpeek====");
  241. }
  242. public void StartSpeekOnClick()
  243. {
  244. StartSpeek();
  245. YiYanBack.BackText.Clear();
  246. YiYanBack.BackText = new Queue<char>();
  247. myt.text = "";
  248. backt.text = "";
  249. }
  250. public void StartSpeek()
  251. {
  252. //UnityToJava.yiYanClass.CallStatic("StartSpeek");
  253. //Debug.Log("StartSpeek====");
  254. //StartCoroutine(AudioIconSetting(0.7f, true));
  255. }
  256. public void send(string str)
  257. {
  258. //StartCoroutine(AudioIconSetting(0, false));
  259. //if (isWakeup)
  260. //{
  261. // if (myt.text.Contains("小度拜拜"))
  262. // {
  263. // isWakeup = false;
  264. // Irobot.gameObject.SetActive(false);
  265. // StartSpeek();
  266. // }
  267. // else
  268. // {
  269. // UnityToJava.yiYanClass.CallStatic("SendText", str);
  270. // Debug.Log("Send " + str);
  271. // }
  272. //}
  273. //else
  274. //{
  275. // StartSpeek();
  276. //}
  277. StartCoroutine(AudioIconSetting(0, false));
  278. HttpsSendLog.Instance.SendLog("SendText", myt.text);
  279. UnityToJava.yiYanClass.CallStatic("SendText", str);
  280. backt.text = "";
  281. oldMyt = null;
  282. //myt.text = "";
  283. }
  284. private IEnumerator OpenSpeek( float times)
  285. {
  286. yield return new WaitForSeconds(times);
  287. StartSpeek();
  288. }
  289. private IEnumerator PlayAnimator( string Animator_Name ,float times)
  290. {
  291. // animator.SetBool(Animator_Name, true);
  292. yield return new WaitForSeconds(times);
  293. //animator.SetBool(Animator_Name, false);
  294. }
  295. private IEnumerator AudioIconSetting(float times , bool state)
  296. {
  297. //if(state)
  298. //{
  299. // yield return new WaitForSeconds(0.5f);
  300. //}
  301. yield return new WaitForSeconds(times);
  302. ui.gameObject.SetActive(state);
  303. }
  304. }
  305. public class YiYanBack : AndroidJavaProxy
  306. {
  307. public static Queue<string> BackTextlist = new Queue<string>();
  308. public static Queue<string> BackFINALTextlist = new Queue<string>();
  309. public static Queue<string> MyTextlist = new Queue<string>();
  310. public static Queue<char> BackText = new Queue<char>();
  311. public YiYanBack() : base("com.xiaodu.dueros.magictool.demo.YiYanBack")
  312. {
  313. }
  314. public void onBackText(string msg)
  315. {
  316. Debug.Log("onBackText====>" + msg);
  317. BackTextlist.Enqueue(msg);
  318. HttpsSendLog.Instance.SendLog("F", msg);
  319. for (int i = 0; i < msg.Length; i++)
  320. {
  321. BackText.Enqueue(msg[i]);
  322. }
  323. }
  324. public void onMyFINALText(string msg)
  325. {
  326. Debug.Log("onBackFINALText====>" + msg);
  327. BackFINALTextlist.Enqueue(msg);
  328. }
  329. public void onMyText(string msg)
  330. {
  331. Debug.Log("onMyText====>" + msg);
  332. MyTextlist.Enqueue(msg);
  333. }
  334. public void onWakeupSucceed(string msg)
  335. {
  336. Debug.Log("onMyText====>" + msg);
  337. MyTextlist.Enqueue(msg);
  338. }
  339. public void onWakeupFailed(string msg)
  340. {
  341. Debug.Log("onMyText====>" + msg);
  342. MyTextlist.Enqueue(msg);
  343. }
  344. }