На сцене нужно создать три объекта:
1. ParentObject
- ChildObject
3. ToggleObject
На объект ToggleObject добавить скрипт ActiveInHierarchyExample.cs:
//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;
[SerializeField]
bool activeSelfChild;
[SerializeField]
bool activeInHierarchyChild;
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(m_ChildObject.name + " (activeInHierarchy): " + m_ChildObject.activeInHierarchy);
Debug.Log(m_ChildObject.name + " (activeSelf): " + m_ChildObject.activeSelf);
//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(m_ChildObject.name + " (activeInHierarchy): " + m_ChildObject.activeInHierarchy);
Debug.Log(m_ChildObject.name + " (activeSelf): " + m_ChildObject.activeSelf);
//Debug.Log("Child Active, parent might not be");
//You no longer need to output the message
m_SelfOutput = true;
}
activeSelfChild = m_ChildObject.activeSelf;
activeInHierarchyChild = m_ChildObject.activeInHierarchy;
}
void OnGUI()
{
//Switch this toggle to activate and deactivate the parent GameObject
m_ActivateParent = GUI.Toggle(new Rect(10, 10, 200, 30), m_ActivateParent, "Activate Parent GameObject");
//Switch this toggle to activate and deactivate the child GameObject
m_ActivateChild = GUI.Toggle(new Rect(10, 40, 200, 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;
}
}
}
После запуска сцены, объект ParentObject будет деактивирован, а вместе с ним станет неактивным ChildObject (они оба станут серого цвета).
Если родительский объект неактивен, и дочерний объект неактивен, то:
m_ChildObject.activeInHierarchy=false
m_ChildObject.activeSelf=false
Если родительский объект неактивен, а дочерний объект активен, то:
m_ChildObject.activeInHierarchy=false
m_ChildObject.activeSelf=true
Если родительский объект активен и дочерний объект активен, то:
m_ChildObject.activeInHierarchy=true
m_ChildObject.activeSelf=true
Если родительский объект активен, а дочерний объект неактивен, то:
m_ChildObject.activeInHierarchy=false
m_ChildObject.activeSelf=false
- EVE-Online: Фильтры каналов - 23.11.2024
- Не приходит СМС для авторизации на сайте Госуслуги - 01.11.2024
- VSCode: Найти и удалить элементы xml - 29.10.2024