AniDriver_MoveAndRotateByDistance.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ChivaXR;
  5. using System;
  6. using Sirenix.OdinInspector;
  7. using DG.Tweening;
  8. public class AniDriver_MoveAndRotateByDistance : AnimationDriverBase
  9. {
  10. [Title("旋转模式(默认模式:WorldAxisAdd)")]
  11. [LabelText("旋转模式")]
  12. [SerializeField]
  13. private RotateMode m_RotateMode = RotateMode.WorldAxisAdd;
  14. [BoxGroup("Angle设定")]
  15. public Vector3 initRotateAngle;
  16. [BoxGroup("Angle设定")]
  17. public Vector3 targetRotateAngle;
  18. [BoxGroup("Position设定")]
  19. public Vector3 initLocalPosition;
  20. [BoxGroup("Position设定")]
  21. public Vector3 targetLocalPosition;
  22. public float duration;
  23. [LabelText("要操作的物体(物体在父节点的坐标应为0)")]
  24. public List<Transform> moveObjs = new List<Transform>();
  25. Sequence sequence;
  26. public override void InitState()
  27. {
  28. sequence?.Kill();
  29. SetObjState(initLocalPosition, initRotateAngle);
  30. }
  31. public override void FinishedState()
  32. {
  33. sequence?.Kill();
  34. SetObjState(targetLocalPosition, targetRotateAngle);
  35. }
  36. public override void StartPlay(Action finishedCallBack = null)
  37. {
  38. sequence?.Kill();
  39. sequence = DOTween.Sequence();
  40. foreach (var item in moveObjs)
  41. {
  42. Tweener tw = item.transform.DOLocalMove(targetLocalPosition,duration).SetEase(Ease.Linear);
  43. Tweener tw1 = item.transform.DOLocalRotate(targetRotateAngle, duration,m_RotateMode).SetEase(Ease.Linear);
  44. sequence.Join(tw);
  45. sequence.Join(tw1);
  46. }
  47. sequence.OnComplete(() =>
  48. {
  49. finishedCallBack?.Invoke();
  50. });
  51. sequence.Play();
  52. }
  53. private void SetObjState(Vector3 _targetPosition, Vector3 _targetRotateAngle)
  54. {
  55. foreach (var item in moveObjs)
  56. {
  57. item.localPosition = _targetPosition;
  58. item.localEulerAngles = _targetRotateAngle;
  59. }
  60. }
  61. }