BerkinAkkaya

Color Changing Script

Sep 29th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(SpriteRenderer))]
  4. public class ColorChanger : MonoBehaviour {
  5.  
  6.     /* Berkin AKKAYA */
  7.     /* berkinakkaya.wixsite.com/blog */
  8.  
  9.     [Range(0.0f, 255.0f)]
  10.     [SerializeField] float brightness = 60.0f;
  11.  
  12.     [Tooltip("Color Changing Speed")]
  13.     [Range(0.0f, 0.2f)][SerializeField]
  14.     float colorChangingSpeed;
  15.  
  16.     private bool increase; //Increase value (decrase if false)
  17.     private float min, max, value;
  18.     private SpriteRenderer sprite;
  19.  
  20.     #region Start and Update
  21.     void Start(){
  22.         sprite = GetComponent<SpriteRenderer>();
  23.         SetBrightness();
  24.     }
  25.  
  26.     void Update () {
  27.         ChangeValueAutomatically();
  28.         ChangeBackgroundColor();
  29.     }
  30.     #endregion
  31.  
  32.     #region Find Color
  33.     Vector3 RGB{
  34.         get{
  35.             float percent = ((value * 100) / 16);
  36.             Vector3 rgb;
  37.  
  38.             if(value < 0.16f){
  39.                 float x = max - (min * percent);
  40.                 rgb = new Vector3(x, min, max);
  41.             }else if(value < 0.32f){
  42.                 percent -= 1;
  43.                 float y = min + (min * percent);
  44.                 rgb = new Vector3(min, y, max);
  45.             }else if(value < 0.48f){
  46.                 percent -= 2;
  47.                 float z = max - (min * percent);
  48.                 rgb = new Vector3(min, max, z);
  49.             }else if(value < 0.64f){
  50.                 percent -= 3;
  51.                 float x = min + (min * percent);
  52.                 rgb = new Vector3(x, max, min);
  53.             }else if(value < 0.80f){
  54.                 percent -= 4;
  55.                 float y = max - (min * percent);
  56.                 rgb = new Vector3(max, y, min);
  57.             }else if(value < 0.96f){
  58.                 percent -= 5;
  59.                 float z = min + (min * percent);
  60.                 rgb = new Vector3(max, min, z);
  61.             }else
  62.                 rgb = new Vector3(max, min, max);
  63.  
  64.             return rgb;
  65.         }
  66.     }
  67.     #endregion
  68.  
  69.     #region Work Automatically
  70.     void ChangeBackgroundColor(){
  71.         sprite.color = new Color(RGB.x, RGB.y, RGB.z);
  72.     }
  73.  
  74.     void ChangeValueAutomatically(){
  75.         if(value >= 1) increase = false;
  76.         if(value <= 0) increase = true;
  77.  
  78.         if(increase) value += colorChangingSpeed * Time.deltaTime;
  79.         else         value -= colorChangingSpeed * Time.deltaTime;
  80.     }
  81.     #endregion
  82.  
  83.     #region Set Brightness
  84.     void SetBrightness(){
  85.         max = brightness / 255;
  86.         min = brightness / 255 / 2;
  87.     }
  88.     #endregion
  89. }
Add Comment
Please, Sign In to add comment