Description:
Loads an image file from the mod's assets and returns it as a Texture2D.

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

Added In: v0.50

Function

public static Texture2D LoadTexture2D(string file, bool shared = false)

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

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



Usage

Texture2D myTexture1 = UMFAsset.LoadTexture2D("MyTexture.png");
//Loads from \Mods\Assets\MyModName\MyTexture.png or from packed Assets\MyModName\MyTexture.png

Texture2D myTexture2 = UMFAsset.LoadTexture2D("MyTexture.png", true);
//Loads from \Mods\Assets\Shared\MyTexture.png

Texture2D myTexture3 = UMFAsset.LoadTexture2D(Path.Combine("MySubFolder", "MyTexture.png"));
//Loads from \Mods\Assets\MyModName\MySubFolder\MyTexture.png or from packed Assets\MyModName\MySubFolder\MyTexture.png



Examples

using UModFramework.API;

namespace MyModName
{
    [UMFScript]
    class MyModName : MonoBehaviour
    {
        private const string myTextureFile = "MyTexture.png";
        internal static Texture2D myTexture = null;

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

        void Awake()
        {
            myTexture = UMFAsset.LoadTexture2D(myTextureFile);
            if (myTexture == null)
            {
                Log("Error: Failed to load the texture.");
                //Do something about the error
            }
            //At this point you can use MyModName.myTexture anywhere in your code to replace or set the Texture2D of something.
        }
    }
}