• Hi Guest!

    We are extremely excited to announce the release of our first Beta1.1 and the first release of our Public AddonKit!
    To participate in the Beta, a subscription to the Entertainer or Creator Tier is required. For access to the Public AddonKit you must be a Creator tier member. Once subscribed, download instructions can be found here.

    Click here for information and guides regarding the VaM2 beta. Join our Discord server for more announcements and community discussion about VaM2.
  • Hi Guest!

    VaM2 Resource Categories have now been added to the Hub! For information on posting VaM2 resources and details about VaM2 related changes to our Community Forums, please see our official announcement here.

Loading an Asset

fabio

Well-known member
Joined
Jan 30, 2021
Messages
78
Reactions
287
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();
            }
        }
    }
 
@fabio Please don't forget to clean up the AssetBundle when your plugin is destroyed.
 
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