UI特效之淡入淡出

 public class FradeInAndOut : MonoBehaviour
    {
        public static FradeInAndOut Instance;
        public GameObject BgroundFrade;
        public float FradeInAndOutSpeed=1.0f;
        private RawImage _FradeUI;
        private bool _BoolSceneFradeIn = true;
        private bool _BoolSceneFradeOut =false;
        private void Awake()
        {
            if (Instance== null) {
                Instance = this;
            }
            if (BgroundFrade != null) {
                _FradeUI = BgroundFrade.GetComponent();
            }
        }
        public void SetSceneFradeIn() {
            _BoolSceneFradeIn = true;
            _BoolSceneFradeOut = false;
        }
        public void SetSceneFradeOut()
        {
            _BoolSceneFradeIn = false;
            _BoolSceneFradeOut = true;
        }

        //淡入
        private void SceneFradeIn() {
            //透明通道
            //插值控制透明度
            _FradeUI.color= Color.Lerp(_FradeUI.color,Color.clear,FradeInAndOutSpeed*Time.deltaTime);
        }
        //淡出
        private void SceneFradeOut() {
            _FradeUI.color = Color.Lerp(_FradeUI.color, Color.black, FradeInAndOutSpeed * Time.deltaTime);
        }
        private void Update()
        {
            if (_BoolSceneFradeIn)
            {
                //淡入
                SceneFradeIn();
                if (_FradeUI.color.a <= 0.05f) {
                    _FradeUI.color = Color.clear;
                    _BoolSceneFradeIn = false;
                    _FradeUI.enabled = false;
                }
            }
           if (_BoolSceneFradeOut) {
                //淡出
                _FradeUI.enabled = true;
                SceneFradeOut();
                if (_FradeUI.color.a >= 0.95f) {
                    _FradeUI.color = Color.black;
                    _BoolSceneFradeOut = false;
                }
            }
        }
    }

 

你可能感兴趣的:(UI特效之淡入淡出)