最近做了一个怪物手册的功能

首先想到的就是获取到当前地图所有的怪物,然后排除重复种类的怪物,接着把玩家攻击怪物时候受到的伤害从小到大进行排序,最后把到UI上显示

遇到的问题:

1.开始偷懒,直接从创建怪物那里获取的怪物列表,然后问题就来了,怪物死掉后图鉴还是可以看到,这是不好的体验

2.排除重复种类的怪物,用的方法比较笨。。。。

3.排序么,想了下还是用sort吧

4.UI没啥好说的,知道RectTransform的anchoredPosition这个属性就好

最后附上代码=.=

using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;


public class MonsterManual : MonoBehaviour
{
    private List<GameObject> monster = new List<GameObject>();//当前地图所有的怪物
    private Transform tr;
    private GameObject instanceMM; //一种怪物用一个面板来显示
    private readonly Vector2 localScale = new Vector2(1, 1); //缩放比例


    private const int baseY = 400;//UI的Y轴
    private const int shift = 115;//UI与UI之间的间隔


    private Dictionary<int, GameObject> only = new Dictionary<int, GameObject>();//用字典来存储
    private List<int> monsterID = new List<int>();//怪物的id
    public List<GameObject> laji = new List<GameObject>();//


    private void Awake()
    {
        tr = transform;
        instanceMM = Resources.Load("Prefab/MonsterManual") as GameObject;
    }
    public void OnEnable()
    {
        monster.Clear();//清空数据
        only.Clear();
        GameObject[] mo = GameObject.FindGameObjectsWithTag(Tags.enemy);
        for (int i = 0; i < mo.Length; i++)//
        {
            monster.Add(mo[i]);
        }
        for (int i = 0; i < monster.Count; i++) //先找出不重复的id
        {
            if (!monsterID.Contains(monster[i].GetComponent<ObjectID>().codeID))
                monsterID.Add(monster[i].GetComponent<ObjectID>().codeID);
        }
        for (int i = 0; i < monsterID.Count; i++) //再根据id找到对应的怪物并设置对应的属性
        {
            if (!SelectGameobject(i))
                continue;
            GameObject clone = Instantiate(instanceMM);
            clone.transform.SetParent(tr);
            clone.GetComponent<RectTransform>().localScale = localScale;


            clone.transform.GetChild(0).GetComponent<Image>().sprite = SelectGameobject(i).GetComponent<SpriteRenderer>().sprite;
            //怪物的图标
            MonsterBase mb = SelectGameobject(i).GetComponent<MonsterBase>();
            clone.transform.GetChild(1).GetComponentInChildren<Text>().text = "攻击:" + mb.attack;
            clone.transform.GetChild(2).GetComponentInChildren<Text>().text = "防御:" + mb.defence;
            clone.transform.GetChild(3).GetComponentInChildren<Text>().text = "生命:" + mb.hp;
            clone.transform.GetChild(4).GetComponentInChildren<Text>().text = "损失:" + mb.GetLossValueString();
            clone.transform.GetChild(5).GetComponentInChildren<Text>().text = "经验:" + mb.exp;
            clone.transform.GetChild(6).GetComponentInChildren<Text>().text = "奖励点数:" + mb.rewardPoints;
            clone.transform.GetChild(7).GetComponent<Text>().text = mb.monsterName;
            int loss = mb.GetLossValue();
            only.Add(loss == -1 ? 999999900 + i : loss + i, clone);//为了保证键不重复
            laji.Add(clone);
        }
        monsterID = only.Keys.ToList();
        monsterID.Sort();
        for (int i = 0; i < monsterID.Count; i++)
        {
            only[monsterID[i]].GetComponent<RectTransform>().anchoredPosition = new Vector2(0, baseY - i * shift);
        }
    }


    private GameObject SelectGameobject(int i)//根据怪物的ID来选择object
    {
        GameObject game = null;
        for (int k = 0; k < monster.Count; k++)
        {
            if (monster[k].GetComponent<ObjectID>().codeID == monsterID[i])
                game = monster[k];
        }
        return game;
    }


    void OnDisable()//关闭的时候隐藏
    {
        for (int i = 0; i < laji.Count; i++)
        {
            laji[i].SetActive(false);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值