背包系统分三个层次   背包整体——背包网格——背包物品,其中物品是用代码实例化出来的。

unity 依赖包在哪里 unity背包系统代码_unity 依赖包在哪里

unity 依赖包在哪里 unity背包系统代码_Image_02

 

目录

背包类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Bag : MonoBehaviour
{
    public static Bag _instance;
    private int coinCount = 1000;//当前背包内有多少的金钱
    public Text coin;//用文本来显示金钱数字
    public List<BagItemGird> ItemGirdList = new List<BagItemGird>();
    //这是背包网格列表,表示这个背包可以存放多少不同的物品

    public BagItem bagItem;//新建一个背包物品类
    private void Awake()
    {
        _instance = this;
    }
    // Start is called before the first frame update
    void Start()
    {
        coin.text = coinCount.ToString();//显示金币数    
    }
    public void GetId(int id)//当有物品要放入背包的时候得到他的id并且显示
    {
        BagItemGird grid = null;//grid用来标记哪个网格可以存放东西
        foreach (BagItemGird temp in ItemGirdList)
        {//对背包网格进行循环找到可以存放物品的网格
            //如果背包要放入的物品的id和背包内某个网格的id一样(背包网格类具有id属性,看下文)那么就把这个物品的数量加一
            if (temp.id == id)
            {
                grid=temp;
                break;
            }
        }
        if (grid != null)//如果存在这么一个网格,物品数量加一
        {
            BagItem item=grid.transform.GetComponentInChildren<BagItem>();
            item.PlusNubmer();//这是背包物品类的一个方法 用来增加物品数量
        }
        else
        {
            //如果该物品在背包内没有相似的,查找一个空的网格放入
            foreach (BagItemGird temp in ItemGirdList)
            {
                if (temp.id == 0)//网格的id为0就表示该网格为空
                {
                    grid = temp;
                    break;
                }
            }
            if (grid != null)//如果有这么一个网格 他的id=0就把物品放入该网格
            {
                bagItem = (BagItem)Instantiate(bagItem, Vector3.zero, transform.rotation);//实例化了一个背包物品
                bagItem.transform.parent = grid.transform;//将背包物品变成网格的子对象
                bagItem.transform.localPosition = Vector3.zero;//设置背包物品的位置为0
                grid.id = id;//把物品id赋值给网格id表示该网格有物品
                bagItem.SetId(id);//这是对物品进行初始化,详情看下文的BagItem类说明
            }
        }

    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.X))
        {
            GetId(Random.Range(1001, 1004));
        }
    }
}

网格类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BagItemGird : MonoBehaviour
{
    public int id = 0;
    private bool isItem = false;
    // Start is called before the first frame update
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}

物品类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class BagItem :MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
{
    public int id = 0;//表明物品id
    private int num = 0;//物品数量
    private Text itemnum;//显示物品数量的文本
    private ObjectInfo info = null;//物品信息
    private BagItemGird parent;//该背包物品的网格
    private Image image;//背包物品显示的图片
    Vector3 CIPosition;//要交换位置的物品位置
    // Start is called before the first frame update
    void Awake()
    {
        image = this.GetComponent<Image>() as Image;
        itemnum = this.GetComponentInChildren<Text>();
    }
    void Start()
    {
    }
    public void SetId(int id, int num = 1)
    {

        this.id = id;//把id赋值给该类的id
        info = ObjectsInfo._instance.GetObjectInfoById(id);//获得该id的信息
        this.SetIconName(info.icon_name);//这个函数用来获得物品名称
        SetImg();//该函数用来设置图片信息
        itemnum.gameObject.SetActive(true);//设置为可见
        this.num = num;
        itemnum.text = num.ToString();//显示数量
    }
    public void SetImg()
    {
        string path = "RPG/GUI/Icon/" + info.icon_name;//设置图片的路径 必须要放在resources里面才可以
        image.sprite = Resources.Load(path, typeof(Sprite)) as Sprite;//将该路径的图片导入到image中
    }
    public void PlusNubmer(int num = 1)
    {
        //增加物品数量
        this.num += 1;
        itemnum.text = this.num.ToString();
    }
    public void ClearInfo()
    {
        //清除物品
        id = 0;
        info = null;
        num = 0;
        GameObject.Destroy(this,0);
    }
    // Update is called once per frame
    void Update()
    {

    }

    public void SetIconName(string icon_name)
    {
        image.name = icon_name;
    }
    //拖拽功能的实现
    public void OnBeginDrag(PointerEventData eventData)
    {
        CIPosition = eventData.position;
        transform.position = eventData.position;
    }

    public void OnDrag(PointerEventData eventData)
    {
        transform.position = eventData.position;
        transform.GetComponent<Image>().raycastTarget = false;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        GameObject go = eventData.pointerCurrentRaycast.gameObject;
        Transform Cparent;
        if (go.tag == TagManage.bagItem && go.transform.parent != transform.parent)
        {
            Cparent = transform.parent;
            transform.parent = go.transform.parent;
            go.transform.parent = Cparent;
            transform.localPosition = Vector3.zero;
            go.transform.localPosition = Vector3.zero;
        }
        else if (go.tag == TagManage.bagItemGird)
        {
            transform.parent = go.transform;
            transform.localPosition = Vector3.zero;
        }
        else
        {
            transform.position = CIPosition;
        }
        transform.GetComponent<Image>().raycastTarget = true;
    }

}