前幾天在玩我叫MT 2遊戲的時候發現打開他們應用的時候發現他們Logo淡入淡出的效果做的挺好的,例如第一張是運營商騰訊的logo第二張是他們公司的遊戲logo。我們也來模仿一下:
第一張圖片:運營商的
這是第二張圖片,遊戲公司自己的。
1,新建測試工程。
腳本文件SplashScreen.cs
using UnityEngine; using System.Collections; public class SplashScreen : MonoBehaviour { //要加載的關卡 public string LevelToLoad="DiveUnityDemo"; //Logo貼圖 public Texture2D SplashLogo; //漸入漸出速度 public float FadeSpeed=0.3F; //等待時間 public float WaitTime=0.5F; #region 漸入漸出的類型 public enum SplashType { LoadLevelThenFadeOut, FadeOutThenLoadLevel } public SplashType Type=SplashType.LoadLevelThenFadeOut; #endregion #region 漸入漸出的狀態 public enum FadeStatus { FadeIn, FadeWait, FadeOut } private FadeStatus mStatus=FadeStatus.FadeIn; #endregion //是否允許玩家觸發漸入漸出事件 public bool WaitForInput=true; //當前透明度 private float mAlpha=0.0F; //攝像機 private Camera mCam; private GameObject mCamObj; //Logo貼圖位置 private Rect mSplashLogoPos; //漸入結束的時間 private float mFadeInFinishedTime; //關卡是否加載完畢 private bool LevelisLoaded=false; void Start () { //保存相機 mCam=Camera.main; mCamObj=Camera.main.gameObject; //計算Logo繪製的位置 mSplashLogoPos.x=(Screen.width * 0.5F-SplashLogo.width * 0.5F); mSplashLogoPos.y=(Screen.height * 0.5F-SplashLogo.height * 0.5F); mSplashLogoPos.width=SplashLogo.width; mSplashLogoPos.height=SplashLogo.height; //如果是漸出後加載關卡則保留相機 if (Type == SplashType.LoadLevelThenFadeOut) { DontDestroyOnLoad(this); DontDestroyOnLoad(Camera.main); } //檢查目標關卡是否爲空 if ((Application.levelCount <= 1) || (LevelToLoad == "")) { Debug.Log("There is not have the level to load please check again"); return; } } void Update () { switch(mStatus) { case FadeStatus.FadeIn: mAlpha+=FadeSpeed * Time.deltaTime; break; case FadeStatus.FadeOut: mAlpha-=FadeSpeed * Time.deltaTime; break; case FadeStatus.FadeWait: //當設定爲FadeWait時可根據時間判定或者玩家觸發進入下一個狀態 if((!WaitForInput && Time.time>mFadeInFinishedTime+WaitTime) || (WaitForInput && Input.anyKey)) { mStatus=FadeStatus.FadeOut; } break; } } void OnGUI() { if(SplashLogo!=null) { //繪製Logo GUI.color=new Color(GUI.color.r,GUI.color.g,GUI.color.b,Mathf.Clamp(mAlpha,0,1)); GUI.DrawTexture(mSplashLogoPos,SplashLogo); //狀態判斷 if(mAlpha>1.0F) { mStatus=FadeStatus.FadeWait; mFadeInFinishedTime=Time.time; mAlpha=1.0F; //如果需要在漸入結束後加載關卡 if(Type==SplashType.LoadLevelThenFadeOut) { mCam.depth=-1000; } } if(mAlpha<0.0F) { //如果需要在關卡加載完後漸出 if(Type==SplashType.FadeOutThenLoadLevel) { Application.LoadLevel("sence2"); }else { Destroy(mCamObj); Destroy(this); } } } if(mStatus==FadeStatus.FadeWait) { mStatus = FadeStatus.FadeOut; StartCoroutine("loadSence2"); //Debug.Log("請按任意鍵繼續"); } } public IEnumerator loadSence2() { yield return new WaitForSeconds(2f); Application.LoadLevel("sence2"); } void OnLevelWasLoaded(int index) { //如果目標關卡已加載需要手動銷燬場景中的GUI和AudioListener if(LevelisLoaded) { Destroy(mCam.GetComponent<AudioListener>()); Destroy(mCam.GetComponent<GUILayer>()); } } }
將腳本掛到MainCamera上,添加圖片即可!
運行效果:
源代碼github連接:https://github.com/wuzhangwuzhang/test/tree/master/testFade