用法:建立一個 C# 腳本,命名為 CameraFade ,之後將腳本拖曳 (套用) 至攝影機,將淡入淡出時間與顏色設定好(如下圖),之後再建立新腳本(或本身 CameraFade 類別)呼叫 FadeIn()、 FadeOut() 方法,即可做到淡入淡出效果!!
控制面板參數說明:
Fade In Time : 淡入時間,數值越大淡入時間越久
Fade Out Time : 淡出時間,數值越大淡出時間越久
Fade In Color : 淡入顏色
Fade In Color : 淡出顏色
程式碼:
using UnityEngine;
控制面板參數說明:
Fade In Time : 淡入時間,數值越大淡入時間越久
Fade Out Time : 淡出時間,數值越大淡出時間越久
Fade In Color : 淡入顏色
Fade In Color : 淡出顏色

程式碼:
using UnityEngine;
using System.Collections;
public class CameraFade : MonoBehaviour {
public float FadeInTime = 1;
public float FadeOutTime = 1;
public float FadeOutTime = 1;
public Color FadeInColor = new Color(0.1F, 0.1F, 0.1F, 1);
public Color FadeOutColor = new Color(0.1F, 0.1F, 0.1F, 1);
public static bool FadeInIsStart = false;
public static bool FadeOutIsStart = false;
public static bool FadeInIsDone = false;
public static bool FadeOutIsDone = false;
private Texture2D t2d;
private GUIStyle gs;
private static float a = 0;
void Awake (){
FadeInIsStart = false;
FadeOutIsStart = false;
FadeInIsDone = false;
FadeOutIsDone = false;
a = 0;
t2d = new Texture2D (1, 1);
t2d.SetPixel (0, 0, FadeInColor);
t2d.Apply ();
gs = new GUIStyle ();
gs.normal.background = t2d;
}
void OnGUI (){
GUI.depth = -1000;
GUI.depth = -1000;
GUI.Label (new Rect (0, 0, Screen.width, Screen.height), t2d, gs);
}
void Update () {
if(FadeInIsStart){
if (a > 0) {
a -= Time.deltaTime / FadeInTime;
t2d.SetPixel (0, 0, new Color (FadeInColor.r, FadeInColor.g, FadeInColor.b, a));
t2d.Apply ();
}else{
FadeInIsStart = false;
FadeInIsDone = true;
}
}
if(FadeOutIsStart){
if (a < 1) {
a += Time.deltaTime / FadeOutTime;
t2d.SetPixel (0, 0, new Color (FadeOutColor.r, FadeOutColor.g, FadeOutColor.b, a));
t2d.Apply ();
}else{
FadeOutIsStart = false;
FadeOutIsDone = true;
}
}
}
// 淡入
public static void FadeIn(){
a = 1;
FadeInIsStart = true;
FadeInIsDone = false;
}
// 淡出
public static void FadeOut(){
a = 0;
FadeOutIsStart = true;
FadeOutIsDone = false;
}
}
全站熱搜