Skip to content

Scripting

Every effect property is a normal shader property, so you drive them with Unity’s own material API. Nothing asset specific is involved.

Three ways, in order of convenience:

  1. Hover over any property in the Material Inspector. The tooltip is the property name.
  2. Look it up on Effects and Properties Breakdown, which lists every effect, its keyword and its properties.
  3. Read the Properties block at the top of Assets\Plugins\AllIn1Assets\AllIn13DShader\Shaders\Generic Shaders\AllIn13DShader.shader.
Material mat = GetComponent<Renderer>().material;
mat.SetFloat("_GeneralAlpha", 0.5f);
mat.SetColor("_Color", new Color(0.5f, 1f, 0f, 1f));
mat.SetTexture("_MainTex", texture);

SetFloat, SetColor and SetTexture cover almost everything the shader exposes. Sliders and toggles are floats, colours are colours, textures are textures.

renderer.material creates an instance the first time you touch it, so changes affect this object alone and you own a material Unity will not clean up until the object dies. renderer.sharedMaterial edits the asset itself, so every object using it changes, and in the editor that change is written to disk.

Use material for a per-object effect like a damage flash. Use sharedMaterial when you deliberately want everything to change at once.

The Hit effect is the one people reach for most from script. Enable it in the inspector, leave _HitBlend at 0, and drive the blend when something takes damage:

using System.Collections;
using UnityEngine;
public class HitFlash : MonoBehaviour
{
private static readonly int HitBlend = Shader.PropertyToID("_HitBlend");
[SerializeField] private float duration = 0.15f;
private Material mat;
private void Awake()
{
mat = GetComponent<Renderer>().material;
}
public void Flash()
{
StopAllCoroutines();
StartCoroutine(FlashRoutine());
}
private IEnumerator FlashRoutine()
{
for (float t = duration; t > 0f; t -= Time.deltaTime)
{
mat.SetFloat(HitBlend, t / duration);
yield return null;
}
mat.SetFloat(HitBlend, 0f);
}
}

Two things worth copying from that. The property ID is cached with Shader.PropertyToID in a static readonly field, which is how you should address any property you set every frame, since the string version hashes the name on each call. And the effect stays enabled the whole time, with only its blend moving. That is the recommended pattern, explained on How to Enable/Disable Effects at Runtime.

Effects with modes rather than an on and off

Section titled “Effects with modes rather than an on and off”

Some effects, Light Model and Specular Model among them, are a choice between modes rather than a switch. Those are keyword enums, so changing them from script means enabling the keyword for the mode you want:

mat.DisableKeyword("_LIGHTMODEL_CLASSIC");
mat.EnableKeyword("_LIGHTMODEL_TOON");

The asset component exists to set materials up in the editor. Do not call it from gameplay code. Once your material is set up you can remove the component, and leaving it costs nothing either way.