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().material.color = Color.red;
}
if(GUILayout.Button ("GetComponents"))
{
var allConmponent = this.GetComponents();
foreach (var item in allConmponent )
{
print(item.GetType());
}
}
if (GUILayout.Button("GetComponentsInChildren"))
{
//获取后代物体的指定类型组件(从自身开始)
//var sun = this.GetComponentInChildren();//返回包含MeshRanderer组件的子对象
var allConmponent = this.GetComponentsInChildren();//返回所有包含MeshRanderer组件的子对象
foreach (var item in allConmponent)
{
item.material.color = Color.blue;
}
}
}
}