这个简易的背包系统可以实现的功能有:
1、背包的打开、关闭与拖拽
2、背包中物品数据的存储
3、背包中物体的显示、移动、交换
4、背包数据库的实时更新
以下是这个背包系统的所以源码
1、实现背包的开关:
void OpenMyBag()
{
isOpen = MyBag.activeSelf;//将MyBag的active状态赋给isOPen
if (Input.GetKeyDown("b"))
{
isOpen = !isOpen;
MyBag.SetActive(isOpen);
}
}
2、GoodsItem与Inventory
[CreateAssetMenu(fileName ="New GoodsItem",menuName ="Inventory/New GoodItem")]
public class GoodsItem : ScriptableObject
{
public string itemName;
public Sprite itemSprite;
public int itemHeld;
[TextArea]
public string itemInfo;
public bool equip;
}
[CreateAssetMenu(fileName ="New Inventory",menuName ="Inventory/New Inventory")]
public class Inventory : ScriptableObject
{
public List<GoodsItem> GoodsItems = new List<GoodsItem>();
}
3、GoodsOnWorld与InventoryManage
public class GoodsOnWorld : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Ruby"))
{
AddNewItem();
Destroy(gameObject);
}
}
public GoodsItem thisItem;
public Inventory thisInventory;
public void AddNewItem()
{
if (!thisInventory.GoodsItems.Contains(thisItem))
{
thisItem.itemHeld=1;
//thisInventory.GoodsItems.Add(thisItem);
//thisItem.equip = true;
for (int i=0;i<thisInventory.GoodsItems.Count;i++)
{
if(thisInventory.GoodsItems[i]==null)
{
thisInventory.GoodsItems[i] = thisItem;
break;
}
}
}
else
{
thisItem.itemHeld++;
}
InventoryManage.RefreshItem();
}
}
public class InventoryManage : MonoBehaviour
{
static InventoryManage instance;
public Inventory MyBag;
public GameObject slotGrids;
//public Slot slotPrefab;
public GameObject emptySlot;
public Text ItemInfo;
public List<GameObject> slots = new List<GameObject>();
private void OnEnable()
{
RefreshItem();
instance.ItemInfo.text = "";
}
public static void UpdateItemInfo(string ItemDescription)
{
instance.ItemInfo.text = ItemDescription;
}
private void Awake()
{
if (instance != null) Destroy(this);
instance = this; //保证单例的唯一性
}
/*public static void CreatNewItem(GoodsItem goodsItem)
{
Slot newItem =
Instantiate(instance.slotPrefab, instance.slotGrids.transform.position,
Quaternion.identity);
//将newItem的位置创建在slotGrids的子级位置上,SetParent:设为父级
newItem.gameObject.transform.SetParent(instance.slotGrids.transform);
//将实例化出来的newItem的物品及其图片替换为传入参数goodsItem的物品及其图片
newItem.slotItem = goodsItem;
newItem.slotImage.sprite = goodsItem.itemSprite;
newItem.slotNum.text = goodsItem.itemHeld.ToString();
}*/
public static void RefreshItem()
{
for (int i = 0; i < instance.slotGrids.transform.childCount; i++)
{
if (instance.slotGrids.transform.childCount == 0) break;
Destroy(instance.slotGrids.transform.GetChild(i).gameObject);
instance.slots.Clear();
}
for (int i = 0; i < instance.MyBag.GoodsItems.Count; i++)//在Ruby与物品碰撞时已经在列表中添加了
{
//CreatNewItem(instance.MyBag.GoodsItems[i]);//把列表GoodsItems的物品创造在slot上
instance.slots.Add(Instantiate(instance.emptySlot)); //Instantiate实例化出来的物体类型为GameObject
instance.slots[i].transform.SetParent(instance.slotGrids.transform);
instance.slots[i].GetComponent<Slot>().slotID = i;
instance.slots[i].GetComponent<Slot>().SetupSlot(instance.MyBag.GoodsItems[i]);
}
}
}
4、Slot
public class Slot : MonoBehaviour
{
public int slotID;
public GoodsItem slotItem;
public Image slotImage;
public Text slotNum;
public string slotInfo;
public GameObject itemInSlot;
public void ItemOnClick()
{
InventoryManage.UpdateItemInfo(slotInfo);
}
public void SetupSlot(GoodsItem item)
{
if (item == null)
{
itemInSlot.SetActive(false);
return;
}
slotImage.sprite = item.itemSprite;
slotNum.text = item.itemHeld.ToString();
slotInfo = item.itemInfo;
}
}
5、ItemOnDrag
public class ItemOnDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public Transform originalParent;
public Inventory myBag;
public int currentItemID;
public void OnBeginDrag(PointerEventData eventData)
{
originalParent = transform.parent;
currentItemID = originalParent.GetComponent<Slot>().slotID;
transform.SetParent(transform.parent.parent);
transform.position = eventData.position;
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
transform.position = eventData.position;
Debug.Log(eventData.pointerCurrentRaycast.gameObject.name);
}
public void OnEndDrag(PointerEventData eventData)
{
if (!eventData.pointerCurrentRaycast.gameObject)
{
transform.SetParent(originalParent);
transform.position = originalParent.position;
GetComponent<CanvasGroup>().blocksRaycasts = true;
return;
}
if (eventData.pointerCurrentRaycast.gameObject.name!= null)
{
if (eventData.pointerCurrentRaycast.gameObject.name == "ItemImage")
{
transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform.parent.parent);
transform.position = eventData.pointerPressRaycast.gameObject.transform.parent.parent.position;
var temp = myBag.GoodsItems[currentItemID];
myBag.GoodsItems[currentItemID] = myBag.GoodsItems[eventData.pointerCurrentRaycast.gameObject.
GetComponentInParent<Slot>().slotID];
myBag.GoodsItems[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotID]
= temp;
eventData.pointerCurrentRaycast.gameObject.transform.parent.position = originalParent.position;
eventData.pointerCurrentRaycast.gameObject.transform.parent.SetParent(originalParent);
GetComponent<CanvasGroup>().blocksRaycasts = true;
return;
}
if (eventData.pointerCurrentRaycast.gameObject.name == "slot(Clone)")
{
transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform);
transform.position = eventData.pointerCurrentRaycast.gameObject.transform.position;
myBag.GoodsItems[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotID]
= myBag.GoodsItems[currentItemID];
if (eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotID != currentItemID)
myBag.GoodsItems[currentItemID] = null;
GetComponent<CanvasGroup>().blocksRaycasts = true;
return;
}
}
transform.SetParent(originalParent);
transform.position = originalParent.position;
GetComponent<CanvasGroup>().blocksRaycasts = true;
}
}
6、MoveBag
public class MoveBag : MonoBehaviour,IDragHandler
{
RectTransform currentRect;
void Awake()
{
currentRect = GetComponent<RectTransform>();
}
public void OnDrag(PointerEventData eventData)
{
currentRect.anchoredPosition += eventData.delta; //anchoredPosition(锚点) eventData.delta(鼠标移动的轻微的值、返回上一帧鼠标移动位置)
}
}
后续我将基于这个基础。完善这个背包,给它添加很多功能。