【UGUI】背包案例(六)

实现道具信息的介绍
截图
背包中一个较为重要的功能了,介绍板,用来展示道具的信息

  1. 先做这样一个Ui面板,然后在它下面创建一个Text
    在这里插入图片描述
  2. 在Inspector面板将他们Text组件和Image组件上的Raycast Target,全部勾选false掉
  3. 将这个Information面板放到Canvas的最下层,这样就不会被别的UI挡住
  4. 在我们的Prpo类里面添加代码
    继承者两个接口IPointerEnterHandler, IPointerExitHandler
    在这里插入图片描述
  5. 实现接口后,就可以添加逻辑代码了
 /// <summary>
    /// 当鼠标移到上面
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerEnter(PointerEventData eventData)
    {
        //如果装备没有被拖拽
        if (!isDrag)
        {
            PrpoManager.instance.informationPanel.SetActive(true);
            //面板位置等于鼠标位置
            PrpoManager.instance.informationPanel.transform.position = Input.mousePosition;

            
            int t_id = int.Parse(GetComponent<Image>().sprite.name);
            string t_name = PrpoManager.instance.prpos[t_id].p_name;
            int t_price = PrpoManager.instance.prpos[t_id].p_price;
            string t_property = PrpoManager.instance.prpos[t_id].p_property;
            string t_point = "null";
            switch (PrpoManager.instance.prpos[t_id].p_wearPositionType)
            {
                case WearPositionType.DEFAULT:
                    t_point = "null";
                    break;
                case WearPositionType.HEAD:
                    t_point = "头";
                    break;
                case WearPositionType.FACE:
                    t_point = "脸";
                    break;
                case WearPositionType.HAND:
                    t_point = "手";
                    break;
                case WearPositionType.WAIST:
                    t_point = "腰";
                    break;
                case WearPositionType.LEG:
                    t_point = "腿";
                    break;
                case WearPositionType.FOOT:
                    t_point = "脚";
                    break;
                default:
                    break;
            }


            PrpoManager.instance.informationPanel.GetComponentInChildren<Text>().text = "名字:" + t_name + "\n价格:" + t_price + "\n属性:" + t_property + "\n穿戴位置:" + t_point;
        }
    }

    /// <summary>
    /// 当鼠标离开
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerExit(PointerEventData eventData)
    {
        PrpoManager.instance.informationPanel.SetActive(false);
    }
  1. 这里面读取的信息,在我们之前写好的PrpoManager里面
    在这里插入图片描述
    PrpoManager类的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 道具管理类,文件信息存储取出
/// </summary>
public class PrpoManager : MonoBehaviour
{
    /// <summary>
    /// 单例模式
    /// </summary>
    public static PrpoManager instance;

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

    /// <summary>
    /// 背包Canvas在面板拖拽进来
    /// </summary>
    public Canvas m_canvas;

    /// <summary>
    /// 信息面板
    /// </summary>
    public GameObject informationPanel;

    private void Awake()
    {
        instance = this;
    }

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

目前为止,已经实现了拖拽、整理、购买、出售、丢弃、合并和信息展示,只剩下使用道具和装备道具了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贪小心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值