BerkinAkkaya

Change Color At Level Up

Oct 13th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class LevelSystem : MonoBehaviour {
  6.  
  7.     [SerializeField] int[] levelPoints;
  8.     [SerializeField] Color backgroundAtStart;
  9.     [SerializeField] Color[] backgrounds;
  10.     [SerializeField] float colorChangingTime;
  11.  
  12.     public static int level;
  13.     float point;
  14.    
  15.     public delegate void LevelPass();
  16.     public static event LevelPass OnLevelPass;
  17.  
  18.     void Start(){
  19.         Camera.main.backgroundColor = backgroundAtStart;
  20.     }
  21.  
  22.     void Update(){
  23.         point = PlayerPrefs.GetInt("Point");
  24.         SetOnLevelPass();
  25.     }
  26.  
  27.  
  28.     void ChangeColor(){
  29.         StartCoroutine("ColorChanger");
  30.     }
  31.    
  32.     IEnumerator ColorChanger(){
  33.         float ElapsedTime = 0.0f;
  34.         float TotalTime = colorChangingTime;
  35.        
  36.         var camera = Camera.main;
  37.         Color current = Camera.main.backgroundColor;
  38.         Color next = backgrounds[level - 1];
  39.        
  40.         while(ElapsedTime < TotalTime)
  41.         {
  42.             ElapsedTime += Time.deltaTime;
  43.             camera.backgroundColor = Color.Lerp(current, next, (ElapsedTime / TotalTime));
  44.             yield return new WaitForEndOfFrame();
  45.         }
  46.        
  47.         ElapsedTime = 0;
  48.         yield return null;
  49.     }
  50.  
  51.     void SetOnLevelPass(){
  52.         for(int i = 0; i < levelPoints.Length; i++)
  53.         {
  54.             if(point == levelPoints[i])
  55.             {
  56.                 level = i + 1;
  57.                 OnLevelPass();
  58.             }
  59.         }
  60.     }
  61.  
  62.     void CallLevelPassedOnce(){
  63.         PlayerPrefs.SetInt("Point", PlayerPrefs.GetInt("Point") + 10);
  64.     }
  65.  
  66.     void OnEnable(){
  67.         OnLevelPass = null;
  68.         OnLevelPass += CallLevelPassedOnce;
  69.         OnLevelPass += ChangeColor;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment