内容纲要

工具提示

步骤:

  1. 创建一个Canvas控件
  2. 在Canvas下新建一个名为BagPanel的Scroll View控件作为一个物品栏,
  3. 在Canvas下新建一个名为ToolTip的Panel控件作为一个信息显示框

    重点:将ToolTip的Rect Transform组件下的Pivot设置到左上角(x=0,y=1)

  4. 在ToolTip控件下新建一个名为Image的Image控件作为物品图片摆放处,新建一个名为Name的Text控件作为一个名称文本,新建一个名为Type的Text控件作为一个类型文本,新建一个名为Level的Text控件作为一个等级文本,新建一个名为Scroll的Scroll View控件作为一个可滚动的显现物品属性的文本,新建一个名为Text的Text控件作为一个介绍文本
  5. 在ToolTip控件下添加Canvas Group组件,取消所有的勾选
  6. 在ToolTip控件下添加ToolTip脚本

ToolTip脚本:

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ToolTip : MonoBehaviour
{
    public static ToolTip instance;

    public Image icon;
    public Text itemName, type, lvl, propText, desc;

    private CanvasGroup cg;
    private Transform canvasTransform;

    // 加载脚本实例时调用 Awake
    private void Awake()
    {
        instance = this;
        gameObject.SetActive(false);

        cg = this.GetComponent<CanvasGroup>();

        //canvasTransform = transform.root.Find("Canvas");//根节点
    }

    // 如果 MonoBehaviour 已启用,则在每一帧都调用 Update
    private void Update()
    {
        //将鼠标当前位置转换为GUI坐标,相对于它的父节点
        Vector2 localPos;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(transform .root as RectTransform, Input.mousePosition, null, out localPos))
        {
            transform.localPosition  = localPos; //相对于父节点
        }
    }
    /// <summary>
    /// 显示ToolTip面板
    /// </summary>
    /// <param name="id">物品类型id</param>
    /// <param name="sprite">物品的图片精灵</param>
    public void Show(int id,Sprite _sprite ) 
    { 
        gameObject.SetActive(true);

        ItemMgr.ItemInfo item = ItemMgr.instance.GetItem(id);
        if(item ==null)
        {
            Debug.Log(string.Format("要显示ToolTip的物品(ID={0}不存在!", id));
        }

        icon.sprite = _sprite;
        itemName.text = item.name;  //ToolTip的Name子节点的Text组件
        type.text = item.GetType().Name;    //Weapon /Potion /Armor
        lvl.text = item.lvl.ToString();
        propText.text = item.ToString();    //Weapon.ToString /Potion.ToString
        desc.text = item.description;
        cg.alpha = 0;
        cg.DOFade(1, 0.5f);
    }

    public void Hide()
    {
        //this.gameObject.SetActive(false);
        //扩展方法:本来一个类A如果没有某一个方法Foo,此时如果我们要给这个类增加要给方法,有几种方法--派生一个类B,在类B中添加Foo方法
        //这种方法不够简洁,C#提出了一种新的语法特征,它给类A增加一个扩展方法,该扩展方法可以访问类A的public成员,并且调用的语法与A a = new A(); a.Foo()相同
        cg.DOFade(0, 0.5f);
        cg.Foo(1, 2, "abc");
    }
}

public static class MyExtension
{
    public static void Foo(this CanvasGroup cg ,int a,int b,string c)
    {

    }
}

ItemView脚本:

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

public class ItemView : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public int typeId;
    public int num;

    //当鼠标划入该脚本绑定的对象时,此消息被引发
    public void OnPointerEnter(PointerEventData eventData)
    {
        ToolTip.instance.Show(typeId, transform.Find("Icon").GetComponent<Image>().sprite);
        print("Show");
    }
    //当鼠标划出该脚本绑定的对象时,此消息被引发
    public void OnPointerExit(PointerEventData eventData)
    {
        ToolTip.instance.Hide();
        print("Hide");
    }
}

ItemMgr脚本:

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

public class ItemMgr
{
    public static ItemMgr instance = new ItemMgr();

    public class ItemInfo
    {
        public int id;  //物品的ID
        public string name; //物品名称
        public string description;  //物品描述
        public string path; //物品图片的加载路径
        public int lvl; //物品等级
        public override string ToString()
        {
            string s = "等级:" + lvl;
            return s;
        }
        public ItemInfo (int _id,string _name,string _description,string _path,int _lvl)
        {
            id = _id;
            name = _name;
            description = _description;
            path = _path;
            lvl = _lvl;
        }
    }

    public class Weapon : ItemInfo
    {
        private int damage;
        public Weapon(int _id, string _name, string _description, string _path, int _lvl,int _damage) : base(_id, _name, _description, _path, _lvl)
        {
            Damage = _damage;
        }

        public int Damage { get => damage; set => damage = value; }

        public override string ToString()
        {
            string s = base.ToString();
            s = s + "\r\n" + "+伤害:" + Damage;
            return s;
        }
    }

    public class Potion:ItemInfo
    {
        public Potion(int _id, string _name, string _description, string _path, int _lvl,int _hp,int _mp) : base(_id, _name, _description, _path, _lvl)
        {
            Hp = _hp;
            Mp = _mp;
        }

        public int Hp { get; private set; }
        public int Mp { get; private set; }

        public override string ToString()
        {
            string s = base.ToString();
            s = s + "\r\n" + "回复血量:" + Hp;
            s = s + "\r\n" + "回复魔力:" + Mp;
            return s;

            //StringBuilder sb = new StringBuilder();
            //sb.AppendLine(base.ToString());
            //sb.AppendLine("回复血量:" + Hp);
            //sb.AppendLine("回复魔力:" + Mp);
            //return sb.ToString();

        }
    }

    public class Armor : ItemInfo
    {
        public int Power { get; private set; }
        public int Agile { get; private set; }
        public int Defense { get; private set; }
        public Armor(int _id, string _name, string _description, string _path, int _lvl,int _power,int _agi,int _def) : base(_id, _name, _description, _path, _lvl)
        {
            Power = _power;
            Agile = _agi;
            Defense = _def;
        }

        public override string ToString()
        {
            string s = base.ToString();
            s = s + "\r\n" + "+力量:" + Power;
            s = s + "\r\n" + "+敏捷:" + Agile;
            s = s + "\r\n" + "+防御:" + Defense;
            return s;
        }
    }

    Dictionary<int, ItemInfo> itemById = new Dictionary<int, ItemInfo>();
    //物品字典,key:物品类型ID(整型),value:物品信息(ItemInfo)
    public void AddItem(ItemInfo item) {
        itemById[item.id] = item;
    }
    public ItemInfo GetItem(int id)
    {
        return itemById[id];
    }
    public ItemMgr()
    {
        AddItem(new Weapon(0, "白帝剑", "稀有武器", string.Empty, 40, 50));
        AddItem(new Armor(1, "羽联衣", "稀有防具", string.Empty, 30, 12, 12, 30));
        AddItem(new Potion(2, "大补丸", "珍贵的回复道具", string.Empty, 50, 50, 50));
    }
}

1 对 “物品信息提示”的想法;

  1. Pingback: viagra

发表评论