using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// Component类提供了查找(当前物体,子类,父类)组件的功能。
///
public class ComponentDemo : MonoBehaviour
{
private void OnGUI()
{
if (GUILayout.Button("Transtorm"))
{
this.transform.position = new Vector3(0, 0, 10);
}
if (GUILayout .Button ("GetComponent"))
{
this.GetComponent
}
if(GUILayout.Button ("GetComponents"))
{
var allConmponent = this.GetComponents
foreach (var item in allConmponent )
{
print(item.GetType());
}
}
if (GUILayout.Button("GetComponentsInChildren"))
{
//获取后代物体的指定类型组件(从自身开始)
//var sun = this.GetComponentInChildren
var allConmponent = this.GetComponentsInChildren
foreach (var item in allConmponent)
{
item.material.color = Color.blue;
}
}
}
}
ComponenDemo
2020-04-04