码迷,mamicode.com
首页 > 其他好文 > 详细

ColorCtr控制颜色渐变

时间:2018-09-10 21:21:11      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:upd   void   速度   tco   初始   gora   public   time   getc   

ColorCtr控制颜色渐变

   public class ColorCtr : MonoBehaviour

    {

        public static global_color Instance;         //方便外部调用

        public float speed = 1F;             //定义速度

        public GameObject goRawImage;            //定义遮挡Object

        private RawImage _image;              //定义遮挡图片

        private bool _toClear = true;            //定义变亮bool

        private bool _toBlack = false;           //定义变暗bool

                  //初始化

        void Awake()

        {

                      //调用实例

            Instance = this;

                      //判断是否有遮挡物、找到遮挡图片

            if (goRawImage)

            {

                _image = goRawImage.GetComponent<RawImage>();

            }

        }

                  //外部调用,改变bool变亮

        public void setToClear()

        {

            _toClear = true;

            _toBlack = false;

        }

                  //外部调用,改变bool变暗

        public void setToBlack()

        {

            _toClear = false;

            _toBlack = true;

        }

                  //改变颜色插值变亮方法

        private void FadeToClear()

        {

            _image.color = Color.Lerp(_image.color, Color.clear, speed*Time.deltaTime);

        }

                  //改变颜色插值变暗方法

        private void FadeToBlack()

        {

            _image.color = Color.Lerp(_image.color, Color.black, speed * Time.deltaTime);

        }

                  //变亮方法的具体实现

        private void SceneToClear()

        {

                      //调用改变颜色变亮插值

            FadeToClear();

                      //颜色彻底改变

            if (_image.color.a <= 0.05)

            {

                _image.color = Color.clear;

                _image.enabled = false;

                _toClear = false;

            }

        }

                  //变暗方法的具体实现

        private void SceneToBlack()

        {

                       //打开image

            _image.enabled = true;

                       //调用改变颜色变暗插值

            FadeToBlack();

                     //颜色彻底改变

            if (_image.color.a >= 0.95)

            {

                _image.color = Color.black;

                _toClear = false;

            }

        }

                  //时刻检测bool变量是否改变,调用方法

        void Update()

        {

            if (_toClear)

            {       

                                      //调用变亮方法

                SceneToClear();

            }

            else if (_toBlack)

            {

                                      //调用变暗方法

                SceneToBlack();

            }

        }

}

注:需要有 goRawImage遮挡物、_image遮挡图片,通过调用setToClear和setToBlack方法具体实现、、、

ColorCtr控制颜色渐变

标签:upd   void   速度   tco   初始   gora   public   time   getc   

原文地址:https://www.cnblogs.com/XiaoLang0/p/9622760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!