using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using static TestFile; using LitJson; using UnityEngine.Android; public class ERNIEBotManager : MonoSingleton { public bool isInit; public Text backt; public Text backfinalt; public Text myt; public RectTransform scrollRect; public GameObject ui; public Transform Irobot; // private Animator animator; public Transform player; private bool isCollect; private float times; private float times2; private float times3; private float backtTimes; private string oldMyt; private string oldback; private XunFeiYuYin xunfei; private float interval; private const string RECORD_AUDIO_PERMISSION = "android.permission.RECORD_AUDIO"; private bool hasRecordAudioPermission = false; private bool isWakeup = false; private Vector3 irobotPos; public bool isAudioPlay; private void Awake() { //animator = Irobot.GetComponent(); } private void OnEnable() { // transform.LookAt(API_GSXR_Slam.GSXR_Get_Head()); // StartCoroutine(PlayAnimator("Waving", 1f)); } void Start() { // 检查是否已经授权了麦克风权限 hasRecordAudioPermission = Permission.HasUserAuthorizedPermission(RECORD_AUDIO_PERMISSION); if (!hasRecordAudioPermission) { // 请求麦克风权限 Permission.RequestUserPermission(RECORD_AUDIO_PERMISSION); } isInit = true; isCollect = false; times = 0; oldMyt = null; oldback = null; backtTimes = 0; interval = 0.2f; irobotPos = new Vector3(0, -1, 6); HttpTool.Instance.Get("https://fat1.qulivr.com/mr-navigator/v1/", "", (string getInterval) => { Debug.Log(getInterval); if (getInterval == null) Debug.LogError(" HttpTool Get Interval Error!!! "); JsonData data = JsonMapper.ToObject(getInterval); Debug.Log(data["code"]); if ((int)data["code"] == 200) { Debug.Log(data["rate"].ToString()); interval = float.Parse(data["rate"].ToString()); float z = float.Parse(data["defaultZAxisValue"].ToString()); float y = float.Parse(data["defaultHeight"].ToString()); z = (string.IsNullOrEmpty(data["defaultZAxisValue"].ToString())) ? 1 : z; y = (string.IsNullOrEmpty(data["defaultHeight"].ToString())) ? 2 : y; irobotPos = new Vector3(0, y, z); } else Debug.LogError(" HttpTool Get Interval Error!!! "); }); YiYanBack _setAndrodCallback = new YiYanBack(); #if UNITY_ANDROID UnityToJava.initSpeek(_setAndrodCallback); isInit = true; isAudioPlay = false; xunfei = XunFeiYuYin.Init("708f4bb8", "MTQ0ZWZhMDE1ODA5NjVmMDYyODhmMTM3", "e2c01e5127d59a6ef92cef37e6255644", ""); #endif // StartCoroutine(OpenSpeek(3f)); } private IEnumerator Speek(float times) { yield return new WaitForSeconds(times); // UnityToJava.yiYanClass.CallStatic("SpeekOff", "HelloWord1234568999"); } private void Update() { backtTimes += Time.deltaTime; if(backtTimes> interval && YiYanBack.BackText.Count>0) { char str = YiYanBack.BackText.Dequeue(); backt.text += str; backtTimes = 0; times2 = 0; oldback = backt.text; // xunfei.startQueue(str.ToString()); } if (YiYanBack.BackTextlist.Count > 0) { //backt.text += YiYanBack.BackTextlist.Dequeue(); // YiYanBack.BackTextlist.Dequeue(); xunfei.startQueue(YiYanBack.BackTextlist.Dequeue()); //times2 = 0; //Debug.Log(backt.text); //oldback = backt.text; // StartCoroutine(PlayAnimator("Point_left", 1f)); } if (YiYanBack.BackFINALTextlist.Count > 0) { backfinalt.text = YiYanBack.BackFINALTextlist.Dequeue(); send(backfinalt.text); } if (YiYanBack.MyTextlist.Count > 0) { myt.text = YiYanBack.MyTextlist.Dequeue(); oldMyt = myt.text; //HttpsSendLog.Instance.SendLog("MYT", myt.text); if (myt.text.Contains("唤醒成功") && myt.text.Contains("小度小度")) { myt.text = ""; oldMyt = ""; if (!Irobot.gameObject.activeSelf) { transform.position = player.position + player.forward * irobotPos.z; transform.position = new Vector3(transform.position.x, irobotPos.y, transform.position.z); transform.LookAt(player); transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0); Irobot.gameObject.SetActive(true); isWakeup = true; } if (backt.text != "") { xunfei.stop(); } StartCoroutine(AudioIconSetting(0, true)); YiYanBack.BackText.Clear(); YiYanBack.BackText = new Queue(); // backt.text = ""; } else isWakeup = false; times2 = 0; times3 = 0; backt.text = ""; HttpsSendLog.Instance.SendLog("MYT", oldMyt+" MY "+ myt.text); } if (oldback != null && oldback == backt.text && !isAudioPlay) { times2 += Time.deltaTime; if (times2 > 8f) { times2 = 0; isWakeup = false; Irobot.gameObject.SetActive(false); } } else times2 = 0; if (oldMyt != null && myt.text == oldMyt) { // HttpsSendLog.Instance.SendLog("MYT", " 计时开始 " + times); times += Time.deltaTime; if (times > 3f) { send(myt.text); oldMyt = null; times = 0; } } if (isWakeup) { times3 += Time.deltaTime; if (times3 > 10f) { times3 = 0; isWakeup = false; Irobot.gameObject.SetActive(false); } } #region old /* //if (oldback != null && oldback == backt.text) //{ // times2 += Time.deltaTime; // if (times2 > 3f) // { // oldback = null; // times2 = 0; // StartSpeek(); // // StartCoroutine(OpenSpeek(1f)); // } //} //else // times2 = 0; //if (oldMyt!=null && myt.text == oldMyt) //{ // if(!isWakeup&&myt.text.Contains("小度小度")) // { // isWakeup = true; // transform.position = player.position + player.forward * irobotPos.z; // transform.position = new Vector3(transform.position.x, irobotPos.y, transform.position.z); // transform.LookAt(player); // transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0); // Irobot.gameObject.SetActive(true); // myt.text = ""; // backfinalt.text = ""; // backt.text = ""; // // StartCoroutine(OpenSpeek(0.3f)); // StartSpeek(); // } // times += Time.deltaTime; // if(times>1f) // StartCoroutine(AudioIconSetting(0, false)); // if (times > 3f) // { // oldMyt = null; // times = 0; // send(myt.text); // } //}else //{ // times = 0; //} */ #endregion } private void OnApplicationPause(bool pause) { if (isInit) { if (pause) { UnityToJava.yiYanClass.CallStatic("onPauseJava"); } else { UnityToJava.yiYanClass.CallStatic("onResumeJava"); } } } private void OnDestroy() { UnityToJava.yiYanClass.CallStatic("onDestroyJava"); } public void StopSpeek() { UnityToJava.yiYanClass.CallStatic("StopSpeek"); Debug.Log("StopSpeek===="); } public void StartSpeekOnClick() { StartSpeek(); YiYanBack.BackText.Clear(); YiYanBack.BackText = new Queue(); myt.text = ""; backt.text = ""; } public void StartSpeek() { //UnityToJava.yiYanClass.CallStatic("StartSpeek"); //Debug.Log("StartSpeek===="); //StartCoroutine(AudioIconSetting(0.7f, true)); } public void send(string str) { //StartCoroutine(AudioIconSetting(0, false)); //if (isWakeup) //{ // if (myt.text.Contains("小度拜拜")) // { // isWakeup = false; // Irobot.gameObject.SetActive(false); // StartSpeek(); // } // else // { // UnityToJava.yiYanClass.CallStatic("SendText", str); // Debug.Log("Send " + str); // } //} //else //{ // StartSpeek(); //} StartCoroutine(AudioIconSetting(0, false)); HttpsSendLog.Instance.SendLog("SendText", myt.text); UnityToJava.yiYanClass.CallStatic("SendText", str); backt.text = ""; oldMyt = null; //myt.text = ""; } private IEnumerator OpenSpeek( float times) { yield return new WaitForSeconds(times); StartSpeek(); } private IEnumerator PlayAnimator( string Animator_Name ,float times) { // animator.SetBool(Animator_Name, true); yield return new WaitForSeconds(times); //animator.SetBool(Animator_Name, false); } private IEnumerator AudioIconSetting(float times , bool state) { //if(state) //{ // yield return new WaitForSeconds(0.5f); //} yield return new WaitForSeconds(times); ui.gameObject.SetActive(state); } } public class YiYanBack : AndroidJavaProxy { public static Queue BackTextlist = new Queue(); public static Queue BackFINALTextlist = new Queue(); public static Queue MyTextlist = new Queue(); public static Queue BackText = new Queue(); public YiYanBack() : base("com.xiaodu.dueros.magictool.demo.YiYanBack") { } public void onBackText(string msg) { Debug.Log("onBackText====>" + msg); BackTextlist.Enqueue(msg); HttpsSendLog.Instance.SendLog("F", msg); for (int i = 0; i < msg.Length; i++) { BackText.Enqueue(msg[i]); } } public void onMyFINALText(string msg) { Debug.Log("onBackFINALText====>" + msg); BackFINALTextlist.Enqueue(msg); } public void onMyText(string msg) { Debug.Log("onMyText====>" + msg); MyTextlist.Enqueue(msg); } public void onWakeupSucceed(string msg) { Debug.Log("onMyText====>" + msg); MyTextlist.Enqueue(msg); } public void onWakeupFailed(string msg) { Debug.Log("onMyText====>" + msg); MyTextlist.Enqueue(msg); } }