Unity5個人版會添加Unity Logo作為啟動畫面,但Unity Logo結束后可以再添加一個自己的啟動畫面,pro版的可以在Edit→Project Settings→Player→Splash Image→去掉勾來跳過Unity自己的啟動畫面。
下面步入正題:
新建一個C#腳本 LHSplashScreens.cs
1 using UnityEngine; 2 using System.Collections; 3 public enum FadeStatus 4 { 5 FadeIn, 6 FadeWaiting, 7 FadeOut 8 } 9 public class LHSplashScreens : MonoBehaviour 10 { 11 public string levelToLoad; 12 public bool waitForInput; 13 public float timeFadingInFinished; 14 public Sprite splashSprite; 15 private float m_fadeSpeed; 16 private float m_waitTime; 17 private float m_alpha; 18 private FadeStatus m_status; 19 private SpriteRenderer m_splashSpriteRenderer; 20 public LHSplashScreens() 21 { 22 levelToLoad = ""; 23 m_fadeSpeed = 0.3f; 24 m_waitTime = 0.5f; 25 m_status = FadeStatus.FadeIn; 26 } 27 void Awake() 28 { 29 Application.targetFrameRate = 60; 30 } 31 // Use this for initialization 32 void Start () 33 { 34 if (Application.levelCount <= 1 || levelToLoad == "") 35 { 36 Debug.LogWarning("Invalid levelToLoad value."); 37 } 38 GameObject m_splashSpriteGO = new GameObject("SplashSprite"); 39 m_splashSpriteGO.AddComponent<SpriteRenderer>(); 40 m_splashSpriteRenderer = m_splashSpriteGO.GetComponent<SpriteRenderer>(); 41 m_splashSpriteRenderer.sprite = splashSprite; 42 Transform m_splashSpriteTransform = m_splashSpriteGO.gameObject.transform; 43 m_splashSpriteTransform.position = new Vector2(0f, 0f); 44 m_splashSpriteTransform.parent = this.transform; 45 } 46 // Update is called once per frame 47 void Update () 48 { 49 FadeStatus fadeStatus = m_status; 50 if (fadeStatus == FadeStatus.FadeIn) 51 { 52 m_alpha += m_fadeSpeed * Time.deltaTime; 53 } 54 else if (fadeStatus == FadeStatus.FadeWaiting) 55 { 56 if ((!waitForInput && Time.time >= timeFadingInFinished + m_waitTime) || (waitForInput && Input.anyKey)) 57 { 58 m_status = FadeStatus.FadeOut; 59 } 60 } 61 else if (fadeStatus == FadeStatus.FadeOut) 62 { 63 m_alpha -= m_fadeSpeed * Time.deltaTime; 64 } 65 UpdateSplashAlpha(); 66 } 67 private void UpdateSplashAlpha() 68 { 69 if (m_splashSpriteRenderer != null) 70 { 71 Color spriteColor = m_splashSpriteRenderer.material.color; 72 spriteColor.a = m_alpha; 73 m_splashSpriteRenderer.material.color = spriteColor; 74 if (m_alpha > 1f) 75 { 76 m_status = FadeStatus.FadeWaiting; 77 timeFadingInFinished = Time.time; 78 m_alpha = 1f; 79 } 80 if (m_alpha < 0) 81 { 82 if (Application.levelCount >= 1 && levelToLoad != "") 83 { 84 Application.LoadLevel(levelToLoad); 85 } 86 } 87 } 88 } 89 }
在你工程中,建立一個新的場景,作為游戲的啟動場景。
把LHSplashScreens.cs腳本掛在場景中可以啟動了的對象中,設置參數:
- Level To Load: 完成啟動畫面后你需要加載的場景
- Splash Sprite:過渡使用的logo sprite
運行之后就可以看到一張圖片漸顯漸隱之后跳轉到你指定的場景中。
感謝Cocos2Der-CSDN:原文地址: http://blog.csdn.net/cocos2der/article/details/44099095