Search Unity

Loading Scenes from Asset Bundle

Discussion in 'Editor & General Support' started by Piflik, Sep 3, 2015.

  1. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    I am currently looking into AssetBundles for a current project, where I need to be able to load new Scenes from external sources, without rebuilding the application. AssetBundles seemed ideal for this, but I encountered a little roadblock when I try to load Scenes form a bundle.

    Loading the bundle itself is not a problem, and also getting the scene paths ist quite straight forward, but these paths are unusable.

    This is what I currently do in order to load a scene from a bundle, which looks extremely stupid to me...


    Code (CSharp):
    1. private void LoadBundle() {
    2.  
    3.         AssetBundle bundle = AssetBundle.CreateFromFile("AssetBundles/testbundle");
    4.        
    5.         Application.LoadLevel(ToScene(bundle.GetAllScenePaths()[0]));
    6.     }
    7.  
    8.     private string ToScene(string path) {
    9.  
    10.         string[] parts = path.Split('/');
    11.  
    12.         string scene = parts[parts.Length - 1];
    13.  
    14.         return scene.Remove(scene.Length - 6);
    15.     }

    Is there a better way to get a usable string to load a Scene than this? Or is this an oversight on Unity's part?