123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- using System;
- using System.Collections;
- using UnityEngine;
- public class uGUICanvasFade : MonoBehaviour
- {
- private CanvasGroup canvasGroup
- {
- get
- {
- if (this.m_CanvasGroup == null)
- {
- this.m_CanvasGroup = base.GetComponent<CanvasGroup>();
- if (this.m_CanvasGroup == null)
- {
- this.m_CanvasGroup = base.gameObject.AddComponent<CanvasGroup>();
- }
- }
- return this.m_CanvasGroup;
- }
- }
- public bool interactable
- {
- get
- {
- return this.canvasGroup.interactable;
- }
- set
- {
- this.canvasGroup.interactable = value;
- }
- }
- public bool blocksRaycasts
- {
- get
- {
- return this.canvasGroup.blocksRaycasts;
- }
- set
- {
- this.canvasGroup.blocksRaycasts = value;
- }
- }
- public float alpha
- {
- get
- {
- return this.canvasGroup.alpha;
- }
- set
- {
- this.canvasGroup.alpha = value;
- }
- }
- private float nowAlpha { get; set; }
- private void Awake()
- {
- this.nowAlpha = this.canvasGroup.alpha;
- }
- public void FadeIn(float time = 0.5f, Action callback = null)
- {
- this.nowAlpha = this.canvasGroup.alpha;
- if (!base.gameObject.activeSelf)
- {
- base.gameObject.SetActive(true);
- }
- if (1f <= this.canvasGroup.alpha)
- {
- if (callback != null)
- {
- callback();
- }
- return;
- }
- base.StopAllCoroutines();
- base.StartCoroutine(this.coroutine_Fade(time, true, callback));
- }
- public void FadeOut(float time = 0.5f, Action callback = null)
- {
- this.nowAlpha = this.canvasGroup.alpha;
- if (this.canvasGroup.alpha <= 0f)
- {
- if (callback != null)
- {
- callback();
- }
- return;
- }
- if (!base.gameObject.activeSelf)
- {
- base.gameObject.SetActive(true);
- }
- base.StopAllCoroutines();
- base.StartCoroutine(this.coroutine_Fade(time, false, callback));
- }
- private IEnumerator coroutine_Fade(float time, bool isFadeIn, Action callback)
- {
- float speed = 1f;
- if (0f < time)
- {
- speed /= time;
- if (isFadeIn)
- {
- while (this.nowAlpha < 1f)
- {
- this.nowAlpha += speed * Time.deltaTime;
- this.canvasGroup.alpha = this.nowAlpha;
- yield return null;
- }
- }
- else
- {
- while (0f < this.nowAlpha)
- {
- this.nowAlpha -= speed * Time.deltaTime;
- this.canvasGroup.alpha = this.nowAlpha;
- yield return null;
- }
- }
- if (callback != null)
- {
- callback();
- }
- yield break;
- }
- if (isFadeIn)
- {
- this.nowAlpha = 1f;
- this.canvasGroup.alpha = 1f;
- }
- else
- {
- this.nowAlpha = 0f;
- this.canvasGroup.alpha = 0f;
- }
- if (callback != null)
- {
- callback();
- }
- yield break;
- }
- private CanvasGroup m_CanvasGroup;
- }
|