Presets are a way to store a components values to an asset file that can be reapplied to other components either in the editor or by scripting.
Presets have one downside though: They are placed in the UnityEditor
namespace and can therefore not be used in your compiled game. Loading presets at runtime is unfourtunately not possible. This plug-in intends solve this problem.
Runtime presets are an alternative to the Unity presets by storing components as prefabs that can then be reapplied at runtime. Using runtime presets is nearly identical to using the default Unity presets:
public class ApplyPreset : MonoBehaviour
{
public Preset _preset;
public Light _light;
private void Awake()
{
_preset.ApplyTo(_light);
}
}
If you want to get a quick overview you should check out the demo project. You can also download the package containing only the essential scripts.
After downloading and importing the package, you will find a new entry Create Runtime Preset
in the gear menu of a component.
Open the gear menu on any GameObjects component and click Create Runtime Preset
.
The plug-in will then create the preset in the Assets
folder. A newly created preset will always have the default name New Preset
. Rename it after creation to prevent it from being overwritten.
The preset is simply a prefab with a copy of your selected component. It also has an additional component called Preset
. Keep this in mind as we'll need it in the next step.
Presets can be set in the inspector once you expose a variable of the Preset
type.
public class ApplyPreset : MonoBehaviour
{
//A preset which needs to be set in the inspector
public Preset _preset;
//A light component that we want to modify
public Light _light;
private void Awake()
{
//Presets store values of a specific component type
//Checking whether the values can be applied may be helpful
if(_preset.CanBeAppliedTo(_light))
{
//Transfers the values from the preset onto the _light component
_preset.ApplyTo(_light);
}
}
}
After compiling the script you can now set the preset and reference the light component. The preset
field is restricted to only accepting presets
. You won't accidentally reference something else than a preset
.
Presets can also be created during runtime. You will need any component to initialize the preset with. The preset can then be used to apply the values to any component of the same type.
public class CreatePreset : MonoBehaviour
{
//A reference to a light from another gameObject
public Light otherLight;
//The preset we want to store the lights values in
private Preset _preset;
void Start()
{
//Getting the light component of this gameObject
var light = GetComponent<Light>();
//Create a new preset from "otherLight" and apply its values to "light"
_preset = Preset.From(otherLight);
_preset.ApplyTo(light);
//Removes the temporary object created to store the runtime preset
//This is optional
_preset.Free();
}
}
The examples might be a bit forced, but I often find myself in more complex situations where runtime presets can be a real life saver. I hope I can save you some time with this plug-in. Feel free to contact me if you have any questions.