I'm saving an asset bundle like this: Code (CSharp): BuildPipeline.BuildAssetBundles("Assets/AssetBundles/", BuildAssetBundleOptions.None, BuildTarget.WSAPlayer); AssetDatabase.Refresh(); Then opening it like this: Code (CSharp): IEnumerator getObject() { WWW www = WWW.LoadFromCacheOrDownload(Application.dataPath + "/AssetBundles/primitives", 1); yield return www; AssetBundle bundle = www.assetBundle; AssetBundleRequest request = bundle.LoadAssetAsync<GameObject>("Sphere"); yield return request; GameObject sphere = request.asset as GameObject; Instantiate<GameObject>(sphere); print("successful sphere"); } I have my assetbundle inside of assets/AssetBundles/primitives. Loading works perfect in the editor, but always returns null when I deploy in uwp. What am I doing wrong?
What Unity version are you on? There was a bug in Unity 2017.3 that made WWW requests to fail, but it was fixed in 2017.4.2f1.
I haven't noticed the paths you were using. Assets/AssetBundles/ will not get copied to your final build. You should use StreamingAssets folder instead: https://docs.unity3d.com/Manual/StreamingAssets.html
I managed to make this work by using the persistentDataPath. If I was using StreamingAssets, would I still be able to import my asset with scripts on it that could be executed at run-time?
Are you packaging scripts themselves into the asset bundle, or just objects that have scripts attached?
The latter is easy and streaming assets will definitely work. Packing scripts themselves into a bundle is much more problematic .
Thanks! Is it possible to keep a data structure filled with scripts that I have preloaded, and then when I want to bring in the desired asset, attach them? I seem to be having trouble adding when trying to do this with AddComponent. The context here is that I'm allowing a user to set characteristics in a config file, and then have those be applied to the bundled assets at runtime.
Basically all I'm doing is trying to trying to add a particular script when I load the object. Code (CSharp): IEnumerator getObject() { WWW www = WWW.LoadFromCacheOrDownload(Application.persistentDataPath + "/primitives", 1); yield return www; AssetBundle bundle = www.assetBundle; AssetBundleRequest request = bundle.LoadAssetAsync<GameObject>("Sphere"); yield return request; GameObject sphere = request.asset as GameObject; ObstacleAttributes attrib = PreProcessor.obstacles["testing"]; GameObject test = Instantiate<GameObject>(sphere); test.AddComponent(attrib); Instantiate<GameObject>(sphere, new Vector3(3, 3, 3), Quaternion.identity, g.transform); print("successful sphere"); } The Script I'm trying to add to my new game object is a list of values assigned elsewhere: Code (CSharp): public class ObstacleAttributes : MonoBehaviour{ public string obj; public double origin_lat; public double origin_lon; public double appear_dist; public List<double> checkpoint_lat = new List<double>(); public List<double> checkpoint_long = new List<double>(); public double obstacle_speed; }
That should work, however, you're spawning 3 copies of the object: 1. In variable "sphere"; 2. In variable "test" with your added component 3. In the last call to Instantiate, even though you never store it anywhere.
Yeah, I was just trying to do some testing. I'm getting an error where it says ObstacleAttributes must be of type System.type.
Right, you can't attach existing objects as new components. What you should do is this: Code (csharp): var attributes = test.AddComponent<ObstacleAttributes>(); attributes.obj = attrib.obj; attributes.origin_lat = attrib.origin_lat; ....
I had this working quite well with just primitive objects (I tried with a sphere), but when I try to apply the same code to a bundle I've constructed from a model with a script and collider attached, I get this error: The AssetBundle could not be loaded because it references scripts that are not compatible with the currently loaded ones. Rebuild the AssetBundle to fix this error. Do I need to do something special to make models with scripts work with bundling?