Unity3d 添加启动过渡场景
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-****:原文地址: http://blog.****.net/cocos2der/article/details/44099095