当前位置:   article > 正文

unity 用Dotween做出想视频播放器一样的漫游效果,可以前进后退控制速度和进度条效果_unity做摄像机自动漫游

unity做摄像机自动漫游
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using DG.Tweening;
  6. using Tx3d.Framework;
  7. using DG.Tweening.Plugins.Options;
  8. using DG.Tweening.Core;
  9. using DG.Tweening.Plugins.Core.PathCore;
  10. namespace SimpleUIFrame
  11. {
  12. public class PlaybackRoamingControl : MonoBehaviour
  13. {
  14. #region 成员属性&变量
  15. //主摄像机
  16. private Camera mainCamera;
  17. //DOTweenPath组件
  18. private DOTweenPath doTweenPath;
  19. //暂停和播放按钮
  20. public Button playOrPauseBut;
  21. //复制的节点数组
  22. 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) };
  23. //进度条暂停播放判断
  24. private bool timeStop;
  25. //进度条
  26. public Slider slider;
  27. //速度
  28. private float speed = 1;
  29. //跳转阶段按钮
  30. public Button _TiaoZ0;
  31. public Button _TiaoZ1;
  32. public Button _TiaoZ2;
  33. //TweenerCore 插件中路径移动的变量
  34. TweenerCore<Vector3, Path, PathOptions> _TC_Path;
  35. //进行时间 总时间
  36. public Text proceed_Time, total_Ttime;
  37. //总的时间设置
  38. public float duration = 30;
  39. //执行播放的动画
  40. public Animator animationGo;
  41. //数组下标第几个执行方法
  42. private int subscript = 8;
  43. //移动中执行等待时间
  44. private float moveAwaitSpeed=2;
  45. #endregion
  46. #region MonoBehaviour方法
  47. void Start()
  48. {
  49. //获取到主摄像机
  50. mainCamera = CameraControllerManager.Instance.MainCamera;
  51. //跳转节点add方法
  52. _TiaoZ0.onClick.AddListener(delegate { TiaoZhuan(0); });
  53. _TiaoZ1.onClick.AddListener(delegate { TiaoZhuan(1); });
  54. _TiaoZ2.onClick.AddListener(delegate { TiaoZhuan(2); });
  55. //获取到主摄像机的DOTweenPath组件
  56. doTweenPath = mainCamera.GetComponent<DOTweenPath>();
  57. //按钮方法
  58. playOrPauseBut.onClick.AddListener(DoPlayOrPause_AutoMoveCamera);
  59. //变量赋值
  60. _TC_Path = transform.GetComponent<Rigidbody>().DOPath(waypoints, duration, PathType.CatmullRom, PathMode.Full3D, 10, null).SetAutoKill(true);
  61. //速度值
  62. _TC_Path.timeScale = 0.1f;
  63. //获取总执行的时间
  64. if (total_Ttime != null)
  65. total_Ttime.text = duration.ToString();
  66. }
  67. private void Update()
  68. {
  69. //进度条
  70. if (!timeStop)
  71. {
  72. //获取进度条0-1
  73. if (slider != null)
  74. slider.value = _TC_Path.ElapsedDirectionalPercentage();
  75. //获取已播放多长时间
  76. if (proceed_Time != null)
  77. proceed_Time.text = _TC_Path.Elapsed().ToString();
  78. //当数值大于0.96的时候直接等于
  79. if (_TC_Path.ElapsedDirectionalPercentage() >= 0.96)
  80. {
  81. slider.value = 1;
  82. //获取已播放多长时间
  83. proceed_Time.text = duration.ToString();
  84. timeStop = true;
  85. }
  86. }
  87. //根据节点下标执行
  88. _TC_Path.OnWaypointChange((index) =>
  89. {
  90. index++;
  91. //当下标到达之后执行方法
  92. if (index == subscript)
  93. StartCoroutine(IntervalTime(moveAwaitSpeed));
  94. });
  95. }
  96. #endregion
  97. #region 公有方法
  98. /// <summary>
  99. /// 跳转节点方法 0是前面 1是中间 2是后面
  100. /// </summary>
  101. /// <param name="nums"></param>
  102. public void TiaoZhuan(int nums)
  103. {
  104. if (!timeStop)
  105. {
  106. switch (nums)
  107. {
  108. case 0:
  109. _TC_Path.Goto(duration * 0.2f, true);
  110. Debug.Log(duration * 0.2f + ":" + duration);
  111. break;
  112. case 1:
  113. _TC_Path.Goto(duration * 0.5f, true);
  114. Debug.Log(duration * 0.5f + ":" + duration);
  115. break;
  116. case 2:
  117. _TC_Path.Goto(duration * 0.8f, true);
  118. Debug.Log(duration * 0.8f + ":" + duration);
  119. break;
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// 快进
  125. /// </summary>
  126. public void SpeedK()
  127. {
  128. _TC_Path.timeScale = 1f;
  129. speed = 10f;
  130. }
  131. /// <summary>
  132. /// 减速
  133. /// </summary>
  134. public void ExitK()
  135. {
  136. // speedkauijin--;
  137. _TC_Path.timeScale = 0.1f;
  138. speed = 1f;
  139. }
  140. /// <summary>
  141. /// 时间间隔停止等待
  142. /// </summary>
  143. /// <returns></returns>
  144. public IEnumerator IntervalTime(float moveAwait)
  145. {
  146. //停止等待几秒后执行
  147. if (animationGo != null)
  148. animationGo.SetBool("isOn", true);
  149. float ol = _TC_Path.timeScale;
  150. _TC_Path.timeScale = 0.0001f;
  151. yield return new WaitForSeconds(moveAwait);
  152. _TC_Path.timeScale = ol;
  153. if (animationGo != null)
  154. animationGo.SetBool("isOn", false);
  155. }
  156. /// <summary>
  157. /// 控制主摄像机的暂停
  158. /// </summary>
  159. public void DoPause_AutoMoveCamera()
  160. {
  161. if (doTweenPath != null)
  162. {
  163. doTweenPath.DOPause();
  164. }
  165. }
  166. /// <summary>
  167. /// 控制主摄像机的移动与暂停
  168. /// </summary>
  169. public void DoPlayOrPause_AutoMoveCamera()
  170. {
  171. if (!timeStop)
  172. {
  173. _TC_Path.Pause();
  174. timeStop = !timeStop;
  175. }
  176. else
  177. {
  178. _TC_Path.Play();
  179. timeStop = !timeStop;
  180. }
  181. }
  182. #endregion
  183. }
  184. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/89808
推荐阅读
相关标签
  

闽ICP备14008679号