AniDriver_JDAnimation.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ChivaXR;
  5. using System;
  6. using Sirenix.OdinInspector;
  7. public class AniDriver_JDAnimation : AnimationDriverBase
  8. {
  9. public TimeType AniTimeType = TimeType.aniFrame;
  10. [ShowIf("AniTimeType", Value = TimeType.aniFrame)]
  11. [LabelWidth(50)]
  12. [HorizontalGroup("frame")]
  13. [LabelText("起始帧")]
  14. public int startFrame;
  15. [ShowIf("AniTimeType", Value = TimeType.aniFrame)]
  16. [LabelWidth(50)]
  17. [HorizontalGroup("frame")]
  18. [LabelText("结束帧")]
  19. public int endFrame;
  20. [LabelWidth(100)]
  21. [ShowIf("AniTimeType", Value = TimeType.aniFrame)]
  22. [LabelText("动画FPS")]
  23. public float aniFPS = 30;
  24. [ShowIf("AniTimeType", Value = TimeType.time)]
  25. [LabelText("动画开始时间")]
  26. [LabelWidth(100)]
  27. [HorizontalGroup("time")]
  28. public float startTime = 0;
  29. [ShowIf("AniTimeType", Value = TimeType.time)]
  30. [LabelText("动画结束时间")]
  31. [LabelWidth(100)]
  32. [HorizontalGroup("time")]
  33. public float endTime = 0;
  34. [LabelWidth(100)]
  35. public float startValue;
  36. [LabelWidth(100)]
  37. public float endValue;
  38. [LabelText("胶带延长线类型")]
  39. public JDLineType jdLineType = JDLineType.lineon_NoJDJuan;
  40. public ChivaVR_JD_ShaderController target;
  41. public override void FinishedState()
  42. {
  43. target.SetJDValue(endValue);
  44. }
  45. public override void InitState()
  46. {
  47. target.SetJDValue(startValue);
  48. target.SetJDLineType(jdLineType);
  49. }
  50. public override void OnDrawGizmos()
  51. {
  52. throw new NotImplementedException();
  53. }
  54. public override void StartPlay(Action finishedCallBack = null)
  55. {
  56. StartAniCoroutine(PlayJDAnimation(finishedCallBack));
  57. }
  58. private IEnumerator PlayJDAnimation(Action finishedCallBack = null)
  59. {
  60. float allRunningTime = 0;
  61. float currentRunningTime = 0;
  62. switch (AniTimeType)
  63. {
  64. case TimeType.time:
  65. yield return new WaitForSeconds(startTime);
  66. allRunningTime = endTime - startTime;
  67. break;
  68. case TimeType.aniFrame:
  69. yield return new WaitForSeconds(startFrame / aniFPS);
  70. allRunningTime = (endFrame - startFrame) / aniFPS;
  71. break;
  72. }
  73. while (currentRunningTime < allRunningTime)
  74. {
  75. currentRunningTime += Time.deltaTime;
  76. target.SetJDValue(Mathf.Lerp(startValue, endValue, currentRunningTime / allRunningTime));
  77. yield return new WaitForEndOfFrame();
  78. }
  79. target.SetJDValue(Mathf.Lerp(startValue, endValue, 1));
  80. finishedCallBack?.Invoke();
  81. }
  82. }