Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by antx · Sep 11, 2015 at 07:30 AM · unity 5sceneassetbundlescene-loading

How to load a scene from an asset bundle in unity 5?

Loading GameObjects from assetbundles is easy since there is documentation available. But how do I load a scene?

 ...
         using(www = WWW.LoadFromCacheOrDownload(moduleAddress, 1)) {
             yield return www;
             
             if (string.IsNullOrEmpty(www.error)) {
                 AssetBundle assetBundle = www.assetBundle;
                 if (assetBundle != null) {
                     string[] scenePath = assetBundle.GetAllScenePaths();
                     Debug.Log("scenepath: " + scenePath[0]);
                     Application.LoadLevel(scenePath[0]);
                 }
                 else Debug.Log("assetBundle is null");
 
                 assetBundle.Unload(false);
                 www.Dispose();
             }
             else Debug.Log(www.error);
         }

This line fails with: IndexOutOfRangeException: Array index is out of range: Debug.Log("scenepath: " + scenePath[0]);

Do I have to do something else first or is there something wrong with my asset bundle?

I build the asset bundle just like I did it with gameObjects: Select the Scene in the Project view, assigned it to an asset bundle at the bottom of the inspector and then run the Editor script:

     [MenuItem("Assets/Build AssetBundles - Editor")]
     static void BuildAssetBundles_Editor ()
     {
         BuildPipeline.BuildAssetBundles (    "AssetBundles/Editor", 
                                             BuildAssetBundleOptions.None, 
                                             BuildTarget.StandaloneWindows);
     }
 

I just can't find a decent example on the internet and the documentation does not explain it either. So I'm asking here.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image antx · Sep 11, 2015 at 01:12 PM 2
Share

Okay, I got a little bit further. The scene gets loaded now, but none of the scripts in it are working.

However, for one part I don't know what I did but I got over the empty scenePath[] somehow. I guess a rebuild of the asset was responsible (with different name).

For another part I had to strip the path and the filename.extension from the scene name:

string[] scenePath = assetBundle.GetAllScenePaths();

string mySceneName = scenePath[0];

mySceneName = mySceneName.Substring(0, mySceneName.Length - 6);

mySceneName = mySceneName.Substring(mySceneName.LastIndexOf("/") + 1);

Application.LoadLevel(mySceneName);

avatar image VMohan · Dec 14, 2015 at 05:27 AM 0
Share

Thank yo so much for positing your answer. It helped me figure out how to load my scene!

avatar image vanshika1012 · Dec 01, 2016 at 10:14 AM 0
Share

I am facing the exact same problem. Can you please elaborate more about your solution. Its really urgent.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by vishal · Dec 02, 2016 at 06:26 PM

Don't forget to add your scene to build setting.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jjennings1990 · Dec 08, 2016 at 11:42 PM 0
Share

Thank you this seemed to have worked for me but can you explain how this is working ? I just want to make sure this isn't a fluke . $$anonymous$$y scene is still being loaded from the asset bundle even though it's referenced in the build settings ?

It looks like it's working I just want to make sure this isn't too good to be true. haha . Thank you !

avatar image RLasne · Mar 27, 2017 at 02:22 PM 0
Share

What if I want to add new scenes to an already built game ? Is there a way to do this ?

avatar image vishal RLasne · Dec 12, 2018 at 11:39 AM 0
Share

@RLasne In that case you have to call Scene$$anonymous$$anager.CreateScene (Empty container) and LoadScene$$anonymous$$ode.Additive for any further assistance.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

35 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does a scene loaded from an Asset Bundle returns empty string when getting its name? 0 Answers

'Resource file has already been unloaded' error when exiting application in Editor 1 Answer

How to change scene without the previous scene being reset when you return? 0 Answers

How to export a Scene as Asset-bundle [Through Command-line args] 0 Answers

Scene assets not bundled in asset bundle 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges