Seaside Studios
HomeOverviewFAQsContactReview
Sprite Shader
Sprite Shader
  • What is All In 1 Sprite Shader
  • Overview
  • First Steps (Must Read)
  • Render Pipeline And Post Processing Setup
  • Asset Component Features
  • Right Click Create Material
  • Asset Window
  • Textures Setup
  • Custom Sort Axis
  • Saving Prefabs
  • Sprite Atlases
  • How to animate effects
  • Scripting
  • How to Enable/Disable Effects at Runtime
  • Random Seed
  • Scaled Time
  • UI Masking
  • 2D Renderer URP Lights
  • 3D Lights
  • Unified Outline
  • Render Material To Image
  • Compatibility with other assets
  • Effects and Properties Breakdown
  • Considerations
  • Running out of Shader Keywords
  • FAQ (Frequently Asked Questions)
Powered by GitBook
On this page

Scripting

PreviousHow to animate effectsNextHow to Enable/Disable Effects at Runtime

Last updated 7 months ago

If you prefer avoiding animations or want to change properties through code you also have the possibility.

You can find the property names by hovering the mouse over any property in the Material Inspector:

To do so you’ll need to use the following Unity functions:

You can find all property names on AllIn1SpriteShader/Resources/AllIn1SpriteShader.shader. All properties are located from line 5 to 225 and can also be found at the Effects and Properties Breakdown section. Here an example code snippet:

Material mat = GetComponent<Renderer>().material;
mat.SetFloat("_Alpha", 1f);
mat.SetColor("_Color", new Color(0.5f, 1f, 0f, 1f));
mat.SetTexture("_MainTex", texture);

Note that there is an important distinction to be made between a “material” and a “sharedMaterial” of a Renderer. You shall use “material” if you only want to change a property of that instance of the material. And “sharedMaterial” if you want to change the property of all the instances of that material

There's also an exception with materials used by a Masked UI Image where you’ll need to use “materialForRendering” instead. An example would be:

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

Finally ,consider that if you are trying to change a Material used in a UI component the material instances will be shared. This means that any change made to an image material will affect all other instances. To avoid this create a new copy of the material via script on an Awake method:

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

Material.SetFloat:

Material.SetColor:

Material.SetTexture:

https://docs.unity3d.com/ScriptReference/Material.SetFloat.html
https://docs.unity3d.com/ScriptReference/Material.SetColor.html
https://docs.unity3d.com/ScriptReference/Material.SetTexture.html