Search Unity

[Free] Using Presets at runtime

Discussion in 'Assets and Asset Store' started by MalteWeiss, Jan 31, 2019.

  1. MalteWeiss


    Sep 26, 2018
    What are (runtime) presets?

    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:

    Code (CSharp):
    2. public class ApplyPreset : MonoBehaviour
    3. {
    4.     public Preset _preset;
    5.     public Light _light;
    7.     private void Awake()
    8.     {
    9.         _preset.ApplyTo(_light);
    10.     }
    11. }

    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.

    Creating a runtime preset

    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.

    Loading runtime presets

    Presets can be set in the inspector once you expose a variable of the Preset type.

    Code (CSharp):
    1. public class ApplyPreset : MonoBehaviour
    2. {
    3.     //A preset which needs to be set in the inspector
    4.     public Preset _preset;
    5.     //A light component that we want to modify
    6.     public Light _light;
    8.     private void Awake()
    9.     {
    10.         //Presets store values of a specific component type
    11.         //Checking whether the values can be applied may be helpful
    12.         if(_preset.CanBeAppliedTo(_light))
    13.         {
    14.             //Transfers the values from the preset onto the _light component
    15.             _preset.ApplyTo(_light);
    16.         }
    17.     }
    18. }
    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.

    Creating presets during runtime

    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.

    Code (CSharp):
    1. public class CreatePreset : MonoBehaviour
    2. {
    3.     //A reference to a light from another gameObject
    4.     public Light otherLight;
    5.     //The preset we want to store the lights values in
    6.     private Preset _preset;
    8.     void Start()
    9.     {
    10.         //Getting the light component of this gameObject
    11.         var light = GetComponent<Light>();
    12.         //Create a new preset from "otherLight" and apply its values to "light"
    13.         _preset = Preset.From(otherLight);
    14.         _preset.ApplyTo(light);
    15.         //Removes the temporary object created to store the runtime preset
    16.         //This is optional
    17.         _preset.Free();    
    18.     }
    19. }
    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.
    St00nsa and PolyCrusher like this.
  2. 1illGamer


    Jul 24, 2016
    Thank you. I was looking for something like this.
    Last edited: Feb 7, 2019
  3. St00nsa


    Oct 22, 2018
    thanks man!