zoukankan      html  css  js  c++  java
  • 使用颜色插值实现屏幕淡入淡出效果

     1 public class FadeInOut : MonoBehaviour
     2     {
     3         //本类实例
     4         public static FadeInOut Instance;
     5         //控制淡入淡出的快慢
     6         public float fadeSpeed = 0.01f;
     7         //淡入淡出图片
     8         public RawImage fadeInOutImage;
     9         //控制淡入淡出图片的alpha值(0,1)
    10         private float _curValue = 1;
    11         //控制淡入的开关
    12         private bool _bFadeIn = false;
    13         //控制淡出的开关
    14         private bool _bFadeOut = false;
    15         
    16         void Awake()
    17         {
    18             Instance = this;
    19         }
    20        
    21         void Update()
    22         {
    23             if(_bFadeOut)
    24             {
    25                 FadeOut();
    26                 
    27             }
    28             else if(_bFadeIn)
    29             {
    30                 FadeIn();
    31             }
    32         }
    33         //淡入效果
    34         private void FadeIn()
    35         {
    36             fadeInOutImage.color = Color.Lerp(fadeInOutImage.color, Color.clear, Time.deltaTime * fadeSpeed);
    37             _curValue = fadeInOutImage.color.a;
    38             if (_curValue <= 0.05f)
    39             {
    40                 fadeInOutImage.color = Color.clear;
    41                 _bFadeIn = false;
    42                 //禁用Image,防止遮罩,按键不能点击
    43                 fadeInOutImage.enabled = false;
    44             }
    45         }
    46 
    47         //淡出效果
    48         private void FadeOut()
    49         {
    50             fadeInOutImage.color = Color.Lerp(fadeInOutImage.color, Color.black, Time.deltaTime * fadeSpeed);
    51             _curValue = fadeInOutImage.color.a;
    52             if (_curValue >= 0.95f)
    53             {
    54                 fadeInOutImage.color = Color.black;
    55                 _bFadeOut = false;
    56             }
    57         }
    58 
    59         //外部调用,开始淡入
    60         public void StartFadeIn()
    61         {
    62             fadeInOutImage.enabled = true;
    63             _bFadeIn = false;           
    64             _bFadeIn = true;
    65             _bFadeOut = false; 
    66         }
    67         //外部调用,开始淡出
    68         public void StartFadeOut()
    69         {
    70             fadeInOutImage.enabled = true;
    71             _bFadeIn = false;
    72             _bFadeOut = true;                   
    73         }
    74     }

    需要播放淡入效果时,在外部调用该类的StartFadeIn方法;需要播放淡出效果时,在外部调用该类的StartFadeOut方法。

  • 相关阅读:
    golang之panic,recover,defer
    Golang之函数练习
    Golang之strings包
    Golang之字符串操作(反转中英文字符串)
    keil中使用——变参数宏__VA_ARGS__
    到底该不该用RTOS——rtos的优点
    c语言联合union的使用用途
    c语言的#和##的用法
    c语言位域的使用注意事项——数据溢出
    基于 Keil MDK 移植 RT-Thread Nano
  • 原文地址:https://www.cnblogs.com/blackteeth/p/10191076.html
Copyright © 2011-2022 走看看