Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 rmccabe82 · Jan 28, 2015 at 09:11 PM · javascriptprefabassetbundleexportimport assets

Problems importing an asset bundle

Like many of you guys, I need to export asset bundles to be loaded into my game when necessary.

I have created a directory in my project called MyAssets, I intend to bundle the contents of this directory into an asset bundle.

For now, all that this contains is a 3D Cube object, for test purpose.

What I'd like to do is load this in during the game and see my cube in the current scene. Sounds easy! :-) But I cant get this working(!)

Ok, code for export (through the IDE as a menu option), taken from the numerous examples and documentation:

     [MenuItem("MyTools/Export test")]
     static void Export()
     {
         string assetFolder = "MyAssets/";
         string bundlename = "TestBundle";
     
         UnityEngine.Object[] assetPathsList = GetAtPath<UnityEngine.Object>(assetFolder);
         Debug.Log("Loaded " + assetPathsList.Length + " assets at path: Assets/" + assetFolder);
     
         if (assetPathsList.Length > 0)
         {
             Debug.Log("Creating asset bundle: " + bundlename + ".unity3d");
             BuildPipeline.BuildAssetBundle(null, assetPathsList, "Assets/" + assetFolder + bundlename + ".unity3d", 
 BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows);
             Debug.Log("Finished building asset bundle");
         }
     }
 
     public static T[] GetAtPath<T>(string path)
     {
         ArrayList al = new ArrayList();
         string[] fileEntries = Directory.GetFiles(Application.dataPath + "/" + path);
 
         foreach (string fileName in fileEntries)
         {
             int assetPathIndex = fileName.IndexOf("Assets");
             string localPath = fileName.Substring(assetPathIndex);
 
             Object t = Resources.LoadAssetAtPath(localPath, typeof(T));
 
             if (t != null)
                 al.Add(t);
         }
 
         T[] result = new T[al.Count];
         
         for (int i = 0; i < al.Count; i++)
             result[i] = (T)al[i];
         
         return result;
     }

This seems to work fine! Then I come to the import side of thing. Which Im not 100% clear on yet. I have created a script with the following code in an attempt to get working:

 using UnityEngine;
 using UnityEditor;
 
 public class Importer : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
 
         string path = EditorUtility.OpenFilePanel("Load Bundle", "", "unity3d");
 
         Debug.Log("Creating asset bundle from file " + path);
 
         AssetBundle assets = AssetBundle.CreateFromFile(path);
         
         assets.LoadAll();
         
         var loadedObject = (GameObject)assets.mainAsset;
         
         Debug.Log("Instantiating object in scene");
 
         Instantiate(loadedObject, new Vector3(0, 0, 0), new Quaternion());
 
         Debug.Log("Instantiated object successfully");
     }
 }


I wasnt sure where to apply this, so for test purposes I applied it to a panel in my scene, hoping that instantiating would make the imported asset a child of the panel but it gives me the following error:

ArgumentException: The prefab you want to instantiate is null.

Can someone help me by pointing out what I'm doing wrong? :-)

Thanks in advance guys!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Erdroy · Mar 23, 2015 at 04:54 PM

I know, old topic... You should do this like:

 var loadedObject = assets.LoadAll()[0];
 Instantiate(loadedObject /*...*/);

or

 foreach(var _object in assets.LoadAll())
 {
     Instantiate(_object /*...*/);
 }

Instead of:

 var loadedObject = (GameObject)assets.mainAsset;
 Instantiate(loadedObject, new Vector3(0, 0, 0), new Quaternion());
Comment
Add comment · Show 1 · 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 rmccabe82 · Apr 07, 2015 at 12:59 PM 0
Share

I got that in the end myself but forgot to update this question. Thanks for posting, hopefully others can find use from this as well!! :-)

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

20 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

Related Questions

How to export Prefab to .FBX file on iOS platform at runtime ? 0 Answers

making 3 different gameobjects as 1 prefab. 1 Answer

Asset Bundle Prefabs missing textures & materials, but have geometry 1 Answer

How to save a prefab with script attached to it? 2 Answers

How Export and Downloading Asset Bundle at runtime? 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