Объявление
public Component GetComponentInParent(Type type);public Component GetComponentInParent(Type type, bool includeInactive);
Параметры
type | Тип компонента для поиска. |
Возвращает
Component Возвращает компонент, если найден компонент, соответствующий типу. В противном случае возвращает null.
Описание
Получает компонент типа type
в GameObject или любом из его родителей.
Этот метод выполняет рекурсию вверх, пока не найдет GameObject с соответствующим компонентом. Сопоставляются только компоненты активных игровых объектов.
using UnityEngine;
using System.Collections;
public class GetComponentInParentExample : MonoBehaviour
{
// Disable the spring on the first HingeJoint component found on any parent object
void Start()
{
HingeJoint hinge = gameObject.GetComponentInParent(typeof(HingeJoint)) as HingeJoint;
if (hinge != null)
hinge.useSpring = false;
}
}
Объявление
public T GetComponentInParent(bool includeInactive = false);Возвращает
T Возвращает компонент, если найден компонент, соответствующий типу. В противном случае возвращает null.
Описание
Общая версия этого метода.
using UnityEngine;
using System.Collections;
public class GetComponentInParentExample : MonoBehaviour
{
// Disable the spring on the first HingeJoint component found on any parent object
void Start()
{
HingeJoint hinge = gameObject.GetComponentInParent<HingeJoint>();
if (hinge != null)
hinge.useSpring = false;
}
}