返回首页DA系统C#IDE文件同步服务屏保 今天是: 2026-05-05    "立夏"  夏季的第一个节气,表示盛夏时节的正式开始

搜索
热搜: linux 技术
Hi~登录注册
查看: 2039|回复: 0

[原创] 【原创】简单的Unity练习(判断敌人血量和位置)

[复制链接]
发表于 2021-3-14 23:18:47 | 显示全部楼层 |阅读模式

少侠不来段修仙之旅吗~

您需要 登录 才可以下载或查看,没有帐号?注册成为修仙之旅的少年~

x
本帖最后由 da11 于 2021-3-17 00:07 编辑

//FindNPCHPMin.cs
[pre]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FindNPCHPMin : MonoBehaviour
{
    //练习,联动EMPInfo.cs脚本,查找EMPInfo类中,HP字段最小的游戏对象
    private void OnGUI()
    {
        if (GUILayout.Button("开始查找血量最低的敌人"))
        {
            //获取EMPInfo类型的数组
            EMPInfo[] GameEMPCache = Object.FindObjectsOfType<EMPInfo>();
            //获取血量最少的游戏对象
            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>();

            //获取距离最短的游戏对象
            GameObject EMPObj = PdNextObject(GameEMPCache);
            print("距离最短的是:" + EMPObj.name);

            //根据游戏对象改变该游戏对象的颜色
            EMPObj.GetComponent<MeshRenderer>().material.color = Color.yellow;
            print("物体名称是: " + EMPObj.name + "已变色");
        }
    }

    //获取血量最少的游戏对象方法
    private GameObject findHPMinNPC(EMPInfo[] GameEMPCache)
    {
        float defulHP = GameEMPCache[0].GetComponent<EMPInfo>().HP;
        GameObject EMPObj = null;

        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的位置);


    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;
            }
        }

        return JLMinObj;
    }

    //获取两个物体的Transform组件距离
    private float findJLNPC(Transform TFObj1, Transform TFObj2)
    {
        float JLCache = Vector3.Distance(TFObj1.position, TFObj2.position);
        return JLCache;
    }
}[/pre]


 楼主| 发表于 2021-3-14 23:19:27 | 显示全部楼层
本帖最后由 da11 于 2021-3-17 00:03 编辑


//EMPInfo.cs
[pre]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EMPInfo : MonoBehaviour
{
    //只有HP字段
    public float HP;
}
[/pre]
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-20 18:32:26 | 显示全部楼层
本帖最后由 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;
    }

}

回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-20 18:36:57 | 显示全部楼层
//优化EMPInfo.cs 类,添加HP缓存集合,用于保存每个物体的HP的数值
//可以直接套用

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EMPInfo : MonoBehaviour
{
    //只有HP字段
    public float HP;

    //更新后代码-----
    //添加HP缓存集合,用于保存每个物体的HP的数值
    public static List<float> Hpcache = new List<float>(10);

}

回复 支持 反对

使用道具 举报

游客
回复
*滑块验证:

DA论坛飞机票来了~
快速回复 返回顶部 返回列表