123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using static TestFile;
- using LitJson;
- using UnityEngine.Android;
- public class ERNIEBotManager : MonoBehaviour
- {
- public bool isInit;
- public Text backt;
- public Text backfinalt;
- public Text myt;
- public RectTransform scrollRect;
- public GameObject ui;
- public Transform Irobot;
-
- public Transform player;
- private bool isCollect;
- private float times;
- private float times2;
- private float backtTimes;
- private string oldMyt;
- private string oldback;
- private float interval;
- private const string RECORD_AUDIO_PERMISSION = "android.permission.RECORD_AUDIO";
- private bool hasRecordAudioPermission = false;
- private bool isWakeup = false;
- private Vector3 irobotPos;
- private void Awake()
- {
-
-
- }
- private void OnEnable()
- {
-
- 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 = 1f;
- 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);
- #endif
-
-
-
-
-
-
-
-
-
- StartCoroutine(OpenSpeek(3f));
- }
- private IEnumerator Speek(float times)
- {
- yield return new WaitForSeconds(times);
-
- }
-
- private void Update()
- {
- backtTimes += Time.deltaTime;
- if(backtTimes> interval && YiYanBack.BackText.Count>0)
- {
- backt.text += YiYanBack.BackText.Dequeue();
- backtTimes = 0;
- times2 = 0;
- oldback = backt.text;
- }
- if (YiYanBack.BackTextlist.Count > 0)
- {
-
- YiYanBack.BackTextlist.Dequeue();
-
-
-
-
- }
- 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;
- }
- if (oldback != null && oldback == backt.text)
- {
- times2 += Time.deltaTime;
- if (times2 > 3f)
- {
- oldback = null;
- times2 = 0;
-
-
- }
- }
- 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 = "";
-
- StartSpeek();
- }
-
- times += Time.deltaTime;
- if(times>1f)
- StartCoroutine(AudioIconSetting(0, false));
- if (times > 3f)
- {
- oldMyt = null;
- times = 0;
- send(myt.text);
- }
- }else
- {
- times = 0;
- }
- }
- 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<char>();
- 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();
- }
- backt.text = "";
- myt.text = "";
- }
- private IEnumerator OpenSpeek( float times)
- {
- yield return new WaitForSeconds(times);
- StartSpeek();
- }
- private IEnumerator PlayAnimator( string Animator_Name ,float times)
- {
-
- yield return new WaitForSeconds(times);
-
- }
- private IEnumerator AudioIconSetting(float times , bool state)
- {
-
-
-
-
-
- yield return new WaitForSeconds(times);
- ui.gameObject.SetActive(state);
- }
-
- }
|