User Tools

Site Tools


api:umfasset:loadaudioclip

Description:
Loads a audio file from the mod's assets and returns it as a AudioClip.

Note: Flat file/on disk assets always overwrite packed assets.

Added In: v0.50

Function

public static AudioClip LoadAudioClip(string file, bool shared = false)

//file: File in mod's Asset folder. Should not include Assets\MyModName in file.
//Packed Mods: Assets\MyModName\MyAudio.ogg
//Unpacked Mods: ...\Mods\Assets\MyModName\MyAudio.ogg

//shared set to true: Get the asset from \Mods\Assets\Shared\ instead.



Usage

AudioClip myAudioClip1 = UMFAsset.LoadAudioClip("MyAudio.ogg");
//Loads from \Mods\Assets\MyModName\MyAudio.ogg or from packed Assets\MyModName\MyAudio.ogg

AudioClip myAudioClip2 = UMFAsset.LoadAudioClip("MyAudio.ogg", true);
//Loads from \Mods\Assets\Shared\MyAudio.ogg

AudioClip myAudioClip3 = UMFAsset.LoadAudioClip(Path.Combine("MySubFolder", "MyAudio.ogg"));
//Loads from \Mods\Assets\MyModName\MySubFolder\MyAudio.ogg or from packed Assets\MyModName\MySubFolder\MyAudio.ogg



Examples

using UModFramework.API;

namespace MyModName
{
    [UMFScript]
    class MyModName : MonoBehaviour
    {
        private const string myAudioFile= "MyAudio.ogg";
        internal static AudioClip myAudioClip = null;

        internal static void Log(string text, bool clean = false)
        {
            using (UMFLog log = new UMFLog()) log.Log(text, clean);
        }

        void Awake()
        {
            myAudioClip = UMFAsset.LoadAudioClip(myAudioFile);
            if (myAudioClip == null)
            {
                Log("Error: Failed to load the audio clip.");
                //Do something about the error
            }
            //At this point you can use MyModName.myAudioClip anywhere in your code to replace or set the AudioClip of a GameObject.
        }
    }
}


This topic does not exist yet

You've followed a link to a topic that doesn't exist yet. If permissions allow, you may create it by clicking on Create this page.

api/umfasset/loadaudioclip.txt · Last modified: 2019/06/28 00:32 by umfdev