using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using Tx3d.Framework;
using DG.Tweening.Plugins.Options;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Core.PathCore;
namespace SimpleUIFrame
{
public class PlaybackRoamingControl : MonoBehaviour
{
#region 成員屬性&變量
//主攝像機
private Camera mainCamera;
//DOTweenPath組件
private DOTweenPath doTweenPath;
//暫停和播放按鈕
public Button playOrPauseBut;
//複製的節點數組
Vector3[] waypoints = new[] { new Vector3(-3.8f, 3f, 213.1f), new Vector3(-11f, 3f, 185.01f), new Vector3(-20f, 3f, 153.01f), new Vector3(-78f, 3f, 141.01f), new Vector3(-81f, 3f, 82.60999f), new Vector3(-64.8f, 3f, 49.71f), new Vector3(-66f, 3f, -116.99f), new Vector3(-47f, 3f, -161.99f), new Vector3(49f, 3f, -161.99f), new Vector3(67f, 3f, -117.99f), new Vector3(64f, 3f, 49.00999f), new Vector3(78.8f, 3f, 85.60999f), new Vector3(80f, 3f, 147.01f), new Vector3(79f, 3f, 199.01f), new Vector3(62f, 3f, 211.01f) };
//進度條暫停播放判斷
private bool timeStop;
//進度條
public Slider slider;
//速度
private float speed = 1;
//跳轉階段按鈕
public Button _TiaoZ0;
public Button _TiaoZ1;
public Button _TiaoZ2;
//TweenerCore 插件中路徑移動的變量
TweenerCore<Vector3, Path, PathOptions> _TC_Path;
//進行時間 總時間
public Text proceed_Time, total_Ttime;
//總的時間設置
public float duration = 30;
//執行播放的動畫
public Animator animationGo;
//數組下標第幾個執行方法
private int subscript = 8;
//移動中執行等待時間
private float moveAwaitSpeed=2;
#endregion
#region MonoBehaviour方法
void Start()
{
//獲取到主攝像機
mainCamera = CameraControllerManager.Instance.MainCamera;
//跳轉節點add方法
_TiaoZ0.onClick.AddListener(delegate { TiaoZhuan(0); });
_TiaoZ1.onClick.AddListener(delegate { TiaoZhuan(1); });
_TiaoZ2.onClick.AddListener(delegate { TiaoZhuan(2); });
//獲取到主攝像機的DOTweenPath組件
doTweenPath = mainCamera.GetComponent<DOTweenPath>();
//按鈕方法
playOrPauseBut.onClick.AddListener(DoPlayOrPause_AutoMoveCamera);
//變量賦值
_TC_Path = transform.GetComponent<Rigidbody>().DOPath(waypoints, duration, PathType.CatmullRom, PathMode.Full3D, 10, null).SetAutoKill(true);
//速度值
_TC_Path.timeScale = 0.1f;
//獲取總執行的時間
if (total_Ttime != null)
total_Ttime.text = duration.ToString();
}
private void Update()
{
//進度條
if (!timeStop)
{
//獲取進度條0-1
if (slider != null)
slider.value = _TC_Path.ElapsedDirectionalPercentage();
//獲取已播放多長時間
if (proceed_Time != null)
proceed_Time.text = _TC_Path.Elapsed().ToString();
//當數值大於0.96的時候直接等於
if (_TC_Path.ElapsedDirectionalPercentage() >= 0.96)
{
slider.value = 1;
//獲取已播放多長時間
proceed_Time.text = duration.ToString();
timeStop = true;
}
}
//根據節點下標執行
_TC_Path.OnWaypointChange((index) =>
{
index++;
//當下標到達之後執行方法
if (index == subscript)
StartCoroutine(IntervalTime(moveAwaitSpeed));
});
}
#endregion
#region 公有方法
/// <summary>
/// 跳轉節點方法 0是前面 1是中間 2是後面
/// </summary>
/// <param name="nums"></param>
public void TiaoZhuan(int nums)
{
if (!timeStop)
{
switch (nums)
{
case 0:
_TC_Path.Goto(duration * 0.2f, true);
Debug.Log(duration * 0.2f + ":" + duration);
break;
case 1:
_TC_Path.Goto(duration * 0.5f, true);
Debug.Log(duration * 0.5f + ":" + duration);
break;
case 2:
_TC_Path.Goto(duration * 0.8f, true);
Debug.Log(duration * 0.8f + ":" + duration);
break;
}
}
}
/// <summary>
/// 快進
/// </summary>
public void SpeedK()
{
_TC_Path.timeScale = 1f;
speed = 10f;
}
/// <summary>
/// 減速
/// </summary>
public void ExitK()
{
// speedkauijin--;
_TC_Path.timeScale = 0.1f;
speed = 1f;
}
/// <summary>
/// 時間間隔停止等待
/// </summary>
/// <returns></returns>
public IEnumerator IntervalTime(float moveAwait)
{
//停止等待幾秒後執行
if (animationGo != null)
animationGo.SetBool("isOn", true);
float ol = _TC_Path.timeScale;
_TC_Path.timeScale = 0.0001f;
yield return new WaitForSeconds(moveAwait);
_TC_Path.timeScale = ol;
if (animationGo != null)
animationGo.SetBool("isOn", false);
}
/// <summary>
/// 控制主攝像機的暫停
/// </summary>
public void DoPause_AutoMoveCamera()
{
if (doTweenPath != null)
{
doTweenPath.DOPause();
}
}
/// <summary>
/// 控制主攝像機的移動與暫停
/// </summary>
public void DoPlayOrPause_AutoMoveCamera()
{
if (!timeStop)
{
_TC_Path.Pause();
timeStop = !timeStop;
}
else
{
_TC_Path.Play();
timeStop = !timeStop;
}
}
#endregion
}
}