unity ToolTip

UGUI
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ToolTipManager : MonoBehaviour {
    public static ToolTipManager instance;
    private RectTransform rect;
    public Text name;
    public Image icon;
    public Text att;
    public Text introduce;
	// Use this for initialization
    void Awake()
    {
        instance = this;
    }
	void Start () {
        rect = GetComponent<RectTransform>();
        SetToolTipPanelHide();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public void SetToolTipPanelShow(PointerEventData eventData,int itemID)
    {

        gameObject.SetActive(true);
        SetPosition(eventData);
        SetValue(itemID);

    }
    public void SetToolTipPanelHide()
    {
        gameObject.SetActive(false);
    }

    public void SetValue(int itemID)
    {
        BaseItem item = BaseItemMangager.instance.GetData(itemID);
        name.text = item.itemName;
        icon.sprite = (Sprite)Resources.Load("Texture/Shop/" +item.itemIcon, typeof(Sprite));
        introduce.text = item.itemIntroduce;
        string str = "";
        switch (item.itemType)
        {
            case ItemType.Equip:
                ItemEquipment tempEquipment = (ItemEquipment)item;
                str = "伤害:" + tempEquipment.itemATK + "\n"
                    + "防御:" + tempEquipment.itemDEF + "\n"
                    + "速度:" + tempEquipment.itemMS;
                break;
            case ItemType.Drug:
                ItemDrug tempDrug = (ItemDrug)item;
                str = "加蓝:" + tempDrug.itemAMP + "\n"
                    + "加血:" + tempDrug.itemAHP + "\n";
                break;
        }
        att.text = str;
    }
    public void SetPosition(PointerEventData eventData)
    {
        Vector3 worldPos;
        if(RectTransformUtility.ScreenPointToWorldPointInRectangle(rect,eventData.position,eventData.pressEventCamera,out worldPos))
        {
            rect.position = worldPos;
        }
    }
}

NGUI

using UnityEngine;
using System.Collections;

public class Tooltip : MonoBehaviour {
    public static Tooltip instance;
    public UILabel name;
    public UISprite icon;
    public UILabel att;
    public UILabel introduce;
    //private RectTransform itemRect;
    //private CanvasGroup canvasGroup;
    private bool isShow = false;
	// Use this for initialization
	void Start () {
        instance = this;
        //itemRect = GetComponent<RectTransform>();
        //canvasGroup = GetComponent<CanvasGroup>();
        //canvasGroup.alpha = 0;
        SetSelfActive(false);
	}
	
	// Update is called once per frame
	void Update () {
        if (isShow)
        {
            SetPosition();
        }
	}


    public void Show(Item item, ItemData itemData)
    {
        //eventD = eventData;
        SetPosition();
        SetValue(item, itemData);
        //canvasGroup.alpha = 1;
        SetSelfActive(true);
    }

    public void Hide()
    {
        //canvasGroup.alpha = 0;
        SetSelfActive(false);
    }

    void SetSelfActive(bool b)
    {
        gameObject.SetActive(b);
        isShow = b;
    }

    public void SetValue(Item item, ItemData itemData)
    {
        name.text = itemData.itemName;
        icon.spriteName = item.itemImage.spriteName;
        introduce.text = itemData.itemIntroduce;
        string str = "";
        switch(itemData.itemType)
        {
            case 0:
                ItemEquipment tempEquipment =(ItemEquipment)itemData ;
                str = "伤害:" + tempEquipment.itemDamage + "\n"
                    + "命中:" + tempEquipment.itemHit + "\n"
                    + "耐久:" + tempEquipment.itemDurable;
                break;
            case 1:
                ItemDrug tempDrug = (ItemDrug)itemData;
                str = "品质:" + tempDrug.itemQuality + "\n"
                    + "恢复气血:" + tempDrug.itemAddHP + "\n"
                    + "等级:" + tempDrug.itemLv;
                break;
        }
        att.text = str;
    }

    private void SetPosition()
    {
        transform.position = UICamera.currentCamera.ScreenToWorldPoint(Input.mousePosition);
    }
}


  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值