⚙️ Presets in Unity

How to add and manage Presets in Unity both Editor and Scripting side? 📝

Irfan Yigit Baysal
7 min readSep 11, 2023

Time and efficiency are essential in the fast-paced world of game production. Developers sometimes struggle with monotonous activities like setting preferences for several objects or components. Presets in Unity, which provides a smooth way to simplify operations and uphold consistency across your project. Simply, you can use to save and apply identical property settings by using Preset. Let’s start with and example of it.

Let’s add a game object and create a preset from its transform. Before creating preset, configure the transform component as it wants to use it as a preset.

Let’s click the preset selector icon incidated above. A window opens and if there is a preset you have made for this component, it appears in the window. You can either select the preset if any or create a new one by clicking the button (“Save current to…”) indicated below. Preset will be ready when we select a directory folder for preset. I commonly prefer to create a Folder named “Presets” for presets.

Let’s exemplify whether to add presets by default through another component.

I have created a preset for Rigidbody component before and let’s say I want this rigidbody component’s preset to become default for all rigidbody components that will be created later.

When I click “Add to Rigidbody default” button indicated above it automatically add itself into Preset Manager.

Preset Manager

It lets you manage the custom presets that we create. It overrides the unity’s pre-defined settings. So that we can control each component that we specify with the presets.

We can also filter the component by editing the filter area in preset manager to select the components we apply to. Let’s test it. I create two game object that contain Rigidbody Component and I define a filter name according to GameObject name.

As you can see above. I create a filter for Rigidbody Components named “FilteredRigidbody” and PresetManager applies preset to this component. For detailed usage of PresetManager, I would like to suggest you take a look at Unity documentation.

Glob Search Filter

We can also use “glob” search to filter more advancedly and inclusively. To create a glob search filter, the syntax is glob:"yoursearchpatternhere".Your filter field must begin glob: .For more details about syntax, I recommend you to visit scripting API about glob topic.

Pros (+)

  1. Batch Import: Using glob allows you to import multiple assets at once, making it efficient for importing a large number of assets into your project.
  2. Organized Import: You can specify a folder structure and naming conventions for your assets using glob patterns, which helps keep your project organized.
  3. Automated Workflow: Automating asset import using glob can save time and reduce human error, especially in projects with frequent updates or large asset libraries.

Cons (-)

  1. Limited Flexibility: glob patterns can be inflexible when dealing with complex import requirements. For instance, it may not handle cases where you need different import settings for specific assets.
  2. Debugging Challenges: If there’s an issue with your glob pattern, it can be challenging to debug, as Unity doesn't provide extensive error messages or debugging tools for Preset Manager configurations.
  3. Maintenance Overhead: As your project evolves, you may need to update and maintain your glob patterns to accommodate new assets or changes in your project structure. This can be time-consuming.
  4. Limited Scripting Control: Some advanced import requirements or custom asset processing may not be achievable using only glob patterns. You might need to resort to custom scripts or tools.

For me, due to limited flexibility issue, I try not to prefer the glob pattern too much unless I have to but it is still a good feature to handle presets if you use it properly according to your requirements. I explained and sampled this issue in Work Life Experience part below.

For solving limited flexibility issue, I prefer to create an editor code like below to control the presets and show them to the desired objects, you can control them more easily by using a simple and useful editor, as shown in the picture below.

https://gist.github.com/irfanbaysal/3e8d8c209b8994d17636e7511315f0b5

You can reach the example code by clicking this.

Code Examples

A Reminder

Presets are placed in the UnityEditor namespace and Editor-only feature, can therefore not be used in your compiled game. Loading presets at runtime is unfourtunately not possible. If we want to add preset via script, we should be careful about using Editor defines and usings. However, there is a package to use presets in runtime. You can check it out by clicking this. In future articles, we are going to examine this package.

Preset.ApplyTo(Object target) is the function that applies to preset to selected object.

#if UNITY_EDITOR
using UnityEditor.Presets;

[SerializeField] private Preset sourcePreset;


private void ApplyPreset()
{
sourcePreset.ApplyTo(this);
}
#endif

What if we want to find all assets in specific path and apply preset to them? I mostly separate the function according to asset type and mostly use this code block.

In this example code block, I assume that the asset we want to modify is a Texture2D type.

[SerializeField] private Preset preset;

private void ApplyPresetToTexture(Texture2D texture)
{
string assetPath = AssetDatabase.GetAssetPath(texture);
TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;

if (textureImporter != null)
{
preset.ApplyTo(textureImporter);
AssetDatabase.ImportAsset(assetPath);
}
else
{
Debug.LogWarning("Failed to apply preset to texture: " + texture.name);
}

When you pass texture or textures to this method, it will apply the preset that you select. You may use this code block by converting any type you want to handle.

[SerializeField] private List<Texture2D> presetTextures = new();

foreach (var texture in presetTextures)
{
ApplyPresetToTexture(texture);
}

If you want a generic search,

private List<T> FindObjectsInFolders<T>(List<string> folders,
string searchPattern = "*.png")
where T : Object
{
return folders
.SelectMany(folderPath => Directory.GetFiles(folderPath, searchPattern, SearchOption.AllDirectories),
(folderPath, texturePath) => AssetDatabase.LoadAssetAtPath<T>(texturePath))
.Where(texture => texture != null)
.ToList();
}

Preset.DataEquals() is the function that checks whether the target object has the same serialized values as the Preset or not. So with this check, you skip to apply the elements that you already applied.

if (preset.DataEquals(textureImporter))
{
Debug.Log($"{textureImporter} has the same preset as you want to apply");
return;
}

Work Life Experience

Do we need to create an editor to control presets?

It depends on your project scale and requirements. I recently needed this kind of editor code.

I had a task to reduce the app size in a project I just took over, and you can think of the game as a level-based 2D animation game consisting of approximately 600 levels. What needed to be done here was to set all sprites, spines and sounds to the relevant presets. I thought the shortest way to do this was to code an editor like this to find and set required objects. Using Glob could also be a solution, but I feel more free when editing something or adding a new feature in such editors.

In conclusion, Unity’s Preset Manager completely transforms how we think about asset management and configuration. It equips developers to improve their procedures and increase efficiency thanks to its user-friendly interface, smooth connection with existing workflows, and robust customization options. The Preset Manager’s ability to quickly manage, arrange, and apply presets across various assets gives up new possibilities for creativity and cooperation, regardless of whether you’re working on a small independent project or a large-scale production. With Unity’s Preset Manager, embrace the future of asset management and open up a world of opportunities for quickening your development process.

Links That You Want To Check

--

--