本帖最后由 da11 于 2021-3-20 18:33 编辑
//优化了FindNPCHPMin.cs 脚本类,添加了1.删除具有EMPInfo组件的物体、2.恢复具有Player便签的物体EMPInfo组件,且组件内属性一并恢复、3.给具有EMPInfo组件的物体打上Player便签
//可以按此最新的直接复制套用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FindNPCHPMin : MonoBehaviour
{
///<summary>
///
///</summary>
/*
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
*/
//练习,联动EMPInfo.cs脚本,查找EMPInfo类中,HP字段最小的游戏对象
private void OnGUI()
{
if (GUILayout.Button("开始查找血量最低的敌人"))
{
//获取EMPInfo类型的数组
EMPInfo[] GameEMPCache = Object.FindObjectsOfType<EMPInfo>();
if (GameEMPCache.Length == 0)
{
print("找不到敌人了");
return;
}
//获取血量最少的游戏对象
GameObject EMPObj = findHPMinNPC(GameEMPCache);
print("血量最少的是:" + EMPObj.GetComponent<EMPInfo>().HP + "物体名称是: " + EMPObj.name);
//根据游戏对象改变该游戏对象的颜色
EMPObj.GetComponent<MeshRenderer>().material.color = Color.red;
print("物体名称是: " + EMPObj.name + "已变色");
}
if (GUILayout.Button("开始查找距离最近的敌人"))
{
//获取EMPInfo类型的数组
EMPInfo[] GameEMPCache = Object.FindObjectsOfType<EMPInfo>();
if (GameEMPCache.Length==0)
{
print("找不到敌人了");
return;
}
//获取距离最短的游戏对象
GameObject EMPObj = PdNextObject(GameEMPCache);
print("距离最短的是:" + EMPObj.name);
//根据游戏对象改变该游戏对象的颜色
EMPObj.GetComponent<MeshRenderer>().material.color = Color.yellow;
print("物体名称是: " + EMPObj.name + "已变色");
}
//更新后代码------
if (GUILayout.Button("清除EMPInfo组件"))
{
EMPInfo[] EMPtemp = Object.FindObjectsOfType<EMPInfo>();
if (EMPtemp.Length==0)
{
print("你名下没有EMPInfo组件需要删除");
return;
}
foreach (var item in EMPtemp)
{
EMPInfo.Hpcache.Add(item.HP);
item.gameObject.tag = "Player";
Object.Destroy(item.GetComponent<EMPInfo>());
}
}
if (GUILayout.Button("恢复默认--恢复EMPInfo组件"))
{
GameObject[] Transformtemp = GameObject.FindGameObjectsWithTag("Player");
if (Transformtemp.Length == 0)
{
print("没有Player的标签");
return;
}
int i = 0;
foreach (var item in Transformtemp)
{
item.AddComponent<EMPInfo>();
item.GetComponent<EMPInfo>().HP = EMPInfo.Hpcache;
print(item.name + "物体已恢复EMPInfo组件");
i++;
}
}
}
//获取血量最少的游戏对象方法
private GameObject findHPMinNPC(EMPInfo[] GameEMPCache)
{
float defulHP = GameEMPCache[0].GetComponent<EMPInfo>().HP;
GameObject EMPObj = GameEMPCache[0].gameObject;
foreach (var HP in GameEMPCache)
{
if (HP.GetComponent<EMPInfo>().HP < defulHP)
{
//将组件转换成该游戏对象并赋值给EMPObj
EMPObj = HP.gameObject;
}
}
return EMPObj;
//Object.Destroy(EMPObj);
//print("物体名称是: " + EMPObj.name + "以销毁");
}
//练习2:查找距离你最近的敌人
//当前物体与敌人的间距
//float distance = Vector3.Distance(物体1的位置,物体2的位置);
//处理EMPInfo数组中,确定距离最短的物体
private GameObject PdNextObject(EMPInfo[] EMPObj)
{
float JLCache = findJLNPC(this.transform, EMPObj[0].transform);
GameObject JLMinObj = EMPObj[0].gameObject;
foreach (var item in EMPObj)
{
if (JLCache > findJLNPC(this.transform, item.transform))
{
JLCache = findJLNPC(this.transform, item.transform);
JLMinObj = item.gameObject;
}
}
print("最短的距离参数:" + JLCache);
return JLMinObj;
}
//获取两个物体的Transform组件距离
private float findJLNPC(Transform TFObj1, Transform TFObj2)
{
float JLCache = Vector3.Distance(TFObj1.position, TFObj2.position);
return JLCache;
}
}
|