Deactivating GameObjects
禁用游戏对象
A GameObject can be temporarily removed from the scene by marking it as inactive. This can be done using its activeSelf property from a script or with the activation checkbox in the inspector
一个游戏对象可以通过标记为不激活使它临时从场景内移除.可以在脚本上使用它的”激活自身”属性或在检视面板的激活复选框上实现.
A GameObject’s activation checkbox
一个游戏对象的激活复选框
Effect of deactivating a parent GameObject
禁用父游戏对象的效果
When a parent object is deactivated, the deactivation also overrides the activeSelf setting on all its child objects, so the whole hierarchy from the parent down is made inactive. Note that this does not change the value of the activeSelf property on the child objects, so they will return to their original state once the parent is reactivated. This means that you can’t determine whether or not a child object is currently active in the scene by reading its activeSelf property. Instead, you should use theactiveInHierarchy property, which takes the overriding effect of the parent into account.
当一个父对象被停用,停用也覆盖”激活自身”设置到所有它的子对象上,所以从父对象以下层级都变为不激活.注意,没有改变子对象的”激活自身”属性的值,所以一旦父对象再激活,它们将返回它们原来的状态.这意味着你不能通过子对象的”激活自身”属性确定它是否在场景内激活.反而,你需要使用”在层级内激活”属性,它将父对象的覆盖效果考虑在内.
This overriding behaviour was introduced in Unity 4.0. In earlier versions, there was a function called SetActiveRecursively which could be used to activate or deactivate the children of a given parent object. However, this function worked differently in that the activation setting of each child object was changed - the whole hierarchy could be switched off and on but the child objects had no way to “remember” the state they were originally in. To avoid breaking legacy code, SetActiveRecursivelyhas been kept in the API for 4.0 but its use is not recommended and it may be removed in the future. In the unusual case where you actually want the children’s activeSelf settings to be changed, you can use code like the following:-
覆盖行为在Unity4.0中有介绍.在更早的版本,有一个功能叫”设置激活递归”,它可用于激活或停用父对象的子对象.然而,此功能在每个子对象激活设置改变后工作变为不同-整个层级将被关闭和打开但子对象没有办法”记住”它们原来的状态.为了避免破坏遗留代码,”设置激活递归”保留在4.0的API内,但不推荐使用它,将来它可能会被移除.在通常情况下你实际上想要改变子对象的”激活自身”设置,你可以使用代码像下面:
// JavaScript
function DeactivateChildren(g: GameObject, a: boolean) {
g.activeSelf = a;
for (var child: Transform in g.transform) {
DeactivateChildren(child.gameObject, a);
}
}
// C#
void DeactivateChildren(GameObject g, bool a) {
g.activeSelf = a;
foreach (Transform child in g.transform) {
DeactivateChildren(child.gameObject, a);
}
}