-
Notifications
You must be signed in to change notification settings - Fork 26
Assets
Mira API provides a flexible asset loading system under MiraAPI.Utilities.Assets. The core abstraction is LoadableAsset<T>, with several concrete implementations for loading assets from different sources.
LoadableAsset<T> is the abstract base class for all loadable assets. It defines a single method, LoadAsset(), and provides a LoadedAsset property for caching.
public abstract class LoadableAsset<T> where T : UnityEngine.Object
{
protected T? LoadedAsset { get; set; }
public abstract T LoadAsset();
}All implementations cache the loaded asset after the first call to LoadAsset(), so subsequent calls are cheap.
Loads a Sprite from an image file embedded as a resource in your plugin's DLL.
public class LoadableResourceAsset(string path, float pixelsPerUnit = 100f) : LoadableAsset<Sprite>| Parameter | Description |
|---|---|
path |
The fully qualified embedded resource path (e.g. "MyPlugin.Resources.icon.png"). |
pixelsPerUnit |
Pixels per unit for the created sprite. Defaults to 100. |
The calling assembly is captured at construction time, so this must be declared in your plugin's own assembly (not a helper library).
public static readonly LoadableResourceAsset MyIcon = new("MyPlugin.Resources.icon.png");
Sprite sprite = MyIcon.LoadAsset();To embed an image as a resource, set the file's Build Action to Embedded Resource in your .csproj:
<ItemGroup>
<EmbeddedResource Include="Resources\icon.png" />
</ItemGroup>Loads an AudioClip from a .wav file embedded as a resource in your plugin's DLL. Only PCM WAV format is supported (8-bit, 16-bit, 24-bit, and 32-bit).
public class LoadableAudioResourceAsset(string path) : LoadableAsset<AudioClip>| Parameter | Description |
|---|---|
path |
The fully qualified embedded resource path (e.g. "MyPlugin.Resources.sound.wav"). |
Like LoadableResourceAsset, the calling assembly is captured at construction time.
public static readonly LoadableAudioResourceAsset MySound = new("MyPlugin.Resources.sound.wav");
AudioClip clip = MySound.LoadAsset();Loads an asset of any type from an already-loaded AssetBundle.
public class LoadableBundleAsset<T>(string name, AssetBundle bundle) : LoadableAsset<T>
where T : UnityEngine.Object| Parameter | Description |
|---|---|
name |
The name of the asset inside the bundle. |
bundle |
The AssetBundle instance to load from. |
Throws InvalidOperationException if the named asset is not found in the bundle.
AssetBundle myBundle = AssetBundle.LoadFromFile("mybundle");
public static readonly LoadableBundleAsset<Sprite> MySprite = new("my_sprite", myBundle);
public static readonly LoadableBundleAsset<GameObject> MyPrefab = new("my_prefab", myBundle);
Sprite sprite = MySprite.LoadAsset();Loads a single asset from the game's Addressables system by GUID. Loading is synchronous via WaitForCompletion. Includes a workaround for a GC issue in Il2CppInterop (#40).
public class LoadableAddressableAsset<T>(string uid) : LoadableAsset<T>
where T : UnityEngine.Object| Parameter | Description |
|---|---|
uid |
The GUID of the addressable asset. |
garbageCollection (optional) |
A callback that receives the instance after loading. Use with GCHandle.Alloc to prevent garbage collection of the loaded asset. |
Throws InvalidOperationException if the asset fails to load.
public static readonly LoadableAddressableAsset<Sprite> MyAddressable =
new("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
Sprite sprite = MyAddressable.LoadAsset();With the GC workaround:
public static readonly LoadableAddressableAsset<Sprite> MyAddressable = new(
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
asset => GCHandle.Alloc(asset.LoadedAsset));Loads a collection of assets from the game's Addressables system by a shared key. Returns a ReadOnlyCollection<T>. Includes the same GC workaround as LoadableAddressableAsset<T>.
public class LoadableAddressableAssets<T>(string key) where T : UnityEngine.ObjectNote: this class does not extend LoadableAsset<T> because it returns a collection rather than a single asset.
| Parameter | Description |
|---|---|
key |
The addressable key shared by the group of assets. |
garbageCollection (optional) |
A callback for GC workaround, same as LoadableAddressableAsset<T>. |
var myAssets = new LoadableAddressableAssets<Sprite>("my_sprite_group");
ReadOnlyCollection<Sprite> sprites = myAssets.LoadAssets();Wraps an existing UnityEngine.Object so it can be used wherever a LoadableAsset<T> is expected. Calls DontDestroy on the asset so it persists across scene loads.
public class LoadableAssetWrapper<T>(T asset) : LoadableAsset<T>
where T : UnityEngine.ObjectSprite existingSprite = /* ... */;
var wrapped = new LoadableAssetWrapper<Sprite>(existingSprite);
Sprite sprite = wrapped.LoadAsset();Like LoadableAssetWrapper<T>, but without the DontDestroy call. Use this when the lifetime of the asset is managed elsewhere.
public class PreloadedAsset<T>(T asset) : LoadableAsset<T>
where T : UnityEngine.Objectvar preloaded = new PreloadedAsset<AudioClip>(someClip);
AudioClip clip = preloaded.LoadAsset();SpriteTools provides low-level helpers for loading sprites directly from embedded resource streams. LoadableResourceAsset uses these internally.
| Method | Description |
|---|---|
Texture2D LoadTextureFromResourcePath(string resourcePath, Assembly assembly) |
Loads a Texture2D from an embedded resource in the given assembly. Throws ArgumentException if the resource is not found. |
Sprite LoadSpriteFromPath(string resourcePath, Assembly assembly, float pixelsPerUnit) |
Loads a Sprite from an embedded resource. Convenience wrapper over LoadTextureFromResourcePath. |
MiraAssets is a static class that exposes the built-in assets used by Mira API itself. These are available for use in custom UI or anywhere you need a standard Mira sprite.
| Property | Type | Description |
|---|---|---|
AcceptedTeal |
Color32 |
The teal accent color used throughout Mira's UI (#2BE9C6). |
MiraAssetBundle |
AssetBundle |
The Mira internal asset bundle (mirabundle). |
ModifierDisplay |
LoadableAsset<GameObject> |
Prefab for the modifier display UI element. |
PresetSavePopup |
LoadableAsset<GameObject> |
Prefab for the preset save dialog. |
RefreshIcon |
LoadableAsset<Sprite> |
Refresh/reload icon sprite. |
FolderIcon |
LoadableAsset<Sprite> |
Folder icon sprite (used in freeplay). |
Empty |
LoadableResourceAsset |
A transparent 1×1 empty sprite. |
RoundedBox |
LoadableAsset<Sprite> |
Sliced rounded rectangle sprite for UI backgrounds. |
NextButton |
LoadableResourceAsset |
Next/forward button sprite. |
NextButtonActive |
LoadableResourceAsset |
Highlighted next button sprite. |
SettingsIcon |
LoadableResourceAsset |
Settings cog icon. |
Cog |
LoadableResourceAsset |
Cog icon used in the role settings menu. |
CheckmarkBox |
LoadableResourceAsset |
Checkbox background sprite. |
ResetButton |
LoadableResourceAsset |
Reset button sprite. |
Checkmark |
LoadableResourceAsset |
Checkmark tick sprite. |
CategoryHeader |
LoadableResourceAsset |
White category header background sprite. |
KeybindButton |
LoadableResourceAsset |
Background sprite for keybind indicator icons. |
CrewmateFile |
LoadableResourceAsset |
Crewmate role file sprite (used in freeplay TaskAdderGame). |
ImpostorFile |
LoadableResourceAsset |
Impostor role file sprite (used in freeplay TaskAdderGame). |
CustomTeamFile |
LoadableResourceAsset |
Custom team role file sprite (used in freeplay TaskAdderGame). |
ModifierFile |
LoadableResourceAsset |
Modifier file sprite (used in freeplay TaskAdderGame). |
TimedModifierFile |
LoadableResourceAsset |
Timed modifier file sprite (used in freeplay TaskAdderGame). |