- Home /
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!
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());
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
Follow this Question
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