Revision Difference
Custom_Asset_Types#545862
<cat>Code.Misc</cat>
<title>Custom Asset Types</title>
Custom assets are things you can define yourself. They give you a nice inspector window and they're hotloaded in-game, which means you can whip things up pretty quickly if you're using them.
You can find plenty of examples of assets throughout s&box, here's a snippet from the clothing asset:
```
[Library( "clothing" ), AutoGenerate]
public partial class Clothing : Asset
{
[Property]
public string Title { get; set; }
[Property, ResourceType( "vmdl" )]
public string Model { get; set; }
// ...
}
```
* `[Library( "clothing" )]` specifies the file extension our assets use will be .clothing
* Because we specified `[AutoGenerate]` on our asset class a .asset file will automatically generate for us.
* Properties that should be editable via the inspector in your asset should be marked with `[Property]`
⤶
⤶
## Using the inspector
⤶
<note>If your Property isn't supported by `[AutoGenerate]`, use `[Property(FGDType = "insert_type_here")]` until it is supported.</note>⤶
⤶
## Using the inspector
Now that you have everything set up, you can use the inspector tool to edit your first weapon.
1. Click the "Inspector" icon in the asset browser.
<upload src="44a6e/8d941a81d2276f1.png" size="36799" name="image.png" />
2. Pick your asset type.
<upload src="44a6e/8d941a921ee5812.png" size="15315" name="image.png" />
3. Set up your weapon however you want.
<upload src="44a6e/8d941a8a3d0efee.png" size="9916" name="image.png" />
4. Save your asset into your addon's config directory - call it "MyWeapon.weapon"
## Accessing assets
All assets are loaded when you first start the game, there are several ways you can access them:
### FromPath
When assets are loaded they are stored in a dictionary with their path, you can access these with `Resource.FromPath<T>`.
```
// Property allows for hot-loading
public Clothing Clothing { get; set; }
// ...
// Load the weapon data from a path
Clothing = Resource.FromPath<Clothing>( "config/tshirt.clothing" );
```
### PostLoad
When assets are loaded they call their PostLoad method, you can use this to store a list of your assets for later use.
```csharp
public partial class Clothing : Asset
{
// Access these statically with Clothing.All
public static IReadOnlyList<Clothing> All => _all;
internal static List<Clothing> _all = new();
protected override void PostLoad()
{
base.PostLoad();
if ( !_all.Contains( this ) )
_all.Add( this );
}
}
```