查看: 1722|回复: 9
打印 上一主题 下一主题

制作载入画面

[复制链接]

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

跳转到指定楼层
楼主
发表于 2011-9-5 08:06:09 |只看该作者 |倒序浏览
using UnityEngine;

using System.Collections;
//

// SplashScreen Script

//

// Version 0.1 by Martijn Dekker

// martijn.pixelstudio@gmail.com

//

// Version 0.2 by Ferdinand Joseph Fernandez, 2010Sep7 16:45 GMT + 8

// Changes:

//  * changed levelToLoad to a string, for easier usage

//  * added waitTime, which adds a pause after fade in, and before fade

//    out (during fade waiting)

//  * added option to either automatically fade out after waitTime

//    seconds (default), or wait for user input (press any key to continue)

//  * added option to wait until fade out is complete before loading next

//    level, instead of the default, which is to load the next level

//    before fade out

//

// Version 0.3 by Ferdinand Joseph Fernandez, 2010Sep8 01:13 GMT + 8

// Changes:

//  * splash screen itself is now fading without the need for a solid

//    background color

//  * optimized some code

//

// Version 0.4 by Ferdinand Joseph Fernandez, 2010Sep14 14:09 GMT + 8

// Changes:

//  * splash screen picture can now be either centered (default) or

//    stretched on the screen

//

// Version 0.5 by Ferdinand Joseph Fernandez, 2010Sep15 18:27 GMT + 8

// Changes:

//  * now has option to start automatically or not. if not started

//    automatically, the splash screen can be started by calling

//    the StartSplash function

//  * code acknowledges if the levelToLoad is blank, in that case,

//    the code simply does not attempt to load a level

//

// Version 0.6 by Ferdinand Joseph Fernandez, 2010Sep29 13:43 GMT + 8

// Changes:

//  * added the property "gui depth" so you can control at which depth the

//    splash screen shows in

//
public class SplashScreen : MonoBehaviour

{

    public int guiDepth = 0;

    public string levelToLoad = ""; // this has to correspond to a level (file>build settings)

    public Texture2D splashLogo; // the logo to splash;

    public float fadeSpeed = 0.3f;

    public float waitTime = 0.5f; // seconds to wait before fading out

    public bool waitForInput = false; // if ***e, this acts as a "press any key to continue"

    public bool startAutomatically = ***e;

    private float timeFadingInFinished = 0.0f;
    public enum SplashType

    {

        LoadNextLevelThenFadeOut,

        FadeOutThenLoadNextLevel

    }

    public SplashType splashType;
    private float alpha = 0.0f;
    private enum FadeStatus

    {

        Paused,

        FadeIn,

        FadeWaiting,

        FadeOut

    }

    private FadeStatus status = FadeStatus.FadeIn;
    private Camera oldCam;

    private GameObject oldCamGO;
    private Rect splashLogoPos = new Rect();

    public enum LogoPositioning

    {

        Centered,

        Stretched

    }

    public LogoPositioning logoPositioning;
    private bool loadingNextLevel = false;
    void Start()

    {

        if (startAutomatically)

        {

            status = FadeStatus.FadeIn;

        }

        else

        {

            status = FadeStatus.Paused;

        }

        oldCam = Camera.main;

        oldCamGO = Camera.main.gameObject;
        if (logoPositioning == LogoPositioning.Centered)

        {

            splashLogoPos.x = (Screen.width * 0.5f) - (splashLogo.width * 0.5f);

            splashLogoPos.y = (Screen.height * 0.5f) - (splashLogo.height * 0.5f);
            splashLogoPos.width = splashLogo.width;

            splashLogoPos.height = splashLogo.height;

        }

        else

        {

            splashLogoPos.x = 0;

            splashLogoPos.y = 0;
            splashLogoPos.width = Screen.width;

            splashLogoPos.height = Screen.height;

        }

        if (splashType == SplashType.LoadNextLevelThenFadeOut)

        {

            DontDestroyOnLoad(this);

            DontDestroyOnLoad(Camera.main);

        }

        if ((Application.levelCount <= 1) || (levelToLoad == ""))

        {

            Debug.LogWarning("Invalid levelToLoad value.");

        }

    }
    public void StartSplash()

    {

        status = FadeStatus.FadeIn;

    }
    void Update()

    {

        switch(status)

        {

            case FadeStatus.FadeIn:

                alpha += fadeSpeed * Time.deltaTime;

            break;

            case FadeStatus.FadeWaiting:

                if ((!waitForInput && Time.time >= timeFadingInFinished + waitTime) || (waitForInput && Input.anyKey))

                {

                    status = FadeStatus.FadeOut;

                }

            break;

            case FadeStatus.FadeOut:

                alpha += -fadeSpeed * Time.deltaTime;

            break;

        }

    }
    void OnGUI()

    {

        GUI.depth = guiDepth;

        if (splashLogo != null)

        {

            GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, Mathf.Clamp01(alpha));

            GUI.DrawTexture(splashLogoPos, splashLogo);

            if (alpha > 1.0f)

            {

                status = FadeStatus.FadeWaiting;

                timeFadingInFinished = Time.time;

                alpha = 1.0f;

                if (splashType == SplashType.LoadNextLevelThenFadeOut)

                {

                    oldCam.depth = -1000;

                    loadingNextLevel = ***e;

                    if ((Application.levelCount >= 1) && (levelToLoad != ""))

                    {

                        Application.LoadLevel(levelToLoad);

                    }

                }

            }

            if (alpha < 0.0f)

            {

                if (splashType == SplashType.FadeOutThenLoadNextLevel)

                {

                    if ((Application.levelCount >= 1) && (levelToLoad != ""))

                    {

                        Application.LoadLevel(levelToLoad);

                    }

                }

                else

                {

                    Destroy(oldCamGO); // somehow this doesn't work

                    Destroy(this);

                }

            }

        }

    }
    void OnLevelWasLoaded(int lvlIdx)

    {

        if (loadingNextLevel)

        {

            Destroy(oldCam.GetComponent<AudioListener>());

            Destroy(oldCam.GetComponent<GUILayer>());

        }

    }
    void OnDrawGizmos()

    {

        Gizmos.color = new Color(1f, 0f, 0f, .5f);

        Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1));

    }

}

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

Asen    

867

主题

0

听众

1万

积分

外协人员

Rank: 7Rank: 7Rank: 7

纳金币
17488
精华
1
沙发
发表于 2011-9-7 08:36:57 |只看该作者
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-5-18 23:20:58 |只看该作者
很经典,很实用,学习了!
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-6-9 23:22:18 |只看该作者
不错哦,谢谢楼主
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-6-20 23:23:08 |只看该作者
我看看就走,你们聊!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

6#
发表于 2012-7-2 23:18:59 |只看该作者
顶!学习了!阅!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

7#
发表于 2012-9-7 23:49:53 |只看该作者
路过、路过、快到鸟,列位请继续...ing
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-9-29 23:25:43 |只看该作者
再看一看,再顶楼主
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

9#
发表于 2012-10-1 23:22:25 |只看该作者
不错哦,谢谢楼主
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

10#
发表于 2013-2-1 23:33:54 |只看该作者
我看看就走,你们聊!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-27 23:31 , Processed in 0.075466 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部