我看到a video 我做了我的,但它的工作很奇怪......
当我播放场景时,会出现淡入淡出效果,并使用Ctrl + P激活场景,而不是在场景发生变化时。
using UnityEngine;
using System.Collections;
public class Fading : MonoBehaviour {
public Texture2D FadeOutTexture; // 화면에 오버레이할 텍스쳐
public float fadeSpeed = 0.4f; // fading speed
private int drawDepth = -1000; //the texture's order int the draw hierarchy:a low number means it renders on top
private float alpha = 1.0f; //the texture's alpha value between 0 and 1
private int fadeDir = -1; // the direction to fade:in=-1, out:1
void OnGUI(){
// fade out/in the alpha value using direction, a speed and time, deltatime to convert the operation to seconds
alpha += fadeDir * fadeSpeed * Time.deltaTime;
// force (clamp) the number between 0 and 1 because GUI.color uses alpha values between 0 and 1
alpha = Mathf.Clamp01(alpha);
// set color of our GUI (in this case out Texture). All color values remain the same & the Alpha is set to the alpha variable
GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha); // set the aplha value;
GUI.depth = drawDepth;
GUI.DrawTexture (new Rect (0,0,Screen.width, Screen.height), FadeOutTexture); // draw the texture to fit the enire screen area
}
// sets fadeDir to the direction parameter making the scene fade in if-1 and out if 1
public float BeginFade(int direction){
fadeDir = direction;
return (fadeSpeed); //return the fadeSpeed variable so it's easy to time the Application.LoadLevel();
}
//OnLevelWasLoaded is called when a level is loaded. It takes loaded level index (int) as a parameter so you can Limit the fade in to certin scenes
void OnLevelWasLoaded(){
alpha = 1; // use this if the alpha is not set to 1 by default
BeginFade(-1);
}
}
void Update () {
if (slimeInContact && CustomInput.CI.isUpTap) {
StartCoroutine(fade());
SceneManager.LoadScene(Scene.name);
}
IEnumerator fade(){
float fadeTime = GameObject.Find ("window").GetComponent<Fading> ().BeginFade (1);
yield return new WaitForSeconds (fadeTime);
}