Blogger Information
Blog 95
fans 0
comment 11
visits 248727
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Unity3d切换加载场景 SceneManager.LoadScene
龍__遇见彩虹的博客
Original
10882 people have browsed it

SceneManager.LoadScene

public static void LoadScene(int sceneBuildIndex,SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);

                   public static void LoadScene(string sceneName,SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);


Description:

Loads the scene by its name or index in Build Settings.



例子1:

using UnityEngine;
using UnityEngine.SceneManagement;//使用场景管理器

public class ExampleClass : MonoBehaviour{

    void Start()
    {
          //使用LoadScene加载场景
          //第二个参数AddSceneMode.Additive表示当前场景不销毁,并加载需要的场景
        SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive);
    }
}


例子2:

using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadScene : MonoBehaviour{

    private AssetBundle myLoadedAssetBundle;
    private string[] scenePaths;

    // Use this for initialization
    void Start()
    {
          //获取所有场景
        myLoadedAssetBundle = AssetBundle.LoadFromFile("Assets/AssetBundles/scenes");
        scenePaths = myLoadedAssetBundle.GetAllScenePaths();
    }
    
    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 100, 30), "Change scene"))
        {
                Debug.Log("scene2 loading: " + scenePaths[0]);
                
                //销毁当前场景,加载要加载的场景
                SceneManager.LoadScene(scenePaths[0], LoadSceneMode.Single);
        }
    }
}


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post