public bool activeInHierarchy ;

설명

Is the GameObject active in the scene?

This lets you know if a gameObject is active in the game. That is the case if its GameObject.activeSelf property is enabled, as well as that of all it's parents.

//This script shows how the activeInHierarchy state changes depending on the active state of the GameObject’s parent

using UnityEngine;

public class ActiveInHierarchyExample : MonoBehaviour { //Attach these in the Inspector public GameObject m_ParentObject, m_ChildObject; //Use this for getting the toggle data bool m_Activate;

void Start() { //Deactivate parent GameObject and toggle m_Activate = false; }

void Update() { //Activate the GameObject you attach depending on the toggle output m_ParentObject.SetActive(m_Activate); }

void OnGUI() { //Switch this toggle to activate and deactivate the parent GameObject m_Activate = GUI.Toggle(new Rect(10, 10, 100, 30), m_Activate, "Activate Parent GameObject");

if (GUI.changed) { //Output the status of the GameObject's active state in the console Debug.Log("Child GameObject Active : " + m_ChildObject.activeInHierarchy); } } }

Unlike GameObject.activeSelf, this also checks if any parent GameObjects affect the GameObject’s currently active state.

When a parent GameObject is deactivated, its children are usually marked as active even though they are not visible, so they are still active according to GameObject.activeSelf. However, GameObject.activeInHierarchy ensures this doesn’t happen by instead checking that the GameObject hasn’t been deactivated by a member in its hierarchy.

//This script shows how activeInHierarchy differs from activeSelf. Use the toggle  to alter the parent and child GameObject’s active states. This makes it output the child GameObject’s state in the console.
//It also shows how activeSelf outputs that the child GameObject is active when the parent is not, while the activeInHierarchy lists the child GameObject as inactive.

using UnityEngine;

public class ActiveInHierarchyExample : MonoBehaviour { public GameObject m_ParentObject, m_ChildObject; //Use this for getting the toggle data bool m_ActivateParent, m_ActivateChild; //Use these for deciding if console is needing updated bool m_HierarchyOutput, m_SelfOutput;

void Start() { //Deactivate parent and child GameObjects and toggles m_ActivateParent = false; m_ActivateChild = false; //Ables script to output current state of GameObject to console m_HierarchyOutput = false; m_SelfOutput = false; }

void Update() { //Activates the GameObject you attach depending on the toggle output m_ParentObject.SetActive(m_ActivateParent); m_ChildObject.SetActive(m_ActivateChild);

//Find out if the GameObject is active in the Game and checks if this state has been output to the console if (m_HierarchyOutput == false) { //Output the state of the GameObject’s activity if it hasn't already been output Debug.Log("Object Active : " + m_ChildObject.activeInHierarchy); //The state of the GameObject is output already, so no need to do it again m_HierarchyOutput = true; } //Check to see if the assigned GameObject is active despite parent GameObject's status if (m_ChildObject.activeSelf && m_SelfOutput == false) { //Output the message if the GameObject is still active Debug.Log("Child Active, parent might not be"); //You no longer need to output the message m_SelfOutput = true; } }

void OnGUI() { //Switch this toggle to activate and deactivate the parent GameObject m_ActivateParent = GUI.Toggle(new Rect(10, 10, 100, 30), m_ActivateParent, "Activate Parent GameObject"); //Switch this toggle to activate and deactivate the child GameObject m_ActivateChild = GUI.Toggle(new Rect(10, 40, 100, 30), m_ActivateChild, "Activate Child GameObject");

//If a change is detected with the toggle, the console outputs updates if (GUI.changed) { m_SelfOutput = false; m_HierarchyOutput = false; } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961