18NGUI之背包系統(tǒng)

NGUI背包系統(tǒng)的制作##

效果展示

一、背包系統(tǒng)的搭建##

背包格子的搭建

二、格子里面物品(預(yù)制物的制作)##

  1. 格子里面的物體需要實(shí)現(xiàn)可以在格子里面拖拽,如果是空的格子可以替換位子,如果有另外一個(gè)物品需要實(shí)現(xiàn)交換物品,如果是拉在間隙部分,或者背包系統(tǒng)以外就回歸到原來(lái)位子。
    2.數(shù)量Label的位置,可以坐在物體上,也可以放在格子上。
預(yù)制物的制作
預(yù)制物上的腳本:
public class DragTest : UIDragDropItem {

    public UISprite sprite;
    public UILabel label;
    int num = 1;
    public void AddCount(int number=1)
    {
        num += number;
        label.text = num + "";
    }



    protected override void OnDragDropRelease(GameObject surface)
    {
        base.OnDragDropRelease(surface);
        //print(surface);
        if (surface.tag == "cell"||surface.tag==null)
        {
            this.transform.parent = surface.transform;
            transform.localPosition = Vector3.zero;
        }
        else if (surface.tag == "UIRoot" || surface.tag == null)
        {
            transform.localPosition = Vector3.zero;
        }
        else//必須給物體添加tag值不然的話。。。拖拽到window外面去的話就會(huì)報(bào)錯(cuò)。。。。
        {
            Transform temp = surface.transform.parent;
            surface.transform.parent = transform.parent;
            transform.parent = temp;
            surface.transform.localPosition = Vector3.zero;
            transform.localPosition = Vector3.zero;
        }

    }
}


三、對(duì)格子及其物品的管理##

對(duì)格子的管理
腳本控制:
public class KnapsackAdd : MonoBehaviour
{

    public GameObject[] cells;

    public GameObject item;
    public string[] equpimentsName;//承儲(chǔ)物品的名字


    void Start()
    {
        

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.X))
        {
            PickUp();
        }
    }

    void PickUp()
    {
        int random = Random.Range(0, equpimentsName.Length);
        string name = equpimentsName[random];
        bool isFind = false;
        for (int i = 0; i < cells.Length; i++)
        {
            if (cells[i].transform.childCount > 0)
            {
                DragTest testDrage = cells[i].transform.GetComponentInChildren<DragTest>();
                if (testDrage.sprite.spriteName == name)
                {
                    testDrage.AddCount();
                    isFind = true;
                    break;
                }
            }
        }
        if (isFind == false)
        {
            for (int i = 0; i < cells.Length; i++)
            {
                if (cells[i].transform.childCount==0)
                {
                    GameObject go = NGUITools.AddChild(cells[i], item);
                    go.GetComponent<UISprite>().spriteName = name;
                    go.transform.localPosition = Vector3.zero;
                    break;
                }
                
            }
        }

    }

}

四、總結(jié)難點(diǎn)##

1.進(jìn)行交互的物體,必須添加boxcollider;
2.實(shí)現(xiàn)在不同的腳本上獲取需要的腳本的信息;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容