Unity3D说明文档翻译-Deactivating GameObjects

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);

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值