How do you import non-hierarcy assets from an Asset Bundle?
I've lost a full day here and looking for some help.
Our game has a large number of fbx files that contain animation.
I am successfully creating asset bundles of these FBX files. At runtime, I can download the neatly packaged Asset Bundle from our server.
I am extracting the Bundle like this:
AssetBundle asset = asset_download.assetBundle;
I have tried:
asset.LoadAllAssets()
asset.LoadAsset(asset_path)
But I am lost as to what is being extracted and to where.
For convenience sake, all of our FBX files are in /Resources/anim/asset_name/...
Although that seems to not be correct if we're using asset bundles.
I had expected that on asset.LoadAllAssets(), those FBX files would simply appear back in their respective /Resources/ path and become accessible to anything that needs them.
It seems as though the only way I can do anything with the assets that have been bundled is:
Instantiate(asset.LoadAsset(asset_name));
Which puts a clone of the packaged object in the current scene and not at all what I need.
Any idea how to put resources back into some sort of persistent "project" location so that those animation files can be used as if they had been packaged with the build?
Answer by Cynikal · Oct 04, 2016 at 10:47 PM
I see you put:
asset.LoadAsset(asset_path). This is not correct.
It's asset.LoadAsset(asset_NAME) (like you have mentioned below in your instantiate).
According to the Wiki, you can do: asset.LoadAsset(asset_name, TYPE); Where type is, you could put Animation. I am assuming (since i've never used Asset Bundles for any of my actual projects, just did it for testing) that you can put it into a List myAnis = asset.LoadAsset(asset_name, Animation); or something similar.
Unfortunately, this doesn't work either. I'm baffled by this - what happens to the asset on bundle.LoadAsset(asset_name)? Does it have to go into the current scene?
Whether I load the asset using the full path: "assets/content/anim/yari/20161002182113.fbx" or the name "20161002182113" nothing happens. Unless I wrap it in Instantiate(), which loads it into the current scene, which isn't what I want.
Sure - you can download it from http://oapi.co/kurotama/local_storage/bundles/20161002182113
It contains a single FBX file that only contains animation: assets/content/anim/yari/20161002182113.fbx
$$anonymous$$y goal is to take this animation and apply it to an Animator Controller on an existing model after loading the asset bundle. But Once loading the asset, I have no idea where it lives or how to access it.
using System;
using UnityEngine;
using System.Collections;
using UnityEditor;
using Object = UnityEngine.Object;
public class TestForUnityAnswers : $$anonymous$$onoBehaviour
{
// Use this for initialization
void Start()
{
StartCoroutine(DownloadAsset<AnimationClip>("20161002182113", "http://oapi.co/kurotama/local_storage/bundles/20161002182113", 1));
}
public Object Obj;
public IEnumerator DownloadAsset<T>(string asset, string url, int version) where T : Object
{
Obj = null;
while (!Caching.ready)
yield return null;
using (WWW www = WWW.LoadFromCacheOrDownload(url, version))
{
yield return www;
if (www.error != null) throw new Exception("WWW download: "+ www.error);
AssetBundle assetBundle = www.assetBundle;
Obj = assetBundle.LoadAsset(asset, typeof(T));
assetBundle.Unload(false);
}
//GameObject test = GameObject.Find("TESTFORUNITY");
//obj = contains your animations.
}
}
I really appreciate you taking the time to put this together. It clarifies a bit of my confusion, but I'm still largely confused.
Your code explicitly sets the objects on unpacking. If there are dependencies, say an Animation Controller that references FBX files, I assume the FBX files need to be explicitly unpacked first. Do you know if that's the case?
Further, this works fine for a simple example like the one I mention here, but I'm concerned about how this scales. If I were to have a bundle with 20 or so FBX files and 30 controllers and other components, do they always need to be unpacked with this degree of care at runtime?
Is there no way to unpack the bundle once and allow those elements to simply exist? Or does a cached bundle always need to be decompressed at runtime and loaded?
Again, you've been super helpful and I understand if you don't have all of the answers - just thrilled to have someone who can provide some clarity!
So, you'll want to do manipulate the "Obj" file, which contains your animations... then add them to wherever.