unity 场景加载

来源:互联网 发布:mac os 英文字体 编辑:程序博客网 时间:2024/04/29 18:57

详解地址:http://www.xuanyusong.com/archives/1427


GUI.Draw Texture   绘制纹理

static function DrawTexture (position : Rectimage : TexturescaleMode : ScaleMode =ScaleMode.StretchToFillalphaBlend : bool = true, imageAspect : float = 0) : void

实例:

//背景图片

一:

GUI.DrawTexture(new Rect(0,0,Screen.height),backImg, ScaleMode.StretchToFill, true,0.0f);

//这个函数的原型可以省略,若只保留第一二个参数,alhpaBlend 图片的混合模式,是否通道混合图片显示,默认为混合通道,如果不,图片直接被绘制显示。

//scaleMode 将采用StretchToFill 的形式对GUI进行填充。

//scaleMode有三个参数,StretchToFill(伸缩图片填充满整个GUI),scaleAndGrop(保持原有图片形式,在GUI中显示),ScaleToFit(使图片适应GUI)

//imageAspect  如果是0,则使用原图的长宽比

二:Application.CanStreamedLevelBeLoaded 流关卡被加载

能流模式加载的关卡是否被加载?

e.ceeger.com/Script/Application/Application.CanStreamedLevelBeLoaded.html

static function CanStreamedLevelBeLoaded (levelIndex : int) : bool

update中检查能流模式能加载,则加载场景

 void Update()
    {
        if (Application.CanStreamedLevelBeLoaded(LoadingSceneName))
        {
            if (count == 0)
            {
                count++;
                StartCoroutine(LoadScene());
            }
        }

Application.LoadLevelAsync 异步加载关卡

http://game.ceeger.com/Script/Application/Application.LoadLevelAsync.html

 private IEnumerator LoadScene()
    {
        Debug.Log("LoadingSceneName:" + LoadingSceneName);
        //Debug.Log("开始加载场景...");
        //异步读取场景
        async = Application.LoadLevelAsync(LoadingSceneName);


        //读取完毕自动返回,系统会自动进入下一个场景
        yield return async;
    }

 //在这里计算读取的进度,

        //progress 的取值范围在0.1 - 1之间, 但是它不会等于1

        //也就是说progress可能是0.9的时候就直接进入新场景了

        //所以在写进度条的时候需要注意一下。
       //为了计算百分比 所以直接乘以100即可
       progress =  (int)(async.progress *100);

综上:loadScene分为以下几个大类
// public Texture2D backImg;  背景图片
//public static string LoadingSceneName = ""  
//public Texture2D roadImg;   //
//public Texture2D carImg;
// private int progress = 0;
//private int count = 0;

loadScene方法
 private IEnumerator LoadScene()
    {
        Debug.Log("LoadingSceneName:" + LoadingSceneName);
        //Debug.Log("开始加载场景...");
        //异步读取场景
        async = Application.LoadLevelAsync(LoadingSceneName);


        //读取完毕自动返回,系统会自动进入下一个场景
        yield return async;
    }
OnGUI里面
 void OnGUI()
    {
        //背景图片
        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), backImg, ScaleMode.StretchToFill, true, 0.0f);


        for (int i = 0; i < (Screen.width / roadImg.width) + 1; i++)
        {
            GUI.DrawTexture(new Rect(0 + roadImg.width * i, Screen.height * 0.85f, roadImg.width, roadImg.height), roadImg);
        }


        GUI.Label(new Rect(Screen.width * 0.25f, Screen.height * 0.2f, 600, 300), "场景加载中,请稍后……", textStyle);
        if (count > 0)
            GUI.DrawTexture(new Rect(Screen.width * async.progress, Screen.height * 0.85f, carImg.width, roadImg.height), carImg, ScaleMode.ScaleToFit, true, 0.0f);
        //GUI.DrawTexture(new Rect(0, Screen.height * 0.6f, roadImg.width, roadImg.height), roadImg);
        //显示计算后的进度
        //GUI.Label(new Rect(100, 100, 100, 40), "Loading!!! " + progress + "%");
    }

Update里面:

 void Update()
    {
        if (Application.CanStreamedLevelBeLoaded(LoadingSceneName))
        {
            if (count == 0)
            {
                count++;
                StartCoroutine(LoadScene());
            }
        }
        //计算场景的读取进度,这里progress的取值是0到1之间,但是它不会等于1
        //也就是说progress可能在0.9的时候就已经加载完新场景了
        if (async != null)
            progress = (int)(async.progress * 100);
    }