You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.6 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. public class BackgroundController : MonoBehaviour {
  4. public float transistionSpeed = 1f;
  5. public float waitTime = 10f;
  6. public bool forceChange = false;
  7. private Material mat;
  8. private float lastTransition = 0.0f;
  9. private bool inverseDirection = false;
  10. // Use this for initialization
  11. void Start () {
  12. mat = GetComponent<Renderer>().material;
  13. }
  14. // Update is called once per frame
  15. void FixedUpdate () {
  16. if (Time.time - lastTransition >= waitTime || forceChange) {
  17. lastTransition = Time.time;
  18. forceChange = false;
  19. StartCoroutine(transition(transistionSpeed));
  20. }
  21. }
  22. private IEnumerator transition(float time) {
  23. float transitionStart = -1;
  24. float transitionEnd = 1;
  25. if (inverseDirection) {
  26. mat.SetFloat("_InvertMap", 1);
  27. }else {
  28. mat.SetFloat("_InvertMap", 0);
  29. }
  30. float elapsedTime = 0;
  31. float transitionFrac = transitionStart;
  32. while ((elapsedTime/ time) <=1) {
  33. transitionFrac = Mathf.Lerp(transitionStart, transitionEnd, (elapsedTime / time));
  34. transitionFrac = (inverseDirection)? transitionFrac : -transitionFrac;
  35. mat.SetFloat("_TimeNode", transitionFrac);
  36. elapsedTime += Time.deltaTime;
  37. yield return new WaitForEndOfFrame();
  38. }
  39. if (inverseDirection) {
  40. mat.SetFloat("_TimeNode", 1);
  41. } else {
  42. mat.SetFloat("_TimeNode", -1);
  43. }
  44. inverseDirection = !inverseDirection;
  45. }
  46. }