S&box Wiki
Home
/
Edit Custom Asset Types
View
Edit
History
No Category
Developer Overview
The Project System
Publishing To Asset Party
Getting Started With Hammer
Mapping Basics
Mapping Entities
Advanced Mapping Techniques
Getting Started with Modeldoc
Animgraph & Animation
Physics
Modeldoc Nodes
Advanced Modelling
UI Basics
Styles & Stylesheets
Razor Templates
Game Menus
Materials
Built In Shaders
Shaders
Shader Reference
Sounds & Audio
Particles
Getting Started
Making Games
Input
Networking
Physics
Rendering
Editor & Tools
VR
Misc
Playing Guides
Console Commands & Variables
Dedicated Server
Log in to edit
Custom Asset Types
<cat>Code.Editor</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", "Describes an item of clothing and implicitly which other items it can be worn with.")] public partial class Clothing : GameResource { public string Title { get; set; } [ResourceType( "vmdl" )] public string Model { get; set; } [Hide] public int Amount { get; set; } [JsonIgnore] public int MySecretNumber => 10; // ... } ``` It is important to note, you should ensure that your filetype is all lowercase, otherwise it will fail to register. * `[GameResource("Clothing Definition", "clothing", "..." )]` defines the title (`Clothing Definition`), file extension (`.clothing`), and a description of the asset type * You should ensure that your **filetype is all lowercase**, otherwise it will fail to register/save. * Note that these are just JSON files with pretty faces and file types easy for the game to locate and developers to work with ##Attributes S&Box utilizes an attribute system to make custom assets easier to manipulate. Any of these attributes can be used before the properties declaration that you want it to affect (See the above example.) For a full list, use the API reference ###[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" )]`. ###[Category()] Allows properties to be grouped in with properties of similar interest in a collapsible block in the editor. Say we have a GameResource for custom props we can do this: `[Category("Prop Info")] public string Name {get; set;}` ###[Hide] Used for backend properties that should not be edited in the editor. ###[Description()] Used to provide a brief description of the property being edited in the editor. We can add this to the above Name property like this: `[Description("Name shown when spawning prop")]` ###[JsonIgnore] This attribute will prevent the value of this property from being serialized to the json file being created. This means the property will not be saved and loaded through the asset file. Good usage of this is when you have helper properties in your GameResource type, these properties wouldn't need to be saved persistently since they aren't data fields. Example: `[JsonIgnore] public int TotalWeight => Weight * Quantity;` ## Using the inspector Now that you have everything set up, you can use the inspector tool to create assets of your custom type. 1. Go to the "Assets Browser" tab in the s&box editor. 2. Right click a folder in your addon and click "New <Your Asset Name>". 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: ### ResourceLibrary.Get<T> When assets are loaded they are stored in a dictionary with their path, you can access these with `ResourceLibrary.Get<T>`. ``` // Property allows for hot-loading public Clothing Clothing { get; set; } // ... // Load the resource from a path // If the asset isn't found, this returns null Clothing = ResourceLibrary.Get<Clothing>( "config/tshirt.clothing" ); ``` ### ResourceLibrary.TryGet<T> `TryGet<T>` serves a similar purpose to `Get<T>` but returns a bool indicating whether the resource could be found. If the resource was found, the method provides it through an `out` parameter. This is just a slightly cleaner way to handle missing assets. ``` if ( ResourceLibrary.TryGet<Clothing>( "config/tshirt.clothing", out var loadedClothing ) ) { Clothing = loadedClothing; } else { // Resource couldn't be found, handle that here... } ``` ### 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 ); } } ``` ## Possible solution for Asset.LoadResource returning null Make sure the class inheriting GameResource has an anonymous constructor, eg. ```cs [GameResource("Some Resource", "somersrc", "Example description")] public partial class SomeResource : GameResource { public SomeResource() { //this is an anonymous constructor //you can put code here } } ```
S&box Wiki
Development
Developer Overview
6
Editor Overview
General FAQ
System Requirements
The s&box wiki
Troubleshooting
Useful Links
The Project System
4
Adding Assets
Creating a Game Project
Project Settings Window - Games
Project Types
Publishing To Asset Party
2
Uploading assets
Uploading projects
Hammer
Getting Started With Hammer
3
Getting Started With Hammer
Making Your First Map
Mapping Resources
Mapping Basics
7
Cordons
Hotspot Materials
Selection Sets
Standard Mapping Dimensions
Tool Materials
Tools Visualisation Modes
Using Entities That Require a Mesh
Mapping Entities
2
Creating a Door
Light Entities
Advanced Mapping Techniques
8
Collaborating With Prefabs and Git
Instances
Prefabs
Quixel Bridge Plugin
Tilesets
Tilesets-Advanced
Tilesets-Proxies
VIS Optimizations
Models & Animation
Getting Started with Modeldoc
7
Automatic Model Setup
Breakpieces
Creating a Model
Guide to Models
Importing Rust Weapons
LODs
ModelDoc FAQ & best practices
Animgraph & Animation
4
Animations without Animgraph
AnimEvents, AnimGraph Tags, Attachments
Animgraph
Delta Animations
Physics
3
Cloth Physics
Collisions, Physics & Surface Types
Jiggle Bones
Modeldoc Nodes
1
Custom ModelDoc nodes
Advanced Modelling
6
Bodygroups
Citizen
First Person
IKChains and Stride Retargeting
Morphs
Vertex Normals
User Interface
UI Basics
7
Custom Fonts
Embedding Websites
Enabling Pointer Events
Events and Input
Localization
UI Basics
UI with Components
Styles & Stylesheets
1
Video Backgrounds
Razor Templates
4
A Razor Overview
Aliases and SetProperty Attributes
Generic Components
Templates
Game Menus
1
Making a Custom Pause Screen
Materials & Shaders
Materials
5
Guide to Materials
Material Attributes
Material Resources
Texture Settings
Using Dynamic Expressions
Built In Shaders
2
Foliage Shader
Glass Shader
Shaders
4
Compute Shaders
Constant Buffers
Material API
Shading Model
Shader Reference
5
Anatomy of Shader Files
Getting rid of Tex2D macros
Shader Reference
Shader States
Texture Format Cheat-Sheet
Other Assets
Sounds & Audio
3
Guide to Sounds
Sound Events
Soundscapes
Particles
5
Creating animated sprites
Creating your first particle effect
Understanding Particle Editor
Using custom sprites
Using particle systems from C#
Coding
Getting Started
5
Cheat Sheet
Learning Resources
Setting up Rider
Setting up Visual Studio
Setting up Visual Studio Code
Making Games
2
Components
GameObjects
Input
4
Commands
ConVars
Input System
Speech Recognition
Networking
7
Auth Tokens
Http Requests
Lobby System
Networked Types
Networking Basics
RPCs
WebSockets
Physics
5
Collisions
Hitboxes
Joints
Traces
Triggers
Rendering
3
Render Tags
RenderHooks
Scenes
Editor & Tools
7
Creating a Tool
Custom Asset Types
Guide to Widgets
Hammer API
Hammer Gizmos
Hotload Performance
Widget Docking
VR
3
Getting Started
VR Input
VR Overlays
Misc
13
Asset Types
Attributes and Component Properties
Backend API
Cloud Assets in code
Code Accesslist
CPU Performance Profiling
DisplayInfo
FileSystem
Mounting assets at runtime
package/find
Setting Up A Navigation Mesh
Threaded Tasks
TypeLibrary
Playing
Playing Guides
3
Default Keybinds
Proton
s&box on macOS (Experimental)
Console Commands & Variables
1
Launch Arguments
Dedicated Server
1
Dedicated Servers