天天看點

UGUI背包系統(初級)

效果圖放上(不斷點選鍵盤G随機擷取物品):

UGUI背包系統(初級)

此篇是接着上篇背包前奏來寫,拖拽和射線檢測部分就不再多說,包括後面的修改加上了交換、回初始位置等功能都是在那個基礎上的擴充,各有所需各自想要的擴充也就不同,大家可以根據上一篇來自己擴充。 該篇主要來介紹一下背包的拾取功能 1、建立一個image用來作為背景,命名為Beibao,比如說如下圖:

UGUI背包系統(初級)

2、在Beibao下添加一個空對象,命名為Grid,并為其添加Gridlayout Group元件,調整大小,符合背景。 3、在Grid下建立一個image,命名為UCell,将其做成prefab,然後做滿整個Grid,當然,數量自己控制,如下圖:

UGUI背包系統(初級)

4、為UCell做一個子對象,命名為UItem,UItem下包含一個text,也做成prefab。當然,大小要自己設定好,至少不能比UCell大。 5、準備幾個圖檔資源,建立一個Resources檔案夾,放到下面,内容自由發揮吧,比如下圖:

UGUI背包系統(初級)

6、開始寫腳本: 建立一個C#腳本,命名為UPickup,關鍵代碼如下:

usingUnityEngine;
usingSystem.Collections;
usingUnityEngine.UI; 
 
publicclass UPickup : MonoBehaviour {
 
        publicGameObject[] cells;
        publicGameObject instantiate;
        GameObject item;
 
        AudioClip Musics;
        Image[] Images;
        Image Imagesingle;
 
        Text index;
        intIndexInt = 0;
        stringIndexStr = "";
        intRandomM = 0;
        stringRandomStrInt = "";
        stringRandomStr = "";
 
        // Use this for initialization
        voidStart () {
 
        }
         
        // Update is called once per frame
        voidUpdate () {
         
                if(Input.GetKeyDown (KeyCode.G)) {
                        Pickup ();
                }
 
        }
 
        publicvoid Pickup(){
                 
                boolisFind = false;
 
                RandomM = Random.Range (0,6);
                RandomStrInt = RandomM.ToString ();
                RandomStr = "Pictures/"+ RandomStrInt;//路徑
                print (RandomStr);
 
                item = Instantiate (instantiate, transform.position, transform.rotation) asGameObject;//擷取預制物體
                Imagesingle = item.transform.GetComponent<Image>();                                                                          //擷取預制物體的image元件
                Imagesingle.overrideSprite = Resources.Load (RandomStr, typeof(Sprite))asSprite;           //動态加載image元件的sprite
 
                for(inti = 0; i < cells.Length; i++) {
                        if(cells [i].transform.childCount > 0) {//判斷目前格子是否有物體
                                //如果有,并且一樣的
                                if(Imagesingle.overrideSprite.name == cells [i].transform.GetChild (0).transform.GetComponent<Image>().overrideSprite.name) {
                                        //判斷的是image加載圖檔的名字
                                        isFind = true;
 
                                        index = cells [i].transform.GetChild (0).transform.GetChild(0).GetComponent<Text>();
                                        IndexInt = int.Parse(index.text);
                                        IndexInt += 1;
                                        IndexStr = IndexInt.ToString();
                                        index.text = IndexStr;
                                        Destroy (item);
 
                                }
                        }
                }
                if(isFind == false) {
                        for(inti = 0; i < cells.Length; i++) {
                                if(cells [i].transform.childCount == 0) {
                                        //目前沒有物體,則添加
                                        item.transform.SetParent(cells[i].transform);
                                        item.transform.localPosition = Vector3.zero;
                                        break;
                                }
                        }
                }
        }
 
}
           

解釋一下,該函數實作的功能有動态随機加載UItem圖檔、判斷目前格子是否有物品,有就加數目,沒有就添加新物品,邏輯結構應該還算清晰。 7、把腳本挂載在Grid上,并配好,如下圖:

UGUI背包系統(初級)

8、添加兩個Tag分别為UCell和UItem

UGUI背包系統(初級)

将UCell元件的Tag設為UCell,将UItem的Tag設為UItem。用作代碼中作判斷。 到此差不多做好了,拖拽不用多少,看過上一篇的應該都明白,有什麼問題盡管問。

UGUI背包系統(初級)

我把工程打了個小包,看看吧,最近有點小忙,沒時間擴充了,其實還有很多地方可以擴充,比如說分頁,寫接口,用對象池等,本想寫好了再放,這等你們自己擴充吧,導入應該就能用,如果有問題可能是标簽沒設定,設定好uitem和ucell選擇相對應的就行

繼續閱讀