AniDriver_SimpleMoveRot.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using ChivaXR;
  2. using Sirenix.OdinInspector;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Text;
  8. #if UNITY_EDITOR
  9. using UnityEditor;
  10. #endif
  11. using UnityEngine;
  12. public class AniDriver_SimpleMoveRot : AnimationDriverBase
  13. {
  14. [HorizontalGroup("AniGroup")]
  15. public SimpleMoveItem mSimpleMoveItem;
  16. #if UNITY_EDITOR
  17. [HorizontalGroup("AniGroup")]
  18. [Button("选中MovePathItem")]
  19. public void SelectMovePathItem()
  20. {
  21. if (mSimpleMoveItem != null)
  22. {
  23. Selection.activeObject = mSimpleMoveItem.gameObject;
  24. }
  25. }
  26. #endif
  27. [Header("反向播放")]
  28. public bool reverse = false;
  29. public override void FinishedState()
  30. {
  31. mSimpleMoveItem.SetFinishedState(reverse);
  32. }
  33. public override void InitState()
  34. {
  35. mSimpleMoveItem.SetInitState(reverse);
  36. }
  37. public override void StartPlay(Action finishedCallBack = null)
  38. {
  39. StartAniCoroutine(MovePathByItem(mSimpleMoveItem, reverse, finishedCallBack));
  40. }
  41. /// <summary>
  42. /// 根据路径点移动
  43. /// </summary>
  44. /// <param name="points"></param>
  45. /// <param name="moveObj"></param>
  46. /// <param name="moveSpeed"></param>
  47. /// <param name="rotateSpeed"></param>
  48. /// <returns></returns>
  49. IEnumerator MovePathByItem(SimpleMoveItem movePathItem, bool isReverse, Action finishedCallBack = null)
  50. {
  51. float timer = 0;
  52. float lerp = 0;
  53. while (timer < movePathItem.duration)
  54. {
  55. lerp = reverse ? (1 - timer / movePathItem.duration) : timer / movePathItem.duration;
  56. foreach (var item in movePathItem.moveObjs)
  57. {
  58. item.Key.transform.position = Vector3.Lerp(item.Value.position, item.Value.position + item.Value.TransformDirection(movePathItem.localMovePos), lerp);
  59. item.Key.transform.rotation = item.Value.rotation;
  60. item.Key.transform.RotateAround(item.Value.position, item.Value.TransformDirection(movePathItem.localMovePos), movePathItem.rotAngle * lerp);
  61. }
  62. timer += Time.deltaTime;
  63. yield return new WaitForEndOfFrame();
  64. }
  65. movePathItem.SetFinishedState(isReverse);
  66. Debug.Log("MoveEnd is true");
  67. finishedCallBack?.Invoke();
  68. }
  69. public override string AnimationDescription()
  70. {
  71. return animationSequence.ToString() + "--移动物体:" + mSimpleMoveItem.name;
  72. }
  73. }