123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Collections.Generic;
- public class CameraFade : MonoBehaviour
- {
- private static CameraFade instance = null;
- public static CameraFade Instance
- {
- get
- {
- if (instance == null)
- {
- GameObject obj = new GameObject("[CameraFade]");
- instance = obj.AddComponent<CameraFade>();
- instance.Init();
- }
- return CameraFade.instance;
- }
- }
- public static CallBack FadeCompleteCallBack;
- private static bool isFading = false;
- public static bool IsFading
- {
- get
- {
- return isFading;
- }
- }
- private void Init()
- {
- alpha = 0;
- tempColor = GUI.color;
- // GUI.depth = 100;
- if (crossfadeTexture == null)
- {
- crossfadeTexture = new Texture2D(1, 1, TextureFormat.RGB24, false);
- crossfadeTexture.SetPixel(0, 0, Color.black);
- crossfadeTexture.Apply();
- }
- }
- #region 外部调用
- //淡入
- public static void FadeIn(float fadeTime, CallBack completeCallBack = null, bool isForceAlpha = false, float delay = 0)
- {
- CameraFadeData data = GetCameraFadeData(CameraFadeType.FadeIn, isForceAlpha, delay, fadeTime, completeCallBack);
- Instance.StartFadeFunc(data);
- }
- //淡出
- public static void FadeOut(float fadeTime, CallBack completeCallBack = null, bool isForceAlpha = false, float delay = 0)
- {
- CameraFadeData data = GetCameraFadeData(CameraFadeType.FadeOut, isForceAlpha, delay, fadeTime, completeCallBack);
- Instance.StartFadeFunc(data);
- }
- //从淡入到淡出
- public static void FadeInToOut(float _fadeInTime, float afterInDelayTime, float _fadeOutTime, CallBack afterFadeInCallback = null, CallBack afterFadeOutCallback = null, float delay = 0, bool isForceAlpha = false)
- {
- CameraFadeData data = GetCameraFadeData(CameraFadeType.FadeIn, isForceAlpha, delay, _fadeInTime, afterFadeInCallback);
- CameraFadeData data2 = GetCameraFadeData(CameraFadeType.FadeOut, isForceAlpha, afterInDelayTime, _fadeOutTime, afterFadeOutCallback);
- Instance.StartFadeFunc(data, data2);
- }
- #endregion
- private List<CameraFadeData> currentFadeDatas = new List<CameraFadeData>();
- private void StartFadeFunc(params CameraFadeData[] paras)
- {
- if (currentFadeDatas.Count > 0)
- {
- foreach (var item in currentFadeDatas)
- {
- if (item.completeCallBack != null)
- item.completeCallBack();
- cacheData.Add(item);
- }
- currentFadeDatas.Clear();
- }
- for (int i = 0; i < paras.Length; i++)
- {
- currentFadeDatas.Add(paras[i]);
- }
- CameraFadeData data = paras[0];
- if (data.isForceAlpha)
- {
- if (data.fadeType == CameraFadeType.FadeIn)
- {
- alpha = 0;
- }
- else
- {
- alpha = 1;
- }
- }
- else
- {
- if (isFading)
- {
- data.delay = 0;
- }
- }
- isFading = true;
- float tempTime = data.fadeTime * alpha;
- data.tempFadeCaculateTime = tempTime;
- }
- // Update is called once per frame
- private void Update()
- {
- if (currentFadeDatas.Count == 0)
- {
- isFading = false;
- return;
- }
- CameraFadeData data = currentFadeDatas[0];
- if (data.delay > 0)
- {
- data.delay -= Time.deltaTime;
- return;
- }
- if (data.fadeType == CameraFadeType.FadeIn)
- {
- if (data.tempFadeCaculateTime >= data.fadeTime)
- {
- alpha = 1;
- RunComplete(data);
- return;
- }
- data.tempFadeCaculateTime += Time.deltaTime;
- }
- else
- {
- if (data.tempFadeCaculateTime <= 0)
- {
- alpha = 0;
- RunComplete(data);
- return;
- }
- data.tempFadeCaculateTime -= Time.deltaTime;
- }
- alpha = data.tempFadeCaculateTime / data.fadeTime;
- }
- private void RunComplete(CameraFadeData data)
- {
- if (currentFadeDatas.Contains(data))
- {
- currentFadeDatas.Remove(data);
- cacheData.Add(data);
- }
- if (data.completeCallBack != null)
- {
- data.completeCallBack();
- }
-
- if (FadeCompleteCallBack != null)
- {
- FadeCompleteCallBack();
- }
- }
- float alpha = 0;
- private Texture2D crossfadeTexture;
- Color tempColor;
- void OnGUI()
- {
- tempColor.a = alpha;
- GUI.color = tempColor;
- if (crossfadeTexture != null)
- {
- GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), crossfadeTexture);
- }
- }
- private static List<CameraFadeData> cacheData = new List<CameraFadeData>();
- private static CameraFadeData GetCameraFadeData(CameraFadeType fadeType, bool isForceAlpha, float delay, float fadeTime, CallBack completeCallBack)
- {
- CameraFadeData data = null;
- if (cacheData.Count > 0)
- {
- data = cacheData[0];
- cacheData.RemoveAt(0);
- }
- else
- {
- data = new CameraFadeData();
- }
- data.SetCameraFadeData(fadeType, isForceAlpha, delay, fadeTime, completeCallBack);
- return data;
- }
- public class CameraFadeData
- {
- public CameraFadeType fadeType;
- /// <summary>
- /// 是否硬性切换透明值到1,或0,再渐变
- /// </summary>
- public bool isForceAlpha = false;
- public float delay;
- public float fadeTime;
- public CallBack completeCallBack;
- public float tempFadeCaculateTime;
- public CameraFadeData() { }
- public CameraFadeData(CameraFadeType fadeType, bool isForceAlpha, float delay, float fadeTime, CallBack completeCallBack)
- {
- SetCameraFadeData(fadeType, isForceAlpha, delay, fadeTime, completeCallBack);
- }
- public void SetCameraFadeData(CameraFadeType fadeType, bool isForceAlpha, float delay, float fadeTime, CallBack completeCallBack)
- {
- this.fadeType = fadeType;
- this.isForceAlpha = isForceAlpha;
- this.delay = delay;
- this.fadeTime = fadeTime;
- this.completeCallBack = completeCallBack;
- tempFadeCaculateTime = fadeTime;
- }
- }
- public enum CameraFadeType
- {
- FadeIn,
- FadeOut,
- }
- }
|