unity ToolTip

来源:互联网 发布:mac上类似美图秀秀 编辑:程序博客网 时间:2024/05/16 23:44
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 framevoid 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 initializationvoid Start () {        instance = this;        //itemRect = GetComponent<RectTransform>();        //canvasGroup = GetComponent<CanvasGroup>();        //canvasGroup.alpha = 0;        SetSelfActive(false);}// Update is called once per framevoid 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);    }}


0 0
原创粉丝点击