• ColorCtr控制颜色渐变


    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方法具体实现、、、

    支持个人观看使用,如商用或转载,请告知! -----萧朗(QQ:453929789 Email:xiaolang_xl@sina.com)
  • 相关阅读:
    阅读书籍推荐
    lvarchar类型对表结构变更影响
    离职总结 | 如何做个好员工?
    Windows7 general operation/cmd notes
    (转)CONST用法
    Linux多线程的使用一:互斥锁
    hadoop2.5.2学习及实践笔记(六)—— Hadoop文件系统及其java接口
    hadoop2.5.2学习及实践笔记(五)—— HDFS shell命令行常见操作
    hadoop2.5.2学习及实践笔记(四)—— namenode启动过程源码概览
    hadoop2.5.2学习及实践笔记(三)—— HDFS概念及体系结构
  • 原文地址:https://www.cnblogs.com/XiaoLang0/p/9622760.html
Copyright © 2020-2023  润新知