【发布时间】:2018-06-19 12:23:07
【问题描述】:
在 Unity 中,我有一个游戏对象作为父对象,两个子文本对象作为子对象。还有一个图像子对象(总共三个子对象)。
现在我想更改游戏对象的三个孩子的 alpha。如何以编程方式完成此操作?
我有这段代码,但它不适用于文本和图像对象:
public void setAlpha(float alpha) {
SpriteRenderer[] children = GetComponentsInChildren<SpriteRenderer>();
Color newColor;
foreach(SpriteRenderer child in children) {
newColor = child.color;
newColor.a = alpha;
child.color = newColor;
}
}
【问题讨论】:
-
作为文本和图像,您指的是 UI 元素吗?如果是这样,您必须使用 GetComponentsInChildren
和 GetComponentsInChildren 。不要忘记包含 UnityEngine.UI -
是的,我指的是 UI 元素。我想改变父母孩子的阿尔法。如何在 Unity 中制作它?