Unity中GameObject.activeInHierarchy和GameObject.activeSelf的区别

引言
昨天在工作中调用GameObject.active时看到有这样的提示”GameObject.active is obsolete. Use GameObject.SetActive(), GameObject.activeSelf or GameObject.activeInHierarchy.”发现Unity推荐使用activeInHierarchy和activeSelf的方法来使用。
这里写图片描述
区别何在?
从注释中看,activeInHierarchy表示在场景中的active状态,active仅代表当前gameobject的active状态。
动手试验
创建如下测试对象
这里写图片描述
将测试代码挂载在UIRoot上

using UnityEngine;
using System.Collections;

public class VisibleTest : MonoBehaviour {
    public GameObject parent, child;
    // Use this for initialization
    void Start () {
        if (parent != null)
        {
            Debug.Log("parent activeInHierarchy: " + parent.activeInHierarchy + ", parent activeSelf: " + parent.activeSelf);
        }
        if (child != null)
        {
            Debug.Log("child activeInHierarchy: " + child.activeInHierarchy + ", child activeSelf: " + child.activeSelf);
        }
    }

    // Update is called once per frame
    void Update () {

    }
}

第一种情况:
parent[active] child[active]
这里写图片描述
parent和child的两种状态都为true。
第二种情况:
parent[inactive] child[active]
这里写图片描述
这是parent的两种状态都为false,而child的activeHierarchy为false,activeSelf为true。此时的child并步可见,但是自身的active属性还是true。

结合上述的例子,可以看出activeHierarchy可以理解为场景中的可见状态如果某个对象在场景中不可见,对应的activeHierarchy和activeSelf属性一定为false,而该对象的子对象的activeHierarchy也为false,其activeSelf属性却取决于自身的状态,不依赖于父对象。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值