0
0
Unityframework~20 mins

Asset bundles and optimization in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Asset Bundle Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Unity C# code snippet?
Consider this code that loads an asset bundle asynchronously and prints the asset names. What will be printed in the console?
Unity
using UnityEngine;
using System.Collections;

public class LoadBundle : MonoBehaviour
{
    IEnumerator Start()
    {
        var bundleLoadRequest = AssetBundle.LoadFromFileAsync("Assets/Bundles/mybundle");
        yield return bundleLoadRequest;
        AssetBundle bundle = bundleLoadRequest.assetBundle;
        if (bundle == null)
        {
            Debug.Log("Failed to load bundle");
            yield break;
        }
        string[] assetNames = bundle.GetAllAssetNames();
        foreach (var name in assetNames)
        {
            Debug.Log(name);
        }
        bundle.Unload(false);
    }
}
APrints all asset names inside the bundle, one per line
BPrints "Failed to load bundle" because LoadFromFileAsync does not exist
CPrints nothing because assetNames is always empty
DThrows a runtime error because bundle.Unload(false) is called too early
Attempts:
2 left
💡 Hint
Think about what LoadFromFileAsync returns and what GetAllAssetNames() does.
🧠 Conceptual
intermediate
1:30remaining
Which optimization technique reduces memory usage when loading asset bundles?
You want to optimize your Unity game to use less memory when loading asset bundles. Which technique helps achieve this?
ALoad all asset bundles at game start and keep them loaded
BAvoid using asset bundles and load assets directly from Resources folder
CLoad asset bundles synchronously to avoid overhead
DUse LoadFromFileAsync and unload bundles with Unload(false) after use
Attempts:
2 left
💡 Hint
Think about asynchronous loading and unloading bundles after use.
🔧 Debug
advanced
2:30remaining
Why does this asset bundle loading code cause a NullReferenceException?
Examine the code below. It sometimes throws a NullReferenceException at runtime. What is the cause?
Unity
using UnityEngine;
using System.Collections;

public class BundleLoader : MonoBehaviour
{
    IEnumerator Start()
    {
        AssetBundle bundle = AssetBundle.LoadFromFile("Assets/Bundles/characters");
        string assetName = "hero";
        var asset = bundle.LoadAsset<GameObject>(assetName);
        Instantiate(asset);
        bundle.Unload(false);
        yield return null;
    }
}
ACalling bundle.Unload(false) before Instantiate unloads the asset causing null reference
BLoadFromFile returns null if the bundle path is wrong, causing bundle to be null
CThe coroutine must yield before calling LoadFromFile to avoid null reference
DLoadAsset<GameObject> returns null if assetName is not found, causing Instantiate to fail
Attempts:
2 left
💡 Hint
Check if the bundle variable could be null before using it.
📝 Syntax
advanced
2:00remaining
Which option correctly loads an asset bundle asynchronously and accesses an asset?
Choose the code snippet that correctly loads an asset bundle asynchronously and then loads an asset named "tree" from it.
A
var request = AssetBundle.LoadFromFileAsync("path");
yield return request;
var bundle = request.assetBundle;
var asset = bundle.LoadAsset&lt;GameObject&gt;("tree");
B
var request = AssetBundle.LoadFromFileAsync("path");
var bundle = request.assetBundle;
var asset = bundle.LoadAsset&lt;GameObject&gt;("tree");
C
var bundle = AssetBundle.LoadFromFileAsync("path").assetBundle;
var asset = bundle.LoadAsset&lt;GameObject&gt;("tree");
D
var bundle = AssetBundle.LoadFromFile("path");
var asset = bundle.LoadAssetAsync&lt;GameObject&gt;("tree");
Attempts:
2 left
💡 Hint
Remember that asynchronous loading requires yielding until done.
🚀 Application
expert
3:00remaining
How many asset bundles are loaded in memory after this code runs?
Given this Unity C# code, how many asset bundles remain loaded in memory after execution?
Unity
using UnityEngine;
using System.Collections;

public class BundleTest : MonoBehaviour
{
    IEnumerator Start()
    {
        var bundleA = AssetBundle.LoadFromFile("Assets/Bundles/bundleA");
        var bundleB = AssetBundle.LoadFromFile("Assets/Bundles/bundleB");
        var assetA = bundleA.LoadAsset<GameObject>("assetA");
        var assetB = bundleB.LoadAsset<GameObject>("assetB");
        bundleA.Unload(false);
        yield return null;
        // No unload called on bundleB
    }
}
A2 bundles loaded
B0 bundles loaded
C1 bundle loaded
DCannot determine without more info
Attempts:
2 left
💡 Hint
Consider what Unload(false) does and which bundles are unloaded.