zoukankan      html  css  js  c++  java
  • 2019.9.19 Unity GameObject.activeSelf, GameObject.activeInHierarchy,GameObject.SetActive和SetActiveRecursively

    activeSelf(read only只读):物体本身的active状态,对应于其在inspector中的checkbox是否被勾选(可以判断物体是否在场景中active)
    activeInHierarchy(read only只读):物体在层次中是否是active的。也就是说要使这个值为true,这个物体及其所有父物体(及祖先物体)的activeself状态都为true。

    一个物体要在场景中是可见的(不是隐藏的),那么不仅仅其本身的activeSelf要为true,其所有父物体(及祖先物体)的activeself状态都要为true。

    总结:
    activeInHierarchy状态代表物体在场景中的实际的active状态。实际上代表的是物体及其所有祖先物体的activeSelf状态。而activeSelf对应于其在inspector中的checkbox是否被勾选

    activeSelf状态代表物体自身的activeSelf状态,所以当物体本身activeSelf为true,而其所有祖先物体的activeSelf状态不全为true时,这个物体的activeInHierarchy状态为false。

    activeSelf==物体自身
    activeInHierarchy==物体自身及其所有祖先物体==物体在场景中实际上是否激活

    至于SetActive,改变的是物体自身的activeSelf状态,所以,对一个物体SetActive时,其在场景中可能不会被激活,因为其祖先物体可能存在未被激活的。
    SetActiveRecursively,改变物体自身及其所有子物体的activeSelf状态,相当于对物体自身及其所有子物体调用SetActive.
    由于SetActiveRecursively已过时(obsolete),未来将移除,所以,当设置一个物体及其所有子物体的active状态时,可以调用一下方法

    1
    2
    3
    4
    5
    6
    7
    void DeactivateChildren(GameObject g, bool a) {
        g.activeSelf = a;
         
        foreach (Transform child in g.transform) {
            DeactivateChildren(child.gameObject, a);
        }
    }

    Advanced Skill :

     Using Extension Method

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public static class Extensions
    {
        public static void SetactivateForAllChildren(this GameObject go, bool state)
        {
            DeactivateChildren(go, state);
        }
     
        public static void DeactivateChildren(GameObject go, bool state)
        {
            go.SetActive(state);
     
            foreach (Transform child in go.transform)
            {
                DeactivateChildren(child.gameObject, state);
            }
        }
    }

      

    Now You Can Use Like That:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public  class MyTest : MonoBehaviour {
     
        public GameObject go;
        // Use this for initialization
        void Start () {
            //过时
            //go.SetActiveRecursively(true);
            go.SetactivateForAllChildren(true);
        }
    }

  • 相关阅读:
    解决多个window.onload冲突问题
    asp.net中img底部出现空白解決辦法
    学习WF起步
    ASP.NET后台注册javascript脚本方法
    WCF、Net remoting、Web service概念及区别
    WCF问答 WCF 与Web Service的区别
    C++深度探索系列:智能指针(Smart Pointer) [一] (转)
    ofstream和ifstream(详细2)转
    全面掌握const、volatile和mutable关键字(转)
    #define用法 收藏
  • 原文地址:https://www.cnblogs.com/LiTZen/p/11551273.html
Copyright © 2011-2022 走看看