Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(SpriteRenderer))]
- public class ColorChanger : MonoBehaviour {
- /* Berkin AKKAYA */
- /* berkinakkaya.wixsite.com/blog */
- [Range(0.0f, 255.0f)]
- [SerializeField] float brightness = 60.0f;
- [Tooltip("Color Changing Speed")]
- [Range(0.0f, 0.2f)][SerializeField]
- float colorChangingSpeed;
- private bool increase; //Increase value (decrase if false)
- private float min, max, value;
- private SpriteRenderer sprite;
- #region Start and Update
- void Start(){
- sprite = GetComponent<SpriteRenderer>();
- SetBrightness();
- }
- void Update () {
- ChangeValueAutomatically();
- ChangeBackgroundColor();
- }
- #endregion
- #region Find Color
- Vector3 RGB{
- get{
- float percent = ((value * 100) / 16);
- Vector3 rgb;
- if(value < 0.16f){
- float x = max - (min * percent);
- rgb = new Vector3(x, min, max);
- }else if(value < 0.32f){
- percent -= 1;
- float y = min + (min * percent);
- rgb = new Vector3(min, y, max);
- }else if(value < 0.48f){
- percent -= 2;
- float z = max - (min * percent);
- rgb = new Vector3(min, max, z);
- }else if(value < 0.64f){
- percent -= 3;
- float x = min + (min * percent);
- rgb = new Vector3(x, max, min);
- }else if(value < 0.80f){
- percent -= 4;
- float y = max - (min * percent);
- rgb = new Vector3(max, y, min);
- }else if(value < 0.96f){
- percent -= 5;
- float z = min + (min * percent);
- rgb = new Vector3(max, min, z);
- }else
- rgb = new Vector3(max, min, max);
- return rgb;
- }
- }
- #endregion
- #region Work Automatically
- void ChangeBackgroundColor(){
- sprite.color = new Color(RGB.x, RGB.y, RGB.z);
- }
- void ChangeValueAutomatically(){
- if(value >= 1) increase = false;
- if(value <= 0) increase = true;
- if(increase) value += colorChangingSpeed * Time.deltaTime;
- else value -= colorChangingSpeed * Time.deltaTime;
- }
- #endregion
- #region Set Brightness
- void SetBrightness(){
- max = brightness / 255;
- min = brightness / 255 / 2;
- }
- #endregion
- }
Add Comment
Please, Sign In to add comment