ERNIEBotManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. float z = float.Parse(data["defaultZAxisValue"].ToString());
  67. float y = float.Parse(data["defaultHeight"].ToString());
  68. z = (string.IsNullOrEmpty(data["defaultZAxisValue"].ToString())) ? 1 : z;
  69. y = (string.IsNullOrEmpty(data["defaultHeight"].ToString())) ? 2 : y;
  70. irobotPos = new Vector3(0, y, z);
  71. }
  72. else
  73. Debug.LogError(" HttpTool Get Interval Error!!! ");
  74. });
  75. YiYanBack _setAndrodCallback = new YiYanBack();
  76. #if UNITY_ANDROID
  77. UnityToJava.initSpeek(_setAndrodCallback);
  78. #endif
  79. // _setAndrodCallback.onBackText("asdfkjalkdjfl;akjdfl;ksajl;fkajsl;dfkjas;dfknsdkofjas90idfjals");
  80. //string msg = "asdfkjalkdjfl;akjdfl;ksajl;fkajsl;dfkjas;dfknsdkofjas90idfjals";
  81. //for (int i = 0; i < msg.Length; i++)
  82. //{
  83. // YiYanBack.BackText.Enqueue(msg[i]);
  84. //}
  85. // StartCoroutine(Speek(3f));
  86. // StartSpeek();
  87. StartCoroutine(OpenSpeek(3f));
  88. }
  89. private IEnumerator Speek(float times)
  90. {
  91. yield return new WaitForSeconds(times);
  92. // UnityToJava.yiYanClass.CallStatic("SpeekOff", "HelloWord1234568999");
  93. }
  94. private void Update()
  95. {
  96. backtTimes += Time.deltaTime;
  97. if(backtTimes> interval && YiYanBack.BackText.Count>0)
  98. {
  99. backt.text += YiYanBack.BackText.Dequeue();
  100. backtTimes = 0;
  101. times2 = 0;
  102. oldback = backt.text;
  103. }
  104. if (YiYanBack.BackTextlist.Count > 0)
  105. {
  106. //backt.text += YiYanBack.BackTextlist.Dequeue();
  107. YiYanBack.BackTextlist.Dequeue();
  108. //times2 = 0;
  109. //Debug.Log(backt.text);
  110. //oldback = backt.text;
  111. // StartCoroutine(PlayAnimator("Point_left", 1f));
  112. }
  113. if (YiYanBack.BackFINALTextlist.Count > 0)
  114. {
  115. backfinalt.text = YiYanBack.BackFINALTextlist.Dequeue();
  116. send(backfinalt.text);
  117. }
  118. if (YiYanBack.MyTextlist.Count > 0)
  119. {
  120. myt.text = YiYanBack.MyTextlist.Dequeue();
  121. oldMyt = myt.text;
  122. }
  123. if (oldback != null && oldback == backt.text)
  124. {
  125. times2 += Time.deltaTime;
  126. if (times2 > 3f)
  127. {
  128. oldback = null;
  129. times2 = 0;
  130. // StartSpeek();
  131. // StartCoroutine(OpenSpeek(1f));
  132. }
  133. }
  134. else
  135. times2 = 0;
  136. if (oldMyt!=null && myt.text == oldMyt)
  137. {
  138. if(!isWakeup&&myt.text.Contains("小度小度"))
  139. {
  140. isWakeup = true;
  141. transform.position = player.position + player.forward * irobotPos.z;
  142. transform.position = new Vector3(transform.position.x, irobotPos.y, transform.position.z);
  143. transform.LookAt(player);
  144. transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
  145. Irobot.gameObject.SetActive(true);
  146. myt.text = "";
  147. backfinalt.text = "";
  148. backt.text = "";
  149. // StartCoroutine(OpenSpeek(0.3f));
  150. StartSpeek();
  151. }
  152. times += Time.deltaTime;
  153. if(times>1f)
  154. StartCoroutine(AudioIconSetting(0, false));
  155. if (times > 3f)
  156. {
  157. oldMyt = null;
  158. times = 0;
  159. send(myt.text);
  160. }
  161. }else
  162. {
  163. times = 0;
  164. }
  165. }
  166. private void OnApplicationPause(bool pause)
  167. {
  168. if (isInit)
  169. {
  170. if (pause)
  171. {
  172. UnityToJava.yiYanClass.CallStatic("onPauseJava");
  173. }
  174. else
  175. {
  176. UnityToJava.yiYanClass.CallStatic("onResumeJava");
  177. }
  178. }
  179. }
  180. private void OnDestroy()
  181. {
  182. UnityToJava.yiYanClass.CallStatic("onDestroyJava");
  183. }
  184. public void StopSpeek()
  185. {
  186. UnityToJava.yiYanClass.CallStatic("StopSpeek");
  187. Debug.Log("StopSpeek====");
  188. }
  189. public void StartSpeekOnClick()
  190. {
  191. StartSpeek();
  192. YiYanBack.BackText.Clear();
  193. YiYanBack.BackText = new Queue<char>();
  194. myt.text = "";
  195. backt.text = "";
  196. }
  197. public void StartSpeek()
  198. {
  199. UnityToJava.yiYanClass.CallStatic("StartSpeek");
  200. Debug.Log("StartSpeek====");
  201. StartCoroutine(AudioIconSetting(0.7f, true));
  202. }
  203. public void send(string str)
  204. {
  205. StartCoroutine(AudioIconSetting(0, false));
  206. if (isWakeup)
  207. {
  208. if (myt.text.Contains("小度拜拜"))
  209. {
  210. isWakeup = false;
  211. Irobot.gameObject.SetActive(false);
  212. StartSpeek();
  213. }
  214. else
  215. {
  216. UnityToJava.yiYanClass.CallStatic("SendText", str);
  217. Debug.Log("Send " + str);
  218. }
  219. }
  220. else
  221. {
  222. StartSpeek();
  223. }
  224. backt.text = "";
  225. myt.text = "";
  226. }
  227. private IEnumerator OpenSpeek( float times)
  228. {
  229. yield return new WaitForSeconds(times);
  230. StartSpeek();
  231. }
  232. private IEnumerator PlayAnimator( string Animator_Name ,float times)
  233. {
  234. // animator.SetBool(Animator_Name, true);
  235. yield return new WaitForSeconds(times);
  236. //animator.SetBool(Animator_Name, false);
  237. }
  238. private IEnumerator AudioIconSetting(float times , bool state)
  239. {
  240. //if(state)
  241. //{
  242. // yield return new WaitForSeconds(0.5f);
  243. //}
  244. yield return new WaitForSeconds(times);
  245. ui.gameObject.SetActive(state);
  246. }
  247. }