Quantcast
Channel: Questions in topic: "assetbundle"
Viewing all articles
Browse latest Browse all 1593

Problems importing an asset bundle

$
0
0
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(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(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!

Viewing all articles
Browse latest Browse all 1593

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>