- Home /
Multi-resolution assets using Unity5.0 Assetbundles
Hello,
following this discussion on the Unity forums, I decided to dig around and see how the new assetbundles works. I found a video from Unite2014 about it where they did exactly what was described in that thread about having different assets for different builds. Luckily the person who held the presentation also uploaded the source that he used to demo the new Assetbundle features.
Using the project as a template I was able to create a SD and HD version of a sample sprite. However when Implementing the code to my own project I wasn't able to do so. The scripts I used are the same as in the assetbundle demo mentioned above. Bellow I have screenshots of my folders and assetbunde labels:
As the folder names suggest, they have the label suffix of .hd, sd and ud respectively.
Shows the labels.
The scene used in this is an empty scene with a canvas and a panel, the panel uses the BG sprite in its Image component.
Bellow is the loader script I'm using:
public class LoadVariants : BaseLoader {
public string variantSceneAssetBundle = "xplatform/assetbundletest.unity3d";
public string variantSceneName = "assetbundletest";
public string[] activeVariants = {"hd"};
// Use this for initialization
IEnumerator Start () {
yield return StartCoroutine(Initialize() );
// Set active variants.
AssetBundleManager.Variants = activeVariants;
// Load variant level which depends on variants.
yield return StartCoroutine(LoadLevel (variantSceneAssetBundle, variantSceneName, true) );
// Unload assetBundles.
AssetBundleManager.UnloadAssetBundle(variantSceneAssetBundle);
}
}
Which is exactly the same as in the demo, but with the label strings changed. All the other bundle building code, loading code, etc is the same as in the demo.
Did I miss a vital piece of setup ?
Thanks in advance for your time!
Are you in simulation mode? Also, show us screenshots of where you apply the asset bundle labels. I just want to see exactly how you named them
Hey $$anonymous$$ike,
I'm not using simulation, below is a screenshot of it. I have added a imgur gallery with the images you requested as I couldn't upload images to this answer for some reason.
Thanks for the quick respone :).
Your code is loading a scene, not your asset variants. Are you sure you want to call LoadLevel?
Yes, I am loading a level. But I'm also specifying the asset variant to either sd,hd or ud. From what I underestood, it also loads the sprites assetsbundle, because of dependency. Here is an console log of what I mean.
I am assu$$anonymous$$g the SD items get loaded into your scene?
Answer by MarimoAli · Mar 19, 2015 at 10:53 PM
Alright Mike, as I have mentioned before, it works when using the demo provided by your colleague from Unite2014. He built the assetbundles the same way I did, in addition he has a couple more elements in the scene, where I have only a sprite.
Then he has a empty scene with a GameObject that has a this loaderscript in it:
public class LoadVariants : BaseLoader {
public string variantSceneAssetBundle = "variants/variant-scene.unity3d";
public string variantSceneName = "variant-scene";
public string[] activeVariants = {"hd"};
// Use this for initialization
IEnumerator Start () {
yield return StartCoroutine(Initialize() );
// Set active variants.
AssetBundleManager.Variants = activeVariants;
// Load variant level which depends on variants.
yield return StartCoroutine(LoadLevel (variantSceneAssetBundle, variantSceneName, true) );
// Unload assetBundles.
AssetBundleManager.UnloadAssetBundle(variantSceneAssetBundle);
}
}
// Set active variants. AssetBundleManager.Variants = activeVariants;
Sets the variant I would like to use, and in my previous the RemapVariantName func find the closest name to it and uses(loads) it, since they are xplatform/ sprites .sd/ud and hd, they all share the same id and can be read from the manifest. If I understood the video correctly. Wasn't this essentially the biggest update of the 5.0 assetbundles?
When editing the assetbundles demo by adding the ud version sprites and rebuilding the assets, it works. By changing in the inspector to sd, hd or ud. It changes which assets are to be loaded. However when trying to do the exact same in my project it doesn't load them. Just to clarify, the demo came with SD and HD examples and I added a UD assets folder and rebuilt the assetsbundle, as can be seen here.
I just loaded up the demo project you were talking about and the variant loading on it doesn't appear to work correctly. Using the variant loader scene, if I load HD assets (which are already in that scene) it works fine. If I try to load the SD I get blank text and a cube without a texture (same problem you are having). It is possible that the api for asset bundles changed slightly since this demo was released. As it stands, it doesn't appear to work in the latest version of Unity 5.
I'm not certain why it isnt working for you correctly, but the demo works very well for me. Are you in simulation mode/ have you built the assetbunles :D? I couldn't find any differences/changes in the API from the scripts in the demo.
Whats really confusing is the fact, that when printing logs, it shows that its loading the scene assent bundle and the dependency assetbundles, that have the sprite. I guess, Ill have to look more into assetbundles.
Right, it is loading the sprite asset bundle, but you have no code to actually put it in the scene (which is why it doesn't appear). The same thing is happening in the demo project. Unless I am missing something with the new system, just because assets are loaded doesn't mean they are actually used in a scene.
As for your other question: I have built the asset bundles with the demo and tested in both simulation and non modes.
I was having an issue with the demo project not switching to sd even though I changed the 'Active Variants' setting in the Inspector of the VariantLoader scene. I got it to work properly by unchecking the menu option "AssetBundles->Simulate AssetBundles" (it's checked on by default). After unchecking that option I can enter 'hd' or 'sd' in the variant element to get it to load the hd/sd content.
Yes, it turns out I was using an older version of the project. The new one works fine in Unity 5 release. After viewing the user's project, my assumption is that there is a bug that needed to be reported.