Skip to content

Scripting

Every effect property is a normal shader property, so you drive them with Unity’s own material API. No asset-specific code is involved, and the component that helps you set materials up in the editor plays no part at runtime.

Four ways, in order of convenience:

  1. From version 4.71, right click the property’s label in the Material Inspector and pick Copy property name. The name, for example _Color, goes to the clipboard ready to paste. Right click the label on the left: the value side keeps Unity’s own menu, which copies the value.
  2. Hover over any property in the Material Inspector. The tooltip shows the property name.
  3. Look it up on Effects and Properties Breakdown, which lists every one.
  4. Read the Properties block at the top of Assets\Plugins\AllIn1Assets\AllIn1SpriteShader\Shaders\AllIn1SpriteShader.shader.
The Material Inspector's General Properties, with an arrow from Main Color to its hover tooltip, which shows the property name _Color
Material mat = GetComponent<Renderer>().material;
mat.SetFloat("_FadeAmount", 0.5f);
mat.SetColor("_OutlineColor", Color.red);
mat.SetTexture("_FadeTex", dissolveTexture);

SetFloat, SetColor and SetTexture cover almost everything the shader exposes. Sliders are floats, colours are colours, textures are textures. Toggles are the exception: the shader reads their keyword, not a float, so switch them with EnableKeyword (each one is listed with its keyword on Effects and Properties Breakdown).

renderer.material creates an instance the first time you touch it, so changes affect this object alone and you own a material that 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 per-object effects like a damage flash. Use sharedMaterial when you deliberately want everything to change at once.

HITEFFECT_ON is the effect people reach for most from script. Enable it in the inspector, leave _HitEffectBlend at 0, and drive the blend when something gets hit:

using System.Collections;
using UnityEngine;
public class HitFlash : MonoBehaviour
{
private static readonly int HitBlend = Shader.PropertyToID("_HitEffectBlend");
[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()
{
mat.SetFloat(HitBlend, 1f);
for (float t = duration; t > 0f; t -= Time.deltaTime)
{
mat.SetFloat(HitBlend, t / duration);
yield return null;
}
mat.SetFloat(HitBlend, 0f);
}
}
The soldier with Hit Effect on and Hit Effect Blend at 0, flashing four times as Flash() is called, each flash fading over 0.15 seconds

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.

Two rules that catch everyone.

Masked images need materialForRendering. An Image inside a Mask or a Scroll View renders through a different material than the one in its slot, so writing to Image.material does nothing visible:

image.materialForRendering.SetFloat("_FadeAmount", t);

UI materials are shared. Every Image using the same material asset shares the same instance, so a change to one changes all of them. If you want per-image control, make a copy on Awake:

private void Awake()
{
Image uiImage = GetComponent<Image>();
uiImage.material = new Material(uiImage.material);
}

Per-renderer values without instancing a material

Section titled “Per-renderer values without instancing a material”

MaterialPropertyBlock also works with this shader and does not create a material instance. The asset uses one itself: SpritePropertiesSync pushes _SpriteFlip that way.

private MaterialPropertyBlock block;
private void Start()
{
block = new MaterialPropertyBlock();
}
private void SetFade(float amount)
{
spriteRenderer.GetPropertyBlock(block);
block.SetFloat("_FadeAmount", amount);
spriteRenderer.SetPropertyBlock(block);
}

This does not work for UI, where Image has no property block, and it cannot toggle keywords. It is for values only.

The AllIn1Shader component exists to set materials up in the editor, and nearly all of it is inside editor-only guards. 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.