using System; using System.Collections; using System.Collections.Generic; using DG.Tweening; using UnityEngine; public class AutoGuiderEx : MonoBehaviour { public Transform tranGuidePositions; public string GuiderAudioPath = "Guide/"; int index = -1; Coroutine corAnim1; Coroutine corAnim2; bool currentExMoveCompleted = true; bool currentExRotateCompleted = true; List lstGuidePoint; void Start() { #region lstGuidePoint = new List(); for (int i = 0; i < tranGuidePositions.childCount; i++) { lstGuidePoint.Add(tranGuidePositions.GetChild(i).GetComponent().GPoint); } // Debug.Log(JsonTools.ListToJson(lstGuidePoint)); #endregion //StartCoroutine(DataLoader.LoadDataList("GuidePoints.json", // delegate() { lstGuidePoint = DataLoader.DataList; })); } GuidePoint currentPoint; public void Navigate() { if (currentExMoveCompleted && currentExRotateCompleted) { BaseController.IsAutoNavigating = true; currentExMoveCompleted = false; currentExRotateCompleted = false; index++; if (index == lstGuidePoint.Count) { BaseController.IsAutoNavigating = false; BaseController.CanControl = true; index = -1; return; } currentPoint = lstGuidePoint[index]; //Debug.Log(index + "开始"); if (!string.IsNullOrEmpty(currentPoint.MoveAudioName)) { AudioManager.Instance.PlayAudio(GuiderAudioPath + currentPoint.MoveAudioName); } if (corAnim1 != null) StopCoroutine(corAnim1); if (corAnim2 != null) StopCoroutine(corAnim2); Vector3 toPosition = new Vector3( currentPoint.Position.x, CommonData.MainCamera.transform.position.y, currentPoint.Position.z); float distance = Vector3.Distance(CommonData.MainCamera.transform.position, toPosition); //Debug.Log("距离:"+distance); corAnim1 = StartCoroutine( CommonData.MainCamera.transform.ExMove( toPosition, distance / currentPoint.MoveTime, delegate () { currentExMoveCompleted = true; //Debug.Log(index + "位置移动结束" + Time.time); NavigateCompleted(); }, null ) ); float angle = Vector3.Distance(CommonData.MainCamera.transform.localRotation.eulerAngles, currentPoint.Rotation); //Debug.Log("角度:"+angle); Vector3 vAngle = currentPoint.Rotation; if (currentPoint.Rotation.y < 0) { vAngle = new Vector3(vAngle.x, vAngle.y + 360, vAngle.z); } corAnim2 = StartCoroutine( CommonData.MainCamera.transform.ExRotate( vAngle, //currentPoint.Rotation, Mathf.Abs( angle )/ currentPoint.MoveTime, delegate () { currentExRotateCompleted = true; //Debug.Log(index + "旋转移动结束" + Time.time); NavigateCompleted(); }, null ) ); } } private void NavigateCompleted() { if (currentExMoveCompleted && currentExRotateCompleted && BaseController.IsAutoNavigating) { if (!string.IsNullOrEmpty(currentPoint.StopAudioName)) { AudioManager.Instance.PlayAudio(GuiderAudioPath + currentPoint.StopAudioName); } Invoke("Navigate", currentPoint.StopTime); } } public void StopNavigate() { AudioManager.Instance.StopAudio(); if (corAnim1 != null) StopCoroutine(corAnim1); if (corAnim2 != null) StopCoroutine(corAnim2); BaseController.IsAutoNavigating = false; currentExMoveCompleted = true; currentExRotateCompleted = true; } } static class TransformExtender { /// /// 当前物体以指定的速度匀速移动至目标点 /// /// /// /// /// /// /// public static IEnumerator ExMove(this Transform self, Vector3 target, float speed, Action onComplete = null, Action onUpdate = null) { while (self.position != target) { self.position = Vector3.MoveTowards( self.position, target, speed * Time.deltaTime ); onUpdate?.Invoke(); yield return 0; } onComplete?.Invoke(); } /// /// 当前物体以指定的速度匀速移动至目标点 /// /// /// /// /// /// /// public static IEnumerator ExRotate(this Transform self, Vector3 target, float speed, Action onComplete = null, Action onUpdate = null) { while (self.eulerAngles != target) { self.localRotation = Quaternion.RotateTowards( self.localRotation, Quaternion.Euler(target), speed * Time.deltaTime ); //Debug.Log(self.localRotation.eulerAngles); onUpdate?.Invoke(); yield return 0; } onComplete?.Invoke(); } }