123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using System;
- using System.Collections;
- using UnityEngine;
- public class PortalFX_UVAnimation : MonoBehaviour
- {
- public int TilesX = 4;
- public int TilesY = 4;
- public float FPS = 30;
- public int StartFrameOffset;
- public bool IsLoop = true;
- public float StartDelay = 0;
- public bool IsReverse;
- public bool IsBump;
- public AnimationCurve FrameOverTime = AnimationCurve.Linear(0, 1, 1, 1);
- private bool isInizialised;
- private int index;
- private int count, allCount;
- private float animationLifeTime;
- private bool isVisible;
- private bool isCorutineStarted;
- private Renderer currentRenderer;
- private Material instanceMaterial;
- private float animationStartTime;
- private bool animationStoped;
- #region Non-public methods
- private void Start()
- {
- currentRenderer = GetComponent<Renderer>();
- InitDefaultVariables();
- isInizialised = true;
- isVisible = true;
- Play();
- }
- private void InitDefaultVariables()
- {
- currentRenderer = GetComponent<Renderer>();
- if (currentRenderer==null)
- throw new Exception("UvTextureAnimator can't get renderer");
- if (!currentRenderer.enabled)
- currentRenderer.enabled = true;
- allCount = 0;
- animationStoped = false;
- animationLifeTime = TilesX * TilesY / FPS;
- count = TilesY * TilesX;
- index = TilesX - 1;
- var offset = Vector3.zero;
- StartFrameOffset = StartFrameOffset - (StartFrameOffset / count) * count;
- var size = new Vector2(1f / TilesX, 1f / TilesY);
- if (currentRenderer!=null) {
- instanceMaterial = currentRenderer.material;
- instanceMaterial.SetTextureScale("_MainTex", size);
- instanceMaterial.SetTextureOffset("_MainTex", offset);
- if (IsBump) {
- instanceMaterial.SetTextureScale("_BumpMap", size);
- instanceMaterial.SetTextureOffset("_BumpMap", offset);
- }
- }
- }
- private void Play()
- {
- if (isCorutineStarted)
- return;
- if (StartDelay > 0.0001f)
- Invoke("PlayDelay", StartDelay);
- else
- StartCoroutine(UpdateCorutine());
- isCorutineStarted = true;
- }
- private void PlayDelay()
- {
- StartCoroutine(UpdateCorutine());
- }
- #region CorutineCode
- private void OnEnable()
- {
- if (!isInizialised)
- return;
- InitDefaultVariables();
- isVisible = true;
- Play();
- }
- private void OnDisable()
- {
- isCorutineStarted = false;
- isVisible = false;
- StopAllCoroutines();
- CancelInvoke("PlayDelay");
- }
- private IEnumerator UpdateCorutine()
- {
- animationStartTime = Time.time;
- while (isVisible && (IsLoop || !animationStoped)) {
- if (!IsReverse)
- UpdateFrame();
- else
- UpdateFrameReversed();
- if (!IsLoop && animationStoped)
- break;
- var frameTime = (Time.time - animationStartTime) / animationLifeTime;
- var currentSpeedFps = FrameOverTime.Evaluate(Mathf.Clamp01(frameTime));
- yield return new WaitForSeconds(1f / (FPS * currentSpeedFps));
- }
- isCorutineStarted = false;
- //currentRenderer.enabled = false;
- }
- #endregion CorutineCode
- private void UpdateFrame()
- {
- ++allCount;
- ++index;
- if (index >= count)
- index = 0;
- if (count==allCount) {
- animationStartTime = Time.time;
- allCount = 0;
- animationStoped = true;
- }
- var offset = new Vector2((float)index / TilesX - (int)(index / TilesX), 1 - (int)(index / TilesX) / (float)TilesY);
- if (currentRenderer!=null) {
- instanceMaterial.SetTextureOffset("_MainTex", offset);
- if (IsBump)
- instanceMaterial.SetTextureOffset("_BumpMap", offset);
-
- }
- }
- private void UpdateFrameReversed()
- {
- --allCount;
- --index;
- if (index <= 0)
- index = count;
- if (count == allCount)
- {
- animationStartTime = Time.time;
- allCount = 0;
- animationStoped = true;
- }
- var offset = new Vector2((float)index / TilesX - (int)(index / TilesX), 1 - (int)(index / TilesX) / (float)TilesY);
- if (currentRenderer!=null) {
- instanceMaterial.SetTextureOffset("_MainTex", offset);
- if (IsBump)
- instanceMaterial.SetTextureOffset("_BumpMap", offset);
- }
- }
- private void OnDestroy()
- {
- if (instanceMaterial!=null) {
- Destroy(instanceMaterial);
- instanceMaterial = null;
- }
- }
- #endregion
- }
|