Unity 切换游戏场景时屏幕淡入淡出效果
发表于2017-02-14
很多游戏都会有游戏场景切换,那么如何在切换场景时让游戏屏幕有淡入淡出的效果呢,相信会有很多开发者想知道,为此下面就给大家介绍下在Unity中切换游戏场景时屏幕淡入淡出的效果实现,一起来看看吧。
下面是代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | using UnityEngine; using System.Collections; public class SceneFadeInOut : MonoBehaviour { public float fadeSpeed = 1.5f; private bool sceneStarting = true ; private bool sceneEnding = false ; void Awake() { guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height); } void Update() { if (sceneStarting) { StartScene(); } if (sceneEnding) { EndScene(); } } void FadeToClear() { guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime); } void FadeToBlack() { guiTexture.color = Color.Lerp (guiTexture.color, Color.black, fadeSpeed * Time.deltaTime); } void StartScene() { FadeToClear(); if (guiTexture.color.a < 0.05f) { guiTexture.color = Color.clear; guiTexture.enabled = false ; sceneStarting = false ; } } public void EndScene() { guiTexture.enabled = true ; FadeToBlack(); if (guiTexture.color.a >= 0.95f) { sceneEnding = false ; Application.LoadLevel(1); } } void OnGUI() { if (!sceneStarting && GUI.Button ( new Rect(0,0,100,100), "NewLevel" )) { sceneEnding = true ; } } } |