Hi.
I have just created a simple method which try to unify the all UI selectable elements’ color block at once.
public void Unify(GameObject parent)
{
//initialize colorBlock
ColorBlock colorBlock = new ColorBlock();
colorBlock.normalColor = normalColor;
colorBlock.highlightedColor = hilightedColor;
colorBlock.pressedColor = pressedColor;
colorBlock.selectedColor = selectedColor;
colorBlock.disabledColor = disabledColor;
int i = 0;
List<GameObject> uiElements = GetAllChildren.GetAll(parent);
foreach(GameObject go in uiElements)
{
if (go.GetComponent<Selectable>() != null)
{
go.GetComponent<Selectable>().colors = colorBlock;
i++;
}
}
Debug.Log($"<color=cyan>{i} UI selectable elements has been modified.</color>");
}
When I call the function above, all ui selectable elements seems to be applied proper color in the editor property, but all of them displayed in grey and no selectedColor and/or pressedColor applied in the game view.
Instead doing above, by modifying the function as below, everything works just perfect but I do not understand what is the difference.
public void Unify(GameObject parent)
{
int i = 0;
List<GameObject> uiElements = GetAllChildren.GetAll(parent);
foreach(GameObject go in uiElements)
{
if (go.GetComponent<Selectable>() != null)
{
var colors = go.GetComponent<Selectable>().colors;
colors.normalColor = normalColor;
colors.highlightedColor = hilightedColor;
colors.pressedColor = pressedColor;
colors.selectedColor = selectedColor;
colors.disabledColor = disabledColor;
go.GetComponent<Selectable>().colors = colors;
i++;
}
}
Debug.Log($"<color=cyan>{i} UI selectable elements has been modified.</color>");
}
I should be misunderstand something. If anyone kindly could point out what it is, please do so.