CameraFade.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. public class CameraFade : MonoBehaviour
  6. {
  7. private static CameraFade instance = null;
  8. public static CameraFade Instance
  9. {
  10. get
  11. {
  12. if (instance == null)
  13. {
  14. GameObject obj = new GameObject("[CameraFade]");
  15. instance = obj.AddComponent<CameraFade>();
  16. instance.Init();
  17. }
  18. return CameraFade.instance;
  19. }
  20. }
  21. public static CallBack FadeCompleteCallBack;
  22. private static bool isFading = false;
  23. public static bool IsFading
  24. {
  25. get
  26. {
  27. return isFading;
  28. }
  29. }
  30. private void Init()
  31. {
  32. alpha = 0;
  33. tempColor = GUI.color;
  34. // GUI.depth = 100;
  35. if (crossfadeTexture == null)
  36. {
  37. crossfadeTexture = new Texture2D(1, 1, TextureFormat.RGB24, false);
  38. crossfadeTexture.SetPixel(0, 0, Color.black);
  39. crossfadeTexture.Apply();
  40. }
  41. }
  42. #region 外部调用
  43. //淡入
  44. public static void FadeIn(float fadeTime, CallBack completeCallBack = null, bool isForceAlpha = false, float delay = 0)
  45. {
  46. CameraFadeData data = GetCameraFadeData(CameraFadeType.FadeIn, isForceAlpha, delay, fadeTime, completeCallBack);
  47. Instance.StartFadeFunc(data);
  48. }
  49. //淡出
  50. public static void FadeOut(float fadeTime, CallBack completeCallBack = null, bool isForceAlpha = false, float delay = 0)
  51. {
  52. CameraFadeData data = GetCameraFadeData(CameraFadeType.FadeOut, isForceAlpha, delay, fadeTime, completeCallBack);
  53. Instance.StartFadeFunc(data);
  54. }
  55. //从淡入到淡出
  56. public static void FadeInToOut(float _fadeInTime, float afterInDelayTime, float _fadeOutTime, CallBack afterFadeInCallback = null, CallBack afterFadeOutCallback = null, float delay = 0, bool isForceAlpha = false)
  57. {
  58. CameraFadeData data = GetCameraFadeData(CameraFadeType.FadeIn, isForceAlpha, delay, _fadeInTime, afterFadeInCallback);
  59. CameraFadeData data2 = GetCameraFadeData(CameraFadeType.FadeOut, isForceAlpha, afterInDelayTime, _fadeOutTime, afterFadeOutCallback);
  60. Instance.StartFadeFunc(data, data2);
  61. }
  62. #endregion
  63. private List<CameraFadeData> currentFadeDatas = new List<CameraFadeData>();
  64. private void StartFadeFunc(params CameraFadeData[] paras)
  65. {
  66. if (currentFadeDatas.Count > 0)
  67. {
  68. foreach (var item in currentFadeDatas)
  69. {
  70. if (item.completeCallBack != null)
  71. item.completeCallBack();
  72. cacheData.Add(item);
  73. }
  74. currentFadeDatas.Clear();
  75. }
  76. for (int i = 0; i < paras.Length; i++)
  77. {
  78. currentFadeDatas.Add(paras[i]);
  79. }
  80. CameraFadeData data = paras[0];
  81. if (data.isForceAlpha)
  82. {
  83. if (data.fadeType == CameraFadeType.FadeIn)
  84. {
  85. alpha = 0;
  86. }
  87. else
  88. {
  89. alpha = 1;
  90. }
  91. }
  92. else
  93. {
  94. if (isFading)
  95. {
  96. data.delay = 0;
  97. }
  98. }
  99. isFading = true;
  100. float tempTime = data.fadeTime * alpha;
  101. data.tempFadeCaculateTime = tempTime;
  102. }
  103. // Update is called once per frame
  104. private void Update()
  105. {
  106. if (currentFadeDatas.Count == 0)
  107. {
  108. isFading = false;
  109. return;
  110. }
  111. CameraFadeData data = currentFadeDatas[0];
  112. if (data.delay > 0)
  113. {
  114. data.delay -= Time.deltaTime;
  115. return;
  116. }
  117. if (data.fadeType == CameraFadeType.FadeIn)
  118. {
  119. if (data.tempFadeCaculateTime >= data.fadeTime)
  120. {
  121. alpha = 1;
  122. RunComplete(data);
  123. return;
  124. }
  125. data.tempFadeCaculateTime += Time.deltaTime;
  126. }
  127. else
  128. {
  129. if (data.tempFadeCaculateTime <= 0)
  130. {
  131. alpha = 0;
  132. RunComplete(data);
  133. return;
  134. }
  135. data.tempFadeCaculateTime -= Time.deltaTime;
  136. }
  137. alpha = data.tempFadeCaculateTime / data.fadeTime;
  138. }
  139. private void RunComplete(CameraFadeData data)
  140. {
  141. if (currentFadeDatas.Contains(data))
  142. {
  143. currentFadeDatas.Remove(data);
  144. cacheData.Add(data);
  145. }
  146. if (data.completeCallBack != null)
  147. {
  148. data.completeCallBack();
  149. }
  150. if (FadeCompleteCallBack != null)
  151. {
  152. FadeCompleteCallBack();
  153. }
  154. }
  155. float alpha = 0;
  156. private Texture2D crossfadeTexture;
  157. Color tempColor;
  158. void OnGUI()
  159. {
  160. tempColor.a = alpha;
  161. GUI.color = tempColor;
  162. if (crossfadeTexture != null)
  163. {
  164. GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), crossfadeTexture);
  165. }
  166. }
  167. private static List<CameraFadeData> cacheData = new List<CameraFadeData>();
  168. private static CameraFadeData GetCameraFadeData(CameraFadeType fadeType, bool isForceAlpha, float delay, float fadeTime, CallBack completeCallBack)
  169. {
  170. CameraFadeData data = null;
  171. if (cacheData.Count > 0)
  172. {
  173. data = cacheData[0];
  174. cacheData.RemoveAt(0);
  175. }
  176. else
  177. {
  178. data = new CameraFadeData();
  179. }
  180. data.SetCameraFadeData(fadeType, isForceAlpha, delay, fadeTime, completeCallBack);
  181. return data;
  182. }
  183. public class CameraFadeData
  184. {
  185. public CameraFadeType fadeType;
  186. /// <summary>
  187. /// 是否硬性切换透明值到1,或0,再渐变
  188. /// </summary>
  189. public bool isForceAlpha = false;
  190. public float delay;
  191. public float fadeTime;
  192. public CallBack completeCallBack;
  193. public float tempFadeCaculateTime;
  194. public CameraFadeData() { }
  195. public CameraFadeData(CameraFadeType fadeType, bool isForceAlpha, float delay, float fadeTime, CallBack completeCallBack)
  196. {
  197. SetCameraFadeData(fadeType, isForceAlpha, delay, fadeTime, completeCallBack);
  198. }
  199. public void SetCameraFadeData(CameraFadeType fadeType, bool isForceAlpha, float delay, float fadeTime, CallBack completeCallBack)
  200. {
  201. this.fadeType = fadeType;
  202. this.isForceAlpha = isForceAlpha;
  203. this.delay = delay;
  204. this.fadeTime = fadeTime;
  205. this.completeCallBack = completeCallBack;
  206. tempFadeCaculateTime = fadeTime;
  207. }
  208. }
  209. public enum CameraFadeType
  210. {
  211. FadeIn,
  212. FadeOut,
  213. }
  214. }