API Assets - shmellyorc/Box GitHub Wiki
Assets
Namespace: Box.Graphics
Description: Represents a class that manages assets within the application, including loading, retrieving, and organizing various asset types such as textures (surfaces), sounds, fonts, maps, spritesheets, and asset packs. It provides methods to load assets from the file system or from packed files, add them to an internal collection, and retrieve them by name or enum identifier. Additionally, it tracks total asset count and memory usage.
Supported Asset File Types
Asset Type | Loading Method(s) | Supported Extensions |
---|---|---|
Surface | LoadSurface , AddSurface |
.bmp, .png, .tga, .jpg, .gif, .psd, .hdr, .pic, .pnm |
SubSurface | LoadSubSurface , AddSubSurface |
.bmp, .png, .tga, .jpg, .gif, .psd, .hdr, .pic, .pnm |
Sound | LoadSound , AddSound |
.wav, .ogg, .flac, .mp3 |
BitmapFont | LoadBitmapFont , AddBitmapFont |
.fnt |
Map | LoadMap , AddMap |
.json, .ldtk |
Spritesheet | LoadSpriteSheet , AddSheet |
.json, .sheet |
Asset Pack | LoadPack |
.pack |
GenericFont | LoadFont , AddFont |
.ttf, .pfa, .pfb, .cff, .otf, .pcf, .bdf, .pfr |
Constructors
This class has no public constructors.
Properties
Property | Type | Description |
---|---|---|
Instance |
Assets | Gets the singleton instance of the Assets manager. |
Count |
int |
Gets the number of assets currently managed. |
Bytes |
long |
Gets the total size of all assets in bytes. |
Methods
Method Signature | Description |
---|---|
bool Exists(string name) |
Checks if an asset with the specified name exists in the collection. |
bool Exists(Enum name) |
Checks if an asset with the specified enum value exists. |
void Add(string name, IAsset asset) |
Adds the given asset to the collection under the specified name. |
void Add(Enum name, IAsset asset) |
Adds the given asset under the specified enum key. |
T Get<T>(string name) where T : IAsset |
Retrieves and initializes the asset of type T by name, or throws if not found or wrong type. |
T Get<T>(Enum name) where T : IAsset |
Retrieves and initializes the asset of type T by enum key. |
bool TryGet<T>(string name, out T asset) where T : IAsset |
Attempts to retrieve and initialize the asset by name; returns false if not found. |
bool TryGet<T>(Enum name, out T asset) where T : IAsset |
Attempts to retrieve and initialize the asset by enum key; returns false if not found. |
T GetFromFile<T>(string filename) where T : IAsset |
Retrieves an asset of type T by scanning loaded assets for matching filename. |
T GetFromPack<T>(string filename) where T : IAsset |
Retrieves an asset of type T from a packed asset file by filename key. |
void LoadPack(string filename) |
Loads an asset pack (*.pack) and overrides content loading to use packed assets. |
bool Remove(string name) |
Removes and disposes the asset with the specified name; returns whether removal succeeded. |
bool Remove(Enum name) |
Removes by enum key. |
void Clear() |
Clears and disposes all assets in the collection. |
static Surface GetSurfaceFromTileset(Map map, int id) |
Retrieves a Surface from a Map's tileset by its unique ID. |
static Surface GetSurfaceFromTileset(MapTileset tileset) |
Retrieves a Surface corresponding to the given MapTileset entry. |
Surface LoadSurface(string filename, bool repeat = false, bool smooth = false) |
Loads a texture asset (Surface) from file or pack, with optional repeat and smoothing flags. |
static Surface GetSurface(string name) |
Retrieves a loaded Surface by name. |
static Surface GetSurface(Enum name) |
Retrieves a loaded Surface by enum name. |
void AddSurface(string name, string filename, bool repeat = false, bool smooth = false) |
Loads and adds a Surface asset under the given name. |
void AddSurface(Enum name, string filename, bool repeat = false, bool smooth = false) |
Loads and adds a Surface by enum key. |
Fonts.BitmapFont LoadBitmapFont(string filename, int spacing = 0, int lineSpacing = 0) |
Loads a bitmap font (*.fnt) with optional spacing parameters. |
void AddBitmapFont(string name, string filename, int spacing = 0, int lineSpacing = 0) |
Loads and adds a bitmap font under the given name. |
void AddBitmapFont(Enum name, string filename, int spacing = 0, int lineSpacing = 0) |
Loads and adds a bitmap font by enum key. |
Surface LoadSubSurface(string filename, Rect2 region, bool repeat = false, bool smooth = false) |
Loads a rectangular region from an image file as a new Surface. |
Surface LoadSubSurface(Surface surface, Spritesheet sheet, string name, bool repeat = false, bool smooth = false) |
Extracts and returns a subsurface region from an existing Surface using a Spritesheet definition. |
void AddSubSurface(string name, string filename, Rect2 region, bool repeat = false, bool smooth = false) |
Loads and adds a subsurface under the given name. |
void AddSubSurface(Enum name, string filename, Rect2 region, bool repeat = false, bool smooth = false) |
Loads and adds a subsurface by enum key. |
void AddSubSurface(string name, Surface surface, Spritesheet sheet, string sheetName, bool repeat = false, bool smooth = false) |
Adds a subsurface from an existing Surface and Spritesheet under the given name. |
void AddSubSurface(Enum name, Surface surface, Spritesheet sheet, string sheetName, bool repeat = false, bool smooth = false) |
Adds a subsurface by enum key. |
Sound LoadSound(string filename, bool looped = false) |
Loads a sound asset from file or pack, with optional looping. |
static Sound GetSound(string name) |
Retrieves a loaded Sound by name. |
static Sound GetSound(Enum name) |
Retrieves a loaded Sound by enum name. |
void AddSound(string name, string filename, bool looped = false) |
Loads and adds a Sound under the given name. |
void AddSound(Enum name, string filename, bool looped = false) |
Loads and adds a Sound by enum key. |
Map LoadMap(string filename) |
Loads a map asset (*.ldtk) from file or pack. |
static Map GetMap(string name) |
Retrieves a loaded Map by name. |
static Map GetMap(Enum name) |
Retrieves a loaded Map by enum name. |
void AddMap(string name, string filename) |
Loads and adds a Map under the given name. |
void AddMap(Enum name, string filename) |
Loads and adds a Map by enum key. |
GenericFont LoadFont(string filename, int size, bool useSmoothing = false, bool bold = false, int thickness = 0, int spacing = 0, int lineSpacing = 0) |
Loads a generic font with customization options. |
static BoxFont GetFont(string name) |
Retrieves a loaded GenericFont as BoxFont by name. |
static BoxFont GetFont(Enum name) |
Retrieves by enum name. |
void AddFont(string name, string filename, int size, bool useSmoothing = false, bool bold = false, int thickness = 0, int spacing = 0, int lineSpacing = 0) |
Loads and adds a GenericFont under the given name. |
void AddFont(Enum name, string filename, int size, bool useSmoothing = false, bool bold = false, int thickness = 0, int spacing = 0, int lineSpacing = 0) |
Loads and adds a GenericFont by enum key. |
Spritesheet LoadSpriteSheet(string filename) |
Loads a spritesheet asset (*.sheet). |
static Spritesheet GetSheet(string name) |
Retrieves a loaded Spritesheet by name. |
static Spritesheet GetSheet(Enum name) |
Retrieves a Spritesheet by enum name. |
void AddSheet(string name, string filename) |
Loads and adds a Spritesheet under the given name. |
void AddSheet(Enum name, string filename) |
Loads and adds a Spritesheet by enum key. |
Examples
// Load and register a texture
var playerSurface = Assets.Instance.LoadSurface("player.png", repeat: false, smooth: true);
Assets.Instance.Add("PlayerTexture", playerSurface);
// Retrieve by name or enum
var tex1 = Assets.GetSurface("PlayerTexture");
var tex2 = Assets.GetSurface(GameAssets.PlayerTexture);
// Load and play a sound
var sfx = Assets.Instance.LoadSound("explosion.wav");
Assets.Instance.Add("ExplosionSfx", sfx);
SoundManager.Instance.Play(sfx);