Modifier functions
A modifier function, ModifierFunction<T>, is what a modifier does to the value
before it, as an object you pass to Modify. In a field, it's picked and set up in the Inspector window:
your code decides when the modifier applies, and the Inspector window decides what it does. You can then tune a zone,
a buff or an effect without editing its script.
Add a function field
To pick a modifier's function in the Inspector window, perform the following steps:
- Add a
[SerializeReference]field of typeModifierFunction<T>to your script, withTthe type of the member it modifies, and pass it toModify:using Concordance; using UnityEngine; public class MusicFadeZone : MonoBehaviour { [SerializeField] AudioSource music; [SerializeReference] ModifierFunction<float> fade; void OnTriggerEnter(Collider other) => music.volume.Modify(fade); void OnTriggerExit(Collider other) => music.volume.StopModifying(); } - In the Inspector window, select a function in the field's dropdown, such as Multiply, and set its fields.
The field needs [SerializeReference]: Unity never saves a [SerializeField] field of this
type. Code analysis reports the missing attribute, and your code editor offers the fix.
A modifier keeps the function's values from when Modify ran. Call Modify again to apply new
values. The copy is shallow: a function's field that holds an object, such as the curve of Remap,
still points to the same object, so editing that object changes the modifier too. A function field left on
None leaves the member's value unchanged.
Tune a function in Play mode
An edit of a function field in the Inspector window applies at the next Modify, as a change from code
does. To see each edit at once, call Modify again from OnValidate, which Unity calls after
every Inspector edit:
bool inside;
void OnTriggerEnter(Collider other)
{
inside = true;
music.volume.Modify(fade);
}
void OnTriggerExit(Collider other)
{
inside = false;
music.volume.StopModifying();
}
void OnValidate()
{
if (Application.isPlaying && inside)
music.volume.Modify(fade);
}
Unity also calls OnValidate outside Play mode, when the script loads or recompiles, and
Modify throws there: check Application.isPlaying first. OnValidate runs whether
the modifier applies or not, so check that too, as inside does here.
Built-in functions
The dropdown lists the functions that fit the member's type:
| Function | Result |
|---|---|
| Set | Sets a value. |
| Add, Multiply and the other operators | Applies the operator to the value and Operand. |
| At Most, At Least, Clamp | Keeps the value under Max, over Min, or between both. |
| Abs | Returns the absolute value. |
| Snap | Rounds a float to the nearest multiple of Step. |
| Remap | Maps a float through Curve. |
| Blend | Moves the value toward Target by Weight, for float, vectors, Color and Quaternion. |
| Scale | Multiplies each axis of a vector by the same axis of Factor. |
Create a custom function
To add your own function to the dropdown, derive a [Serializable] class from
ModifierFunction<T>, and return the new value from Apply:
using System;
using System.ComponentModel;
using Concordance;
using UnityEngine;
[Serializable]
[DisplayName("Round Up")]
public sealed class RoundUp : ModifierFunction<float>
{
[SerializeField] float step = 1f;
public override float Apply(float previous) => Mathf.Ceil(previous / step) * step;
}
[DisplayName] sets the function's label in the dropdown. Apply runs each time the member's
value is computed again, so make it depend only on previous and its fields.