Loading lessons...
AssetDatabase & Presets
AssetDatabase & Presets
For advanced project automation and workflow uniformity, Unity offers the AssetDatabase API and Preset configuration templates.
The AssetDatabase API
The AssetDatabase class (located in the UnityEditor namespace) allows you to interact with files and assets within the Unity project programmatically inside editor scripts:
- Creating Assets: Use
AssetDatabase.CreateAsset(myAsset, "Assets/MyAsset.asset");to serialize a ScriptableObject instance to disk. - Importing Assets: Import or re-import assets programmatically using
AssetDatabase.ImportAsset("Assets/Textures/stone.png");. - Loading Assets: Load assets by their path using
AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/Textures/stone.png");. - AssetDatabase.SaveAssets(): Writes all unsaved asset changes to disk.
Presets
A Preset is a special asset containing configuration properties copied from an existing component or asset importer:
- Creating Presets: Click the slider icon in the top right of a component (e.g. Rigidbody or BoxCollider) and select Save current to... to create a Preset asset.
- Default Presets: You can assign default presets in the Preset Manager. For instance, you can configure a default Preset for textures so that all newly imported images are automatically set to Sprite mode and compressed correctly.
TL;DR
- AssetDatabase provides C# scripting controls to import, create, and load project assets.
- AssetDatabase APIs are only available in the Editor namespace.
- Presets save component or importer properties into reusable asset templates.
- Use default Presets to automate setup values for newly created or imported files.