S&box Wiki

Revision Difference

Custom_Asset_Types#546844

<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: ``` [GameResource("Clothing Definition", "clothing" )] public partial class Clothing : GameResource { public string Title { get; set; } [ResourceType( "vmdl" )] public string Model { get; set; } [HideInEditor] public int Amount { get; set; } // ... } ``` * `[GameResource("Clothing Definition", "clothing" )]` specifies the file extension our assets use will be .clothing * Properties that should not be editable via the inspector in your asset should be marked with `[HideInEditor]` * You can mark properties with specific categories using `[Category("Example Category")]` ⤶ <note>If your Property type isn't automatically converted to a valid FGD type, use `[FGDType( "insert_type_here" )]` until it is supported.</note>⤶ ⤶ ### How to use [ResourceType] ⤶ ⤶ ### How to use [ResourceType] ResourceType supports specifying the file extension of a resource you want to use, which will add a navigator to the inspector. Examples: * `[ResourceType( "png" )]` * `[ResourceType( "vmdl" )]`. In addition, it also supports asset files, including custom ones. e.g. `[ResourceType( "sound" )]` or `[ResourceType( "clothing" )]`. ## Using the inspector Now that you have everything set up, you can use the inspector tool to edit your first weapon. 1. Go to the "Assets Browser" tab in the s&box editor. 2. Right click a folder in your addon and click "Add X Definition". Typically you'd want to put these in some sort of `data` folder. <upload src="aa125/8da3ac2bed7fc93.png" size="26483" name="image.png" /> 3. Enter a name for your asset <upload src="aa125/8da3ac1fd392295.png" size="6257" name="image.png" /> 4. Go to the "Inspector" tab and set your asset up how you want it. 5. Click the save button (looks like a floppy disk) to save your asset. ## 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 `Asset.FromPath<T>`. ``` // Property allows for hot-loading public Clothing Clothing { get; set; } // ... // Load the weapon data from a path Clothing = ResourceLibrary.Get<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 : GameResource { // 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 ); } } ```