Hey everyone!
I’ve written a small multi-channel mask shader to be used in my game’s UI.
This shader is being used for selectable objects, and need to change one or more colors on selection state change.
The shader seems to work as intended in inspector, changing all colors properly, but at runtime some weirdness begins to happen.
I start by spawning a few objects using a material with this shader, and use a method in these objects’ Awake to set their initial colors. Then I change the colors of my initially selected object.
This all works fine, but when I later try to change the selection through a click on a UI element, the state change goes through fine but the material colors do not update. I have verified that all the necessary code is being called for this to happen, and it worked with a previous shader I used for these materials (that I had to replace due to Masking issues. Shadergraph shaders seem to not support masking yet).
Relevant Shader Properties:
_Color_Outline("Color_Outline", Color) = (1,1,1,1)
_Color_Border("Color_Border", Color) = (1,1,1,1)
_Color_Fill("Color_Fill", Color) = (1,1,1,1)
UI Element Code:
public bool Selected
{
get
{
return _selected;
}
set
{
if(_selected != value)
{
_selected = value;
SetOutlineColors(_selected);
}
}
}
private void Awake()
{
var coIndex = _baseImage.material.shader.FindPropertyIndex("_Color_Outline");
var cbIndex = _baseImage.material.shader.FindPropertyIndex("_Color_Border");
_outlineColorId = _baseImage.material.shader.GetPropertyNameId(coIndex);
_borderColorId = _baseImage.material.shader.GetPropertyNameId(cbIndex);
_button.onClick.AddListener(() => { Selected = true; });
SetOutlineColors(Selected);
}
private void SetOutlineColors(bool selected)
{
_baseImage.material.SetColor(_outlineColorId, selected ? _outlineColorSelected : _outlineColorDefault);
_baseImage.material.SetColor(_borderColorId, selected ? _borderColorSelected : _borderColorDefault);
Debug.Log($"{name} Setting Outline Colors, selected: {selected}."); //This calls when expected!
}