弹来弹去跑马灯!

Unity 异步加载场景 动画Loading

参考:Application.LoadLevelAsync 异步加载关卡
static function LoadLevelAsync (levelName : string) : AsyncOperation
http://www.ceeger.com/Script/Application/Application.LoadLevelAsync.html

建2个C#脚本:LoadingComponent.cs 和 LoadLevelOnClick.cs


LoadingComponent.cs:

using UnityEngine;

[ExecuteInEditMode]
public class LoadingComponent : MonoBehaviour
{
private AsyncOperation asy;
public Texture2D backgroundImage;
public Font fontType;
public string levelName;
public AudioClip loadingAudio;
public float loadingBarHeight = 25f;
public Texture2D loadingBarImage;
public Vector2 loadingBarPosition;
public string loadingText = "Loading Now...";
public Color loadingTextColor;
public FontStyle loadingTextFont;
public int loadingTextSize = 5;
public bool normalized;
public bool showProgressPercentage = true;
public float startDelay = 1.0f;
private GUIStyle style;
private GUIStyle textStyle;
public Texture2D emptyBarImage;
private GUIStyle emptyStyle;

private void Start()
{
    if (Application.isPlaying)
    {
        if (loadingAudio)
        {
            if (!GetComponent<AudioSource>())
            {
                gameObject.AddComponent("AudioSource");
            }


            GetComponent<AudioSource>().loop = true;
            GetComponent<AudioSource>().playOnAwake = false;
            GetComponent<AudioSource>().clip = loadingAudio;
        }
        gameObject.AddComponent("GUITexture");
        style = new GUIStyle();
        style.normal.background = loadingBarImage;
        style.alignment = TextAnchor.MiddleCenter;
        style.normal.textColor = loadingTextColor;
        style.fontStyle = loadingTextFont;
        style.fontSize = loadingTextSize;
        textStyle = new GUIStyle();
        textStyle.alignment = TextAnchor.MiddleCenter;
        textStyle.fontStyle = loadingTextFont;
        textStyle.normal.textColor = loadingTextColor;
        textStyle.fontSize = loadingTextSize;
        textStyle.font = fontType;
        emptyStyle = new GUIStyle();
        emptyStyle.normal.background = emptyBarImage;
     
    }
}

private void Update()
{
    if (normalized)
    {
        loadingBarPosition.x = Mathf.Clamp(loadingBarPosition.x, 0f, 1f);
        loadingBarPosition.y = Mathf.Clamp(loadingBarPosition.y, 0f, 1f);
    }
}

private void LoadNextLevel()
{
    transform.position = new Vector3(0, 0, 100);
    transform.localScale = Vector3.zero;

    guiTexture.pixelInset = new Rect(0, 0, Screen.width, Screen.height);
    guiTexture.texture = backgroundImage;
    if (loadingAudio)
    {
        audio.Play();
    }
    DontDestroyOnLoad(this);
    asy = Application.LoadLevelAsync(levelName);
}


private void OnGUI()
{
    if (asy != null)
    {
        if (!asy.isDone)
        {

            if (!normalized)
            {
                GUI.Label(new Rect(Screen.width/2, loadingBarPosition.y - 20, 50, 50), loadingText, textStyle);

                if (showProgressPercentage)
                {
                    
                  
                    GUI.Box(
                        new Rect(loadingBarPosition.x, loadingBarPosition.y,
                                 Screen.width - (loadingBarPosition.x * 2),
                                 loadingBarHeight),
                        "", emptyStyle);
                      
                    GUI.Label(
                        new Rect(loadingBarPosition.x, loadingBarPosition.y,
                                 ((Screen.width - (loadingBarPosition.x*2))*(asy.progress*100f))/100f,
                                 loadingBarHeight),
                        "", style);

                    GUI.Label(
                     new Rect(loadingBarPosition.x, loadingBarPosition.y,
                              Screen.width - (loadingBarPosition.x * 2),
                              loadingBarHeight),
                     Mathf.RoundToInt(asy.progress * 100f) + "%", emptyStyle);
                }
                else
                {
                    GUI.Box(
                        new Rect(loadingBarPosition.x, loadingBarPosition.y,
                                 Screen.width - (loadingBarPosition.x * 2),
                                 loadingBarHeight),
                        "", emptyStyle);
                    GUI.Label(
                        new Rect(loadingBarPosition.x, loadingBarPosition.y,
                                 ((Screen.width - (loadingBarPosition.x*2))*(asy.progress*100f))/100f,
                                 loadingBarHeight),
                        "", style);
                }
            }
            else
            {
                GUI.Label(new Rect(Screen.width/2, (loadingBarPosition.y*Screen.height) - 50, 50, 50),
                          loadingText, textStyle);
                if (showProgressPercentage)
                {
                    GUI.Box(
                     new Rect(loadingBarPosition.x * Screen.width, loadingBarPosition.y * Screen.height,
                             Screen.width - (loadingBarPosition.x * Screen.width * 2),
                              loadingBarHeight),
                     "", emptyStyle);
                    GUI.Label(
                        new Rect(loadingBarPosition.x*Screen.width, loadingBarPosition.y*Screen.height,
                                 ((Screen.width - (loadingBarPosition.x*Screen.width*2))*(asy.progress*100f))/
                                 100f,
                                 loadingBarHeight),
                        "", style);
                    GUI.Label(
                    new Rect(loadingBarPosition.x * Screen.width, loadingBarPosition.y * Screen.height,
                            Screen.width - (loadingBarPosition.x * Screen.width * 2),
                             loadingBarHeight),
                    Mathf.RoundToInt(asy.progress * 100f) + "%", textStyle);
                }
                else
                {
                    GUI.Box(
                     new Rect(loadingBarPosition.x * Screen.width, loadingBarPosition.y * Screen.height,
                              Screen.width - (loadingBarPosition.x * Screen.width * 2),
                              loadingBarHeight),
                     "", emptyStyle);
                    GUI.Label(
                        new Rect(loadingBarPosition.x*Screen.width, loadingBarPosition.y*Screen.height,
                                 ((Screen.width - (loadingBarPosition.x*Screen.width*2))*(asy.progress*100f))/
                                 100f,
                                 loadingBarHeight),
                        "", style);
                }
            }
        }
        else
        {
            if (!normalized)
            {
                GUI.Label(new Rect(Screen.width/2, loadingBarPosition.y - 50, 50, 50), loadingText, textStyle);
                if (showProgressPercentage)
                {
                    GUI.Box(
                     new Rect(loadingBarPosition.x, loadingBarPosition.y,
                              Screen.width - (loadingBarPosition.x * 2),
                              loadingBarHeight),
                     "", emptyStyle);
                    GUI.Label(
                        new Rect(loadingBarPosition.x, loadingBarPosition.y,
                                 ((Screen.width - (loadingBarPosition.x*2))*(asy.progress*100f))/100f,
                                 loadingBarHeight),
                       "", style);
                    GUI.Label(
                    new Rect(loadingBarPosition.x, loadingBarPosition.y,
                             Screen.width - (loadingBarPosition.x * 2),
                             loadingBarHeight),
                    Mathf.RoundToInt(asy.progress * 100f) + "%", textStyle);
                }
                else
                {
                    GUI.Box(
                        new Rect(loadingBarPosition.x, loadingBarPosition.y,
                                 Screen.width - (loadingBarPosition.x * 2),
                                 loadingBarHeight),
                        "", emptyStyle);
                    GUI.Label(
                        new Rect(loadingBarPosition.x, loadingBarPosition.y,
                                 ((Screen.width - (loadingBarPosition.x*2))*(asy.progress*100f))/100f,
                                 loadingBarHeight),
                        "", style);
                }
            }
            else
            {
                GUI.Label(new Rect(Screen.width/2, (loadingBarPosition.y*Screen.height) - 50, 50, 50),
                          loadingText, textStyle);
                if (showProgressPercentage)
                {
                    GUI.Box(
                   new Rect(loadingBarPosition.x * Screen.width, loadingBarPosition.y * Screen.height,
                           Screen.width - (loadingBarPosition.x * Screen.width * 2),
                            loadingBarHeight),
                   "", emptyStyle);
                    GUI.Label(
                        new Rect(loadingBarPosition.x*Screen.width, loadingBarPosition.y*Screen.height,
                                 ((Screen.width - loadingBarPosition.x*Screen.width*2)*(asy.progress*100f))/100f,
                                 loadingBarHeight),
                        "", style);
                    GUI.Label(
                 new Rect(loadingBarPosition.x * Screen.width, loadingBarPosition.y * Screen.height,
                         Screen.width - (loadingBarPosition.x * Screen.width * 2),
                          loadingBarHeight),
                 Mathf.RoundToInt(asy.progress * 100f) + "%", textStyle);
                }
                else
                {
                    GUI.Box(
                   new Rect(loadingBarPosition.x * Screen.width, loadingBarPosition.y * Screen.height,
                           Screen.width - (loadingBarPosition.x * Screen.width * 2),
                            loadingBarHeight),
                   "", emptyStyle);
                    GUI.Label(
                        new Rect(loadingBarPosition.x*Screen.width, loadingBarPosition.y*Screen.height,
                                 ((Screen.width - loadingBarPosition.x*Screen.width*2)*(asy.progress*100f))/100f,
                                 loadingBarHeight),
                        "", style);
                }
            }
            Destroy(gameObject, startDelay);
        }
    }
}

}


LoadLevelOnClick.cs

using UnityEngine;

public class LoadLevelOnClick : MonoBehaviour
{
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
gameObject.SendMessage("LoadNextLevel");
}
}
}

源码下载: http://pan.baidu.com/s/1kUik3lH 密码: f5eh

posted @ 2016-01-02 16:29  wgscd  阅读(1351)  评论(0编辑  收藏  举报