BFX_ManualAnimationUpdate.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class BFX_ManualAnimationUpdate : MonoBehaviour
  5. {
  6. public BFX_BloodSettings BloodSettings;
  7. public AnimationCurve AnimationSpeed = AnimationCurve.Linear(0, 0, 1, 1);
  8. public float FramesCount = 99;
  9. public float TimeLimit = 3;
  10. public float OffsetFrames = 0;
  11. private float currentTime;
  12. Renderer rend;
  13. private MaterialPropertyBlock propertyBlock;
  14. void Awake()
  15. {
  16. if (propertyBlock == null)
  17. propertyBlock = new MaterialPropertyBlock();
  18. rend = GetComponent<Renderer>();
  19. }
  20. void OnEnable()
  21. {
  22. rend.enabled = true;
  23. rend.GetPropertyBlock(propertyBlock);
  24. propertyBlock.SetFloat("_UseCustomTime", 1.0f);
  25. propertyBlock.SetFloat("_TimeInFrames", 0.0f);
  26. rend.SetPropertyBlock(propertyBlock);
  27. currentTime = 0;
  28. }
  29. void Update()
  30. {
  31. currentTime += Time.deltaTime * BloodSettings.AnimationSpeed;
  32. if (currentTime / TimeLimit > 1.0)
  33. {
  34. if (rend.enabled) rend.enabled = false;
  35. return;
  36. }
  37. var currentFrameTime = AnimationSpeed.Evaluate(currentTime / TimeLimit);
  38. currentFrameTime = currentFrameTime * FramesCount + OffsetFrames + 1.1f;
  39. float timeInFrames = (Mathf.Ceil(-currentFrameTime) / (FramesCount + 1)) + (1.0f / (FramesCount + 1));
  40. rend.GetPropertyBlock(propertyBlock);
  41. propertyBlock.SetFloat("_LightIntencity", Mathf.Clamp(BloodSettings.LightIntensityMultiplier, 0.01f, 1f));
  42. propertyBlock.SetFloat("_TimeInFrames", timeInFrames);
  43. rend.SetPropertyBlock(propertyBlock);
  44. }
  45. }