Loading an Asset

fabio

Well-known member
Messages
70
Reactions
263
Points
53
I've been following MacGruber's guide
Unity AssetBundles for VaM

and I have created an AssetBundle that contains a custom .otf font. Now I want to load the AssetBundle back but I can't.

Code:
using UnityEngine;

    public override void Init()
    {
        // https://docs.unity3d.com/ScriptReference/AssetBundle.LoadFromFile.html
        var myLoadedAssetBundle = AssetBundle.LoadFromFile(assetbundle_path);
    }

Error log
"The name `AssetBundle' does not exist in the current context"

I can't figure out what's wrong. Am I missing a namespace?
 
Various file access methods are blocked in VaM for security. However, VaM has a mechanism to load AssetBundles that you can use, these have safeties buildin so that you can't load/save outside the VaM directory. I recommend to check out other plugins how they load stuff. For example my SuperShot plugin (part of Essentials) loads a shader. Or you could check out RandomSoundFromAB from LogicBricks, which loads sounds in a similar way, but also uses the file open dialog so the user can select an AssetBundle rather than a hardcoded path.
 
That makes sense, thanks. You know, your guide saved me a lot of time. It's funny that I overlooked the example provided by Essentials, as I've studied it (along with others) when I was looking around about scripts. I don't know how long it would have taken me to find an example without your help.

Code:
    public override void Init()
    {
        Request request = new AssetLoader.AssetBundleFromFileRequest {path = assetbundle_path, callback = OnAssetBundleLoaded};
        AssetLoader.QueueLoadAssetBundleFromFile(request);
    }

    private void OnAssetBundleLoaded(Request aRequest)
    {
        if (aRequest.assetBundle != null)
        {
            Font myFont = aRequest.assetBundle.LoadAsset<Font>(font_name);

            if (myFont != null)
            {
                text_2.font = myFont;
                renderText = true;
                UpdateStatus();
            }
        }
    }
 
Updated code with clean up & namespaces

Code:
using UnityEngine;
using MeshVR;
using Request = MeshVR.AssetLoader.AssetBundleFromFileRequest;

    public override void Init()
    {
        Request request = new AssetLoader.AssetBundleFromFileRequest {path = assetbundle_path, callback = OnAssetBundleLoaded};
        AssetLoader.QueueLoadAssetBundleFromFile(request);
    }

    private void OnAssetBundleLoaded(Request aRequest)
    {
        if (aRequest.assetBundle != null)
        {
            Font myFont = aRequest.assetBundle.LoadAsset<Font>(font_name);

            if (myFont != null)
            {
                text_1.font = myFont;
                renderText = true;
                UpdateStatus();
            }
        }
    }

    public void OnDestroy()
    {
        if (assetbundle_path != null)
        {
            AssetLoader.DoneWithAssetBundleFromFile(assetbundle_path);
            assetbundle_path = null;
        }
    }
 
Back
Top Bottom