获取组件与获取物体
获取物体/寻找物体
Find()
public static GameObject Find(string name);
此方法是遍历场景内所有物体,找出匹配物体,在大型复杂场景慎用,特别在Update慎用;
其中name中可以利用/指定搜索路径;
FindGameObjectsWithTag()
public static GameObject[] FindGameObjectsWithTag(string tag);
根据tag进行对象检索,返回的是对象数组;
tag需要提前设定好才能使用,tag为空或者未预设都会报错;
未预设报错:
空值报错:
寻找tag为player的物体数组:
void Start () {
Object = GameObject.FindGameObjectsWithTag("Player");
if (Object!=null) {
Debug.Log(Object.Length);
Debug.Log(Object[0].name);
}
}
FindWithTag()
public static GameObject FindWithTag(string tag);
根据tag进行对象检索,返回的是单个对象;
tag需要提前设定好才能使用,tag为空或者未预设都会报错;
寻找tag为player的物体:
void Start () {
Object = GameObject.FindGameObjectsWithTag("Player");
if (Object!=null) {
Debug.Log(Object.name+"找到");
}
}
获取组件/寻找组件
GetComponent
public Component GetComponent(Type type);
获取遍历的第一个对应组件;
//第一种调用方法
Transform trans = gameObject.GetComponent(typeof(Transform)) as Transform;
//第二种调用方法
Transform trans = gameObject.GetComponent<Transform>();
GetComponentInChildren
public Component GetComponentInChildren(Type type);
public Component GetComponentInChildren(Type type, bool includeInactive);
在场景中物体本身及子物体中深度优先遍历,获取遍历的第一个对应组件;
includeInactive:选择是否活跃的对象组件;
//单参数第一种调用方法
Transform trans = gameObject.GetComponentInChildren(typeof(Transform)) as Transform;
//单参数第二种调用方法
Transform trans = gameObject.GetComponentInChildren<Transform>();
//多参数第一种调用方法
Transform trans = gameObject.GetComponentInChildren(typeof(Transform),true) as Transform;
//多参数第二种调用方法
Transform trans = gameObject.GetComponentInChildren<Transform>(true);
GetComponentInParent
public Component GetComponentInParent(Type type);
在场景中物体本身及父物体中遍历,获取遍历的第一个对应组件;
//第一种调用方法
Transform trans = gameObject.GetComponentInParent(typeof(Transform)) as Transform;
//第二种调用方法
Transform trans = gameObject.GetComponentInParent<Transform>();
GetComponents
public Component [] GetComponents(类型type);
获取遍历的对应组件数组;
//第一种调用方法
Transform[] trans = gameObject.GetComponents(typeof(Transform)) as Transform;
//第二种调用方法
Transform[] trans = gameObject.GetComponents<Transform>();
GetComponentsInChildren
public Component[] GetComponentsInChildren(Type type, bool includeInactive = false);
在场景中物体本身及子物体中深度优先遍历,获取遍历的对应组件数组;
includeInactive:选择是否活跃的对象组件;
//单参数第一种调用方法
Transform trans = gameObject.GetComponentsInChildren(typeof(Transform)) as Transform;
//单参数第二种调用方法
Transform trans = gameObject.GetComponentsInChildren<Transform>();
//多参数第一种调用方法
Transform trans = gameObject.GetComponentsInChildren(typeof(Transform),true) as Transform;
//多参数第二种调用方法
Transform trans = gameObject.GetComponentsInChildren<Transform>(true);
GetComponentsInParent
public Component[] GetComponentsInParent(Type type, bool includeInactive = false);
public Component[] GetComponentsInChildren(Type type, bool includeInactive = false);
在场景中物体本身及父物体中遍历,获取遍历的对应组件数组;
includeInactive:选择是否活跃的对象组件;
//单参数第一种调用方法
Transform trans = gameObject.GetComponentsInParent(typeof(Transform)) as Transform;
//单参数第二种调用方法
Transform trans = gameObject.GetComponentsInParent<Transform>();
//多参数第一种调用方法
Transform trans = gameObject.GetComponentsInParent(typeof(Transform),true) as Transform;
//多参数第二种调用方法
Transform trans = gameObject.GetComponentsInParent<Transform>(true);