AniDriver_UVOffset.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_UVOffset : 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 Vector2 startUVOffset;
  36. [LabelWidth(100)]
  37. public Vector2 endUVOffset;
  38. public MeshRenderer target;
  39. public override void FinishedState()
  40. {
  41. target.material.SetTextureOffset("_MainTex", endUVOffset);
  42. }
  43. public override void InitState()
  44. {
  45. target.material.SetTextureOffset("_MainTex", startUVOffset);
  46. }
  47. public override void StartPlay(Action finishedCallBack = null)
  48. {
  49. StartAniCoroutine(PlayUVAni(finishedCallBack));
  50. }
  51. IEnumerator PlayUVAni(Action finishedCallBack=null)
  52. {
  53. float allRunningTime = 0;
  54. float currentRunningTime = 0;
  55. switch (AniTimeType)
  56. {
  57. case TimeType.time:
  58. yield return new WaitForSeconds(startTime);
  59. allRunningTime = endTime - startTime;
  60. break;
  61. case TimeType.aniFrame:
  62. yield return new WaitForSeconds(startFrame / aniFPS);
  63. allRunningTime = (endFrame - startFrame) / aniFPS;
  64. break;
  65. }
  66. while (currentRunningTime < allRunningTime)
  67. {
  68. currentRunningTime += Time.deltaTime;
  69. target.material.SetTextureOffset("_MainTex", Vector2.Lerp(startUVOffset, endUVOffset, currentRunningTime / allRunningTime));
  70. yield return new WaitForEndOfFrame();
  71. }
  72. target.material.SetTextureOffset("_MainTex", Vector2.Lerp(startUVOffset, endUVOffset, 1));
  73. finishedCallBack?.Invoke();
  74. }
  75. }