UGUI背包(二)鼠标移动会显示信息框,拖拽物品

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;//
using System;//Action是命名空间下的委托
public class GridUI : MonoBehaviour ,IPointerEnterHandler,IPointerExitHandler,IBeginDragHandler,IDragHandler,IEndDragHandler{//前两个是鼠标显示信息框的,后两个是物品拖拽的
    public static Action OnEnter;//定义静态事件
    public static Action OnExit;//定义静态事件

    public void OnPointerEnter(PointerEventData evenData){//鼠标进入UI调用该方法
        if (evenData.pointerEnter.tag == "Grid") {
            if(OnEnter !=null){
                OnEnter (transform);//传参
                Debug.Log("进入");
            }
        }
    }
    public void OnPointerExit(PointerEventData evenData){//鼠标离开UI调用该方法
        if (evenData.pointerEnter.tag == "Grid") {
            if(OnExit !=null){
                OnExit ();
                Debug.Log ("离开");
            }
        }
    }



    public static Action OnLeftBeginDrag;//左键开始拖拽
    public static Action OnLeftEndDrag;//左键结束拖拽(当前自己的,拖拽后的)

    public void OnBeginDrag(PointerEventData evenData){
        if(evenData.button == PointerEventData.InputButton.Left){//左键按下
            if(OnLeftBeginDrag !=null){
                OnLeftBeginDrag (transform);//调用事件
            }
        }
    }
    public void OnDrag(PointerEventData evenData){

    }
    public void OnEndDrag(PointerEventData evenData){
        if(evenData.button == PointerEventData.InputButton.Left){//左键按下结束
            if(OnLeftEndDrag !=null){
                if(evenData.pointerEnter == null)//没放到UI上
                    OnLeftEndDrag (transform, null);//调用事件
                else
                    OnLeftEndDrag (transform,evenData.pointerEnter.transform);


            }
        }
    }
}

UGUI背包(二)鼠标移动会显示信息框,拖拽物品_第1张图片

做UI界面要注意,因为信息框是随着信息的多少而放大缩小的

这里写图片描述

这里要注意,tooltip和Content都是text

UGUI背包(二)鼠标移动会显示信息框,拖拽物品_第2张图片

using UnityEngine;
using System.Collections;
using System.Collections.Generic;//list,泛型的命名空间
using System.Text;//StringBuilder
//背包的所有操作,存,取,扔
public class KnapsackManager : MonoBehaviour {
    private static KnapsackManager _instance;//单例模式,全局调用和访问
    public static KnapsackManager Instance{ get{ return _instance;}}

    public Dictionary<int,Item> ItemList ;//定义字典,
    public DragItemUI DragItemUI;
    public GridPanelUI GridPanelUI;//获取组件
    private bool isShow = false;//是否显示
    private bool isDrag = false;//是否拖拽
    public ToolTipsUI ToolTipsUI;//信息框
    void Awake(){
        //赋值
        _instance = this;
        //数据
        Load ();
        //事件
        GridUI.OnEnter +=GridUI_OnEnter;
        GridUI.OnExit += GridUI_OnExit;



        GridUI.OnLeftBeginDrag += GridUI_OnLeftBeginDrag;
        GridUI.OnLeftEndDrag += GridUI_OnLeftEndDrag;
    }
    void Update(){//信息框出现在鼠标下面
        Vector2 position;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(GameObject.Find("KnapsackUI").transform as RectTransform, Input.mousePosition, null, out position);

        if(isDrag){
            DragItemUI.Show ();
            DragItemUI.SetLocalPosition(position);
        }
        else if (isShow) {
            ToolTipsUI.Show ();
            ToolTipsUI.SetLocalPosition(position);
        }
    }
    private void GridUI_OnEnter(Transform gridTransform){//事件回调(进入)
        Item item = ItemModel.GetItem (gridTransform.name);//得到保存的数据
        if (item == null)
            return;
        string text = GetTooltipText (item);
        ToolTipsUI.UpdateTooltip (text);//不是空,显示item
        Debug.Log(item.Name);
        isShow = true;
    }

    private  void GridUI_OnExit(){//事件回调(退出)
        isShow = false;
        ToolTipsUI.Hide();
        Debug.Log ("退出");
    }



    private void GridUI_OnLeftBeginDrag(Transform gridTransform){//开始拖拽
        if (gridTransform.childCount == 0)
            return;
        else {
            Item item = ItemModel.GetItem (gridTransform.name);//获取信息

            DragItemUI.UpdateItem(item.Name);
            Destroy (gridTransform.GetChild(0).gameObject);//拖拽后原来地方的字体消失
            //ItemModel.DeleteItem(gridTransform.name);//删除数据
            isDrag = true;
        }
    }

    private void GridUI_OnLeftEndDrag(Transform preTransform, Transform enterTransform){
        isDrag = false;
        DragItemUI.Hide ();
        if(enterTransform == null){//扔东西
            ItemModel.DeleteItem (preTransform.name);
            Debug.Log ("你已经扔掉了");
        }

        else if (enterTransform.tag == "Grid") {//拖到另一个或者当前格子里
            if (enterTransform.childCount == 0) {//直接扔进去
                Item item = ItemModel.GetItem(preTransform.name);//获取数据
                this.CreateNewItem(item,enterTransform);
                //删除原来的数据,保存现在的数据
                ItemModel.DeleteItem (preTransform.name);
            } else 
            {//交换
                //删除原来的物品
                Destroy(enterTransform.GetChild(0).gameObject);
                //获取数据
                Item prevGirdItem = ItemModel.GetItem (preTransform.name);
                Item enterGridItem = ItemModel.GetItem (enterTransform.name);//进入的格子的信息
                //交换两个物体
                this.CreateNewItem(prevGirdItem,enterTransform);
                this.CreateNewItem (enterGridItem,preTransform);
            }
        } else {//拖到UI其他地方,还原
            Item item = ItemModel.GetItem(preTransform.name);
            this.CreateNewItem(item,preTransform);
        }
    }
    private string GetTooltipText(Item item){//得到显示的文字
        if (item == null)
            return "";
        StringBuilder sb = new StringBuilder ();
        sb.AppendFormat ("{0}\n\n",item.Name);
        switch(item.ItemType){
        case "Armor":
            Armor armor = item as Armor;
            sb.AppendFormat ("力量:{0}\n防御:{1}\n敏捷:{2}\n\n", armor.Power, armor.Defend, armor.Agility);//追加字符串
            break;
        case "Consumable":
            Consumable consumable = item as Consumable;
            sb.AppendFormat ("HP:{0}\nMP:{1}\n\n", consumable.BackHP,consumable.BackMP);
            break;
        case "Weapon":
            Weapon weapon = item as Weapon;
            sb.AppendFormat ("攻击:{0}\n\n",weapon.gongjili);
            break;
        default:
            break;
        }
        sb.AppendFormat ("购买价格:{0}\n出售价格:{1}\n\n描述:{2}", item.BUYPrice, item.SELLPrice, item.Description);
        return sb.ToString();
    }
    public void StoreItem(int itemId){//存储物品

        if(!ItemList.ContainsKey(itemId)){//当前字典没有这个ID

            return;
        }
        Transform emptygrid = GridPanelUI.GetEmptyGrid ();//取到一个格子
        if(emptygrid == null){//取到的格子是空的,表示背包满了
            Debug.LogWarning ("背包满了");
            return;
        }

        Item temp = ItemList[itemId];//有这个ID,就获取到了信息(取出来了item)

        this.CreateNewItem (temp,emptygrid);

    }
    private void Load(){//模拟取数据的过程,加载
        ItemList = new Dictionary<int,Item> ();//声明list集合,

        Weapon w1 = new Weapon (0,"牛刀","厉害的刀",20,10,"",100);
        Weapon w2 = new Weapon (1,"羊刀","杀羊刀",15,10,"",200);

        Consumable c1 = new Consumable (2,"红瓶","加血",25,11,"",29,50);
        Consumable c2 = new Consumable (3,"蓝瓶","加蓝",30,50,"",40,39);
        Consumable c3 = new Consumable (4,"大红瓶","加血",25,11,"",20,49);
        Consumable c4 = new Consumable (5,"大蓝瓶","加蓝",30,50,"",40,38);

        Armor a1 = new Armor (6,"头盔","保护脑袋",120,83,"",5,40,1);
        Armor a2 = new Armor (7,"护肩","保护肩膀",130,68,"",5,4,8);

        ItemList.Add(w1.ID,w1);
        ItemList.Add(w2.ID,w2);
        ItemList.Add(c1.ID,c1);
        ItemList.Add(c2.ID,c2);
        ItemList.Add(c3.ID,c3);
        ItemList.Add(c4.ID,c4);
        ItemList.Add(a1.ID,a1);
        ItemList.Add(a2.ID,a2);
    }
    private void CreateNewItem(Item item,Transform parent){
        GameObject itemPrefab =  Resources.Load ("Prefabs/Item");//创建Item,动态加载
        itemPrefab.GetComponent ().UpdateItem (item.Name);//更新显示
        GameObject itemGO = GameObject.Instantiate(itemPrefab);//实例化出来,(加载和实例化不同)
        itemGO.transform.SetParent(parent);
        //位置和缩放初始化
        itemGO.transform.localPosition = Vector3.zero;
        itemGO.transform.localScale = Vector3.one;
        ItemModel.StoreItem (parent.name,item);//信息存储
    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ToolTipsUI : MonoBehaviour {
    public Text OutLineText;//边框
    public Text ContentText;//显示内容
    public void UpdateTooltip(string text){//更新UI的介绍
        OutLineText.text = text;
        ContentText.text = text;
        Debug.Log ("刷新");
    }


    //显示和隐藏
    public void Show(){
        gameObject.SetActive (true);
        Debug.Log ("显示");

    }
    public void Hide(){
        gameObject.SetActive (false);
        Debug.Log ("隐藏");
    }
    public void SetLocalPosition(Vector2 position){
        transform.localPosition = position;
    }
}

你可能感兴趣的:(unity3d)