欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【UGUI】背包案例(三)

程序员文章站 2022-05-30 17:42:29
...

现在给背包中加入道具
在Grid格子下,创建一个Image,然后将找好的UI素材图片放进去,大小调整到合适之后,在Image下再创建一个Text,放在右下角,用来记录道具的数量
【UGUI】背包案例(三)
【UGUI】背包案例(三)
**注意:**右下角的Text如何设置可以不模糊
先对比一下右边没有设置过,明显要模糊很多
【UGUI】背包案例(三)
首先将Text的FontSize字体调到,100就可以了
然后将宽高设置一下,确保我们输入的内容显示出来了
最后将Text的TransFormScale等比缩小

给商店中加入道具
同理,不过商店的物品就不添加Text了,可以无限购买,所以只需要在商店的格子下添加一个Image就好了
【UGUI】背包案例(三)
人物装备
【UGUI】背包案例(三)
新建一个道具类,用来设置道具的各种属性

using UnityEngine;

public class Prpos
{

    /// <summary>
    /// ID
    /// </summary>
    public int p_id { get; }
    /// <summary>
    /// 名字
    /// </summary>
    public string p_name { get; }
    /// <summary>
    /// 价格
    /// </summary>
    public int p_price { get; }
    /// <summary>
    /// 属性
    /// </summary>
    public string p_property { get; }
    /// <summary>
    /// 精灵图片
    /// </summary>
    public Sprite p_sprite { get; }
    /// <summary>
    /// 类型
    /// </summary>
    public PrpoType p_prpoType { get; }
    /// <summary>
    /// 穿戴位置
    /// </summary>
    public WearPositionType p_wearPositionType { get; }

    public Prpos(int m_id, string m_name, int m_price, string m_property, Sprite m_sprite, PrpoType m_prpoType, WearPositionType m_wearPositionType)
    {
        p_id = m_id;
        p_name = m_name;
        p_price = m_price;
        p_property = m_property;
        p_sprite = m_sprite;
        p_prpoType = m_prpoType;
        p_wearPositionType = m_wearPositionType;
    }
}

两个枚举,用来区分装备的信息

public enum PrpoType
{
    /// <summary>
    /// 默认
    /// </summary>
    DEFAULT,
    /// <summary>
    /// 商品
    /// </summary>
    Commodity,
    /// <summary>
    /// 装备
    /// </summary>
    Equipment,
    /// <summary>
    /// 道具
    /// </summary>
    Prpo
}
/// <summary>
/// 穿戴位置
/// </summary>
public enum WearPositionType
{
    /// <summary>
    /// 默认
    /// </summary>
    DEFAULT,
    /// <summary>
    /// 头
    /// </summary>
    HEAD,
    /// <summary>
    /// 脸
    /// </summary>
    FACE,
    /// <summary>
    /// 手
    /// </summary>
    HAND,
    /// <summary>
    /// 腰
    /// </summary>
    WAIST,
    /// <summary>
    /// 腿
    /// </summary>
    LEG,
    /// <summary>
    /// 脚
    /// </summary>
    FOOT
}

最后一个道具管理类,将Resource文件下UI文件夹内的道具贴图和信息通过设置好的ID匹配起来

using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 道具管理类,文件信息存储取出
/// </summary>
public class PrpoManager : MonoBehaviour
{
    #region//私有的信息
    /// <summary>
    /// 接收Resources文件里的图片
    /// </summary>
    private List<Sprite> sprites;

    /// <summary>
    /// 将图片与ID对应储存
    /// </summary>
    private Dictionary<int, Sprite> keyValueSprites;

    /// <summary>
    /// 将信息文件与ID对应
    /// </summary>
    private Dictionary<int, PrpoInformation> keyValuePrpoInformations;
    #endregion

    /// <summary>
    /// 储存所有实例化的道具对象
    /// </summary>
    public Dictionary<int,Prpos> prpos;

    /// <summary>
    /// 序列化的文件中,存放我们设定好的属性
    /// </summary>
    public List<PrpoInformation> prpoInformation;

    void Start()
    {
        sprites = new List<Sprite>();
        sprites.AddRange(Resources.LoadAll<Sprite>("UI"));
        keyValueSprites = new Dictionary<int, Sprite>();
        foreach (var item in sprites)
        {
            keyValueSprites.Add(int.Parse(item.name), item);
        }

        keyValuePrpoInformations = new Dictionary<int, PrpoInformation>();

        foreach (var item in prpoInformation)
        {
            PrpoInformation PrpoInfor = new PrpoInformation();
            PrpoInfor = item;
            keyValuePrpoInformations.Add(item.id, PrpoInfor);
        }

        CreatPrpo();
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void CreatPrpo()
    {
        prpos = new Dictionary<int, Prpos>();

        foreach (var item in keyValueSprites)
        {
            int p_id = keyValuePrpoInformations[item.Key].id;
            int p_price = keyValuePrpoInformations[item.Key].price;
            string p_name = keyValuePrpoInformations[item.Key].name;
            string p_property = keyValuePrpoInformations[item.Key].property;
            PrpoType p_prpoType = keyValuePrpoInformations[item.Key].prpoType;
            WearPositionType p_wearPositionType = keyValuePrpoInformations[item.Key].wearPositionType;

            Prpos prpo = new Prpos(p_id, p_name, p_price, p_property, item.Value, p_prpoType, p_wearPositionType);
            prpos.Add(p_id,prpo);
        }
        foreach (var item in prpos)
        {
            Debug.Log(item.Value.p_id+ item.Value.p_name);
        }
    }
    [System.Serializable]
    public class PrpoInformation
    {
        public int id;
        public string name;
        public int price;
        public string property;
        public PrpoType prpoType;
        public WearPositionType wearPositionType;
    }
}

【UGUI】背包案例(三)
【UGUI】背包案例(三)
现在做的是将道具整合,分类,然后设置好了它独特的属性和ID,通过PrpoManager来获取

相关标签: UGUI