1. 程式人生 > >UGUI揹包(一)

UGUI揹包(一)

這裡寫圖片描述
實現的功能:滑鼠中鍵點選,隨機獲取裝備名稱,滑鼠移到裝備上,自動彈出裝備的詳細資訊,移開自動消失,並且實現了拖拽功能(拖到區域外,預設刪除,拖到有裝備的預設交換)

首先第一步是 建立資料類,裝備分類,有些屬性是共有的,所以建立一個子類,然後不同的裝備分別繼承它

子類程式碼如下

using UnityEngine;
using System.Collections;
//共有的資料類
public class Item  {

    public int ID{ get; private set;}
    public string Name{ get; private set;}
    public
string Description{ get; private set;} public int BUYPrice{ get; private set;} public int SELLPrice{ get; private set;} public string Icon{ get; private set;} public string ItemType{ get; protected set;}//道具分類用字串或者列舉都可以,protected保證子類可以設定它 public Item(int id,string name,string description,int
buyprice,int sellprice,string icon){ this.ID = id; this.Name = name; this.Description = description; this.BUYPrice = buyprice; this.SELLPrice = sellprice; this.Icon = icon; } }

這裡我分成了三類裝備:武器,血瓶,防具

using UnityEngine;
using System.Collections;

public
class Weapon : Item { public int gongjili{ get; private set;} public Weapon(int id,string name,string description,int buyprice,int sellprice,string icon,int gongji):base(id,name,description,buyprice,sellprice,icon){ this.gongjili = gongji; base.ItemType = "Weapon"; } }
using UnityEngine;
using System.Collections;
//消耗品(回覆血量,回覆藍)
public class Consumable : Item {
    public int BackHP{ get;  private set;}
    public int BackMP{ get; private set;}
    public Consumable(int id,string name,string description,int buyprice,int sellprice,string icon,int backhp,int backmp):base(id,name,description,buyprice,sellprice,icon){
        this.BackHP = backhp;
        this.BackMP = backmp;
        base.ItemType = "Consumable";
    }

}
using UnityEngine;
using System.Collections;
//防具類
public class Armor : Item {

    public int Power{ get; private set;}//力量
    public int Defend{get;private set;}//防禦力
    public int Agility{get; private set;}//敏捷


    public Armor(int id,string name,string description,int buyprice,int sellprice,string icon,int power,int defend,int agility):base(id,name,description,buyprice,sellprice,icon){

        this.Power = power;
        this.Defend = defend;
        this.Agility = agility;
        base.ItemType = "Armor";

    }
}

到此為止,資料類就算寫完了

製作好UI介面,然後開始資料的儲存,

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//資料的儲存
public  class ItemModel  {//定義靜態方法
    private static Dictionary<string,Item> GridItem = new Dictionary<string,Item> ();//string是每個格子的名字,
    public static void StoreItem(string name,Item item){//儲存資料
        if (GridItem.ContainsKey (name))//已經有了,就刪除再新增
            DeleteItem (name);
        GridItem.Add (name,item);   
        Debug.Log ("儲存");
    }

    public static Item GetItem(string name ){//取出資料
        if (GridItem.ContainsKey (name)) {
            Debug.Log ("獲取");
            return GridItem [name];
        }
        else
            return null;
    }

    public static void DeleteItem(string name){
        if (GridItem.ContainsKey (name))
            GridItem.Remove (name);
    }
}

點選鍵,隨機獲取一個裝備;首先要判斷格子是空的,這樣才可以掛裝備的名字(圖片)

using UnityEngine;
using System.Collections;
//檢視層指令碼
public class GridPanelUI : MonoBehaviour {
    public Transform[] Grids;//陣列
    public Transform GetEmptyGrid(){//定義一個方法,獲取格子
        for(int i = 0;i<Grids.Length;i++){//判斷是否為空
            if (Grids [i].childCount == 0)
                return Grids[i];
        }
        return null;//找不到
    }

}
using UnityEngine;
using System.Collections;

public class InputDetor : MonoBehaviour {//監聽點選事件
    void Update () {//模擬獲取到一個物品
        if(Input.GetMouseButtonDown(2)){//0.滑鼠左鍵,1,滑鼠右鍵,2,滑鼠中鍵
            int index = Random.Range (0,8);//隨機數確定索引

            KnapsackManager.Instance.StoreItem(index);
        }
    }
}

從儲存的資料中獲取它的名字

using UnityEngine;
using System.Collections;
using UnityEngine.UI;//
public class ItemUI : MonoBehaviour {
    //public Image ItemImage;
    public Text ItemName;//道具的名字
    public void  UpdateItem(string name){
        ItemName.text = name;
    }

//  public void  UpdateItem(Sprite s){
//      //Resources.Load ();//動態載入圖片,傳遞的是路徑
//
//      ItemImage.sprite = s;
//  }
}
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<GameObject> ("Prefabs/Item");//建立Item,動態載入
        itemPrefab.GetComponent<ItemUI> ().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);//資訊儲存
    }