123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using ChivaXR;
- using System;
- using Sirenix.OdinInspector;
- public class AniDriver_UVOffset : AnimationDriverBase
- {
- public TimeType AniTimeType = TimeType.aniFrame;
- [ShowIf("AniTimeType", Value = TimeType.aniFrame)]
- [LabelWidth(50)]
- [HorizontalGroup("frame")]
- [LabelText("起始帧")]
- public int startFrame;
- [ShowIf("AniTimeType", Value = TimeType.aniFrame)]
- [LabelWidth(50)]
- [HorizontalGroup("frame")]
- [LabelText("结束帧")]
- public int endFrame;
- [LabelWidth(100)]
- [ShowIf("AniTimeType", Value = TimeType.aniFrame)]
- [LabelText("动画FPS")]
- public float aniFPS = 30;
- [ShowIf("AniTimeType", Value = TimeType.time)]
- [LabelText("动画开始时间")]
- [LabelWidth(100)]
- [HorizontalGroup("time")]
- public float startTime = 0;
- [ShowIf("AniTimeType", Value = TimeType.time)]
- [LabelText("动画结束时间")]
- [LabelWidth(100)]
- [HorizontalGroup("time")]
- public float endTime = 0;
-
- [LabelWidth(100)]
- public Vector2 startUVOffset;
- [LabelWidth(100)]
- public Vector2 endUVOffset;
- public MeshRenderer target;
- public override void FinishedState()
- {
- target.material.SetTextureOffset("_MainTex", endUVOffset);
- }
- public override void InitState()
- {
- target.material.SetTextureOffset("_MainTex", startUVOffset);
- }
- public override void StartPlay(Action finishedCallBack = null)
- {
- StartAniCoroutine(PlayUVAni(finishedCallBack));
- }
- IEnumerator PlayUVAni(Action finishedCallBack=null)
- {
- float allRunningTime = 0;
- float currentRunningTime = 0;
- switch (AniTimeType)
- {
- case TimeType.time:
- yield return new WaitForSeconds(startTime);
- allRunningTime = endTime - startTime;
- break;
- case TimeType.aniFrame:
- yield return new WaitForSeconds(startFrame / aniFPS);
- allRunningTime = (endFrame - startFrame) / aniFPS;
- break;
- }
- while (currentRunningTime < allRunningTime)
- {
- currentRunningTime += Time.deltaTime;
- target.material.SetTextureOffset("_MainTex", Vector2.Lerp(startUVOffset, endUVOffset, currentRunningTime / allRunningTime));
- yield return new WaitForEndOfFrame();
- }
- target.material.SetTextureOffset("_MainTex", Vector2.Lerp(startUVOffset, endUVOffset, 1));
- finishedCallBack?.Invoke();
- }
- }
|