using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FindEnemyDeom : MonoBehaviour
{
private void OnGUI()
{
if (GUILayout .Button ("查找血量最低的敌人"))
{
//查找场景中所有Enemy类型的引用
Enemy[] allEnemy = Object.FindObjectsOfType<Enemy>();
//获取血量最低的对象的引用
Enemy min = FindEnemyByMinHp(allEnemy);
//根据Enemy类型的引用 获取其他组件类型引用
min.GetComponent<MeshRenderer>().material.color = Color.blue;
}
if (GUILayout.Button ("层级位置获取子物体"))
{
var childTF = TransformHelper.GetChild(this.transform , "Cube3");
childTF.GetComponent<MeshRenderer>().material.color = Color.blue;
//print(childTF.name);
}
}
public Enemy FindEnemyByMinHp(Enemy [] allEnemy)
{
Enemy min = allEnemy[0];
for (int i = 1; i < allEnemy .Length ; i++)
{
if (min.Hp > allEnemy[i].Hp)
min = allEnemy[i];
}
return min;
}
}