ERNIEBotManager.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 backtTimes;
  23. private string oldMyt;
  24. private string oldback;
  25. private float interval;
  26. private const string RECORD_AUDIO_PERMISSION = "android.permission.RECORD_AUDIO";
  27. private bool hasRecordAudioPermission = false;
  28. private bool isWakeup = false;
  29. private Vector3 irobotPos;
  30. private void Awake()
  31. {
  32. animator = Irobot.GetComponent<Animator>();
  33. }
  34. private void OnEnable()
  35. {
  36. // transform.LookAt(API_GSXR_Slam.GSXR_Get_Head());
  37. StartCoroutine(PlayAnimator("Waving", 1f));
  38. }
  39. void Start()
  40. {
  41. // 检查是否已经授权了麦克风权限
  42. hasRecordAudioPermission = Permission.HasUserAuthorizedPermission(RECORD_AUDIO_PERMISSION);
  43. if (!hasRecordAudioPermission)
  44. {
  45. // 请求麦克风权限
  46. Permission.RequestUserPermission(RECORD_AUDIO_PERMISSION);
  47. }
  48. isInit = true;
  49. isCollect = false;
  50. times = 0;
  51. oldMyt = null;
  52. oldback = null;
  53. backtTimes = 0;
  54. interval = 1f;
  55. irobotPos = new Vector3(0, -1, 6);
  56. HttpTool.Instance.Get("https://fat1.qulivr.com/mr-navigator/v1/", "", (string getInterval) => {
  57. Debug.Log(getInterval);
  58. if (getInterval == null)
  59. Debug.LogError(" HttpTool Get Interval Error!!! ");
  60. JsonData data = JsonMapper.ToObject(getInterval);
  61. Debug.Log(data["code"]);
  62. if ((int)data["code"] == 200)
  63. {
  64. Debug.Log(data["rate"].ToString());
  65. interval = float.Parse(data["rate"].ToString());
  66. irobotPos = new Vector3(0, float.Parse(data["defaultHeight"].ToString()), float.Parse(data["defaultZAxisValue"].ToString()));
  67. }
  68. else
  69. Debug.LogError(" HttpTool Get Interval Error!!! ");
  70. });
  71. YiYanBack _setAndrodCallback = new YiYanBack();
  72. #if UNITY_ANDROID
  73. UnityToJava.initSpeek(_setAndrodCallback);
  74. #endif
  75. // _setAndrodCallback.onBackText("asdfkjalkdjfl;akjdfl;ksajl;fkajsl;dfkjas;dfknsdkofjas90idfjals");
  76. //string msg = "asdfkjalkdjfl;akjdfl;ksajl;fkajsl;dfkjas;dfknsdkofjas90idfjals";
  77. //for (int i = 0; i < msg.Length; i++)
  78. //{
  79. // YiYanBack.BackText.Enqueue(msg[i]);
  80. //}
  81. // StartCoroutine(Speek(3f));
  82. // StartSpeek();
  83. StartCoroutine(OpenSpeek(3f));
  84. }
  85. private IEnumerator Speek(float times)
  86. {
  87. yield return new WaitForSeconds(times);
  88. UnityToJava.yiYanClass.CallStatic("SpeekOff", "HelloWord1234568999");
  89. }
  90. private void Update()
  91. {
  92. backtTimes += Time.deltaTime;
  93. if(backtTimes> interval && YiYanBack.BackText.Count>0)
  94. {
  95. backt.text += YiYanBack.BackText.Dequeue();
  96. backtTimes = 0;
  97. times2 = 0;
  98. oldback = backt.text;
  99. }
  100. if (YiYanBack.BackTextlist.Count > 0)
  101. {
  102. //backt.text += YiYanBack.BackTextlist.Dequeue();
  103. YiYanBack.BackTextlist.Dequeue();
  104. //times2 = 0;
  105. //Debug.Log(backt.text);
  106. //oldback = backt.text;
  107. StartCoroutine(PlayAnimator("Point_left", 1f));
  108. }
  109. if (YiYanBack.BackFINALTextlist.Count > 0)
  110. {
  111. backfinalt.text = YiYanBack.BackFINALTextlist.Dequeue();
  112. send(backfinalt.text);
  113. }
  114. if (YiYanBack.MyTextlist.Count > 0)
  115. {
  116. myt.text = YiYanBack.MyTextlist.Dequeue();
  117. oldMyt = myt.text;
  118. }
  119. if (isCollect)
  120. {
  121. Debug.Log(isCollect);
  122. isCollect = false;
  123. StartSpeek();
  124. }
  125. //else
  126. //{
  127. // ui.gameObject.SetActive(true);
  128. //}
  129. if (oldback != null && oldback == backt.text)
  130. {
  131. times2 += Time.deltaTime;
  132. if (times2 > 3f)
  133. {
  134. oldback = null;
  135. times2 = 0;
  136. StartSpeek();
  137. // StartCoroutine(OpenSpeek(1f));
  138. }
  139. }
  140. else
  141. times2 = 0;
  142. if (oldMyt!=null && myt.text == oldMyt)
  143. {
  144. if(!isWakeup&&myt.text.Contains("小度小度"))
  145. {
  146. isWakeup = true;
  147. transform.position = player.position + player.forward * irobotPos.z;
  148. transform.position = new Vector3(transform.position.x, irobotPos.y, transform.position.z);
  149. transform.LookAt(player);
  150. transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
  151. Irobot.gameObject.SetActive(true);
  152. myt.text = "";
  153. backfinalt.text = "";
  154. // StartCoroutine(OpenSpeek(0.3f));
  155. StartSpeek();
  156. }
  157. times += Time.deltaTime;
  158. if (times > 3f)
  159. {
  160. oldMyt = null;
  161. times = 0;
  162. send(myt.text);
  163. }
  164. }else
  165. {
  166. times = 0;
  167. }
  168. }
  169. private void OnApplicationPause(bool pause)
  170. {
  171. if (isInit)
  172. {
  173. if (pause)
  174. {
  175. UnityToJava.yiYanClass.CallStatic("onPauseJava");
  176. }
  177. else
  178. {
  179. UnityToJava.yiYanClass.CallStatic("onResumeJava");
  180. }
  181. }
  182. }
  183. private void OnDestroy()
  184. {
  185. UnityToJava.yiYanClass.CallStatic("onDestroyJava");
  186. }
  187. public void StopSpeek()
  188. {
  189. UnityToJava.yiYanClass.CallStatic("StopSpeek");
  190. Debug.Log("StopSpeek====");
  191. }
  192. public void StartSpeek()
  193. {
  194. UnityToJava.yiYanClass.CallStatic("StartSpeek");
  195. Debug.Log("StartSpeek====");
  196. //myt.text = "";
  197. StartCoroutine(AudioIconSetting(1f, true));
  198. }
  199. public void send(string str)
  200. {
  201. if(isWakeup)
  202. {
  203. if (str.Contains("小度拜拜"))
  204. {
  205. isWakeup = false;
  206. Irobot.gameObject.SetActive(false);
  207. StartSpeek();
  208. }
  209. else
  210. {
  211. UnityToJava.yiYanClass.CallStatic("SendText", str);
  212. Debug.Log("Send " + str);
  213. StartCoroutine(AudioIconSetting(1f, false));
  214. }
  215. }
  216. backt.text = "";
  217. }
  218. private IEnumerator OpenSpeek( float times)
  219. {
  220. yield return new WaitForSeconds(times);
  221. isCollect = true;
  222. }
  223. private IEnumerator PlayAnimator( string Animator_Name ,float times)
  224. {
  225. animator.SetBool(Animator_Name, true);
  226. yield return new WaitForSeconds(times);
  227. animator.SetBool(Animator_Name, false);
  228. }
  229. private IEnumerator AudioIconSetting(float times , bool state)
  230. {
  231. yield return new WaitForSeconds(times);
  232. ui.gameObject.SetActive(state);
  233. }
  234. }