UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
GameObject.activeInHierarchy
# [GameObject](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.html).activeInHierarchy public bool **activeInHierarchy** ; ## 描述 定义 GameObject 在 Scene 中是否处于活动状态。 这可使您知道 GameObject 在游戏中是否处于活动状态。如果启用了其 [GameObject.activeSelf](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject-activeSelf.html) 属性,以及其所有父项的这一属性,则该 GameObject 处于活动状态。 ``` //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); } } } ``` 与 [GameObject.activeSelf](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject-activeSelf.html) 不同,它还检查是否有父 GameObject 影响此 GameObject 的当前活动状态。 当停用父 GameObject 时,即使其子项不可见,通常也会将它们标记为活动,这样根据 [GameObject.activeSelf](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject-activeSelf.html),它们仍处于活动状态。但 [GameObject.activeInHierarchy](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject-activeInHierarchy.html) 会确认其层级视图中的成员尚未停用此 GameObject,从而确保不会发生这种情况。 ``` //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; } } } ```
da
2022年5月26日 17:59
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码