使用的Unity2018版本和c#。
素材链接,里面有杂项文件,需要自己清理清理
链接: https://pan.baidu.com/s/1o7_RW_QQ1rrAbDzT69ApRw 提取码: 8s95
先搭建Ui
先创建一个Item类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Item",menuName = "Inventory/New Iten")]
public class Item : ScriptableObject {
public string itemName;
public Sprite itemImage;
public int itemHeld;
[TextArea]
public string itemInfo;
public bool Equp;
}
在这里创建三个
在创建一个Inventory类声明
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/New Inventory")]
public class Inventory : ScriptableObject {
public List<Item> itemList = new List<Item>();
}
和上面两个创建方式一样
创建itemOnWord类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class itemOnWord : MonoBehaviour {
public Item thisItem;
public Inventory playerInventory;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
AddNewItem();
Destroy(gameObject);
}
}
public void AddNewItem()
{
if (!playerInventory.itemList.Contains(thisItem))
{
//playerInventory.itemList.Add(thisItem);
//InventoryManager.CreateNewItem(thisItem);
for (int i = 0; i < playerInventory.itemList.Count; i++)
{
if (playerInventory.itemList[i] == null)
{
playerInventory.itemList[i] = thisItem;
break;
}
}
}
else
{
thisItem.itemHeld += 1;
}
InventoryManager.RefreshItem();
}
}
将代码拖进这个里,并拖入素材
创建一个Slot类,需要引用命名空间using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Slot : MonoBehaviour {
public int slotId;
public Item slotItem;
public Image slotImage;
public Text slotNum;
public string slotInfo;
public GameObject itemInSlot;
public void ItemOnClicked()
{
InventoryManager.UpdateItemInfo(slotInfo);
}
public void SetupSlot(Item item)
{
if (item == null)
{
itemInSlot.SetActive(false);
return;
}
slotImage.sprite = item.itemImage;
slotNum.text = item.itemHeld.ToString();
slotInfo = item.itemInfo;
}
}
拖入物体并赋值
创建InventoryManager 类并引用命名空间using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryManager : MonoBehaviour {
static InventoryManager instance;
public Inventory myBag;
public GameObject slotGrid;
public GameObject emptySlot;
public Text itemInfromation;
public List<GameObject> slots = new List<GameObject>();
void Awake()
{
if (instance != null)
Destroy(this);
instance = this;
}
public void OnEnable()
{
RefreshItem();
instance.itemInfromation.text = "";
}
public static void UpdateItemInfo(string itemDescription)
{
instance.itemInfromation.text = itemDescription;
}
public static void RefreshItem()
{
for (int i = 0; i < instance.slotGrid.transform.childCount; i++)
{
if (instance.slotGrid.transform.childCount == 0)
break;
Destroy(instance.slotGrid.transform.GetChild(i).gameObject);
instance.slots.Clear();
}
for (int i = 0; i < instance.myBag.itemList.Count; i++)
{
instance.slots.Add(Instantiate(instance.emptySlot));
instance.slots[i].transform.SetParent(instance.slotGrid.transform);
instance.slots[i].GetComponent<Slot>().slotId = i;
instance.slots[i].GetComponent<Slot>().SetupSlot(instance.myBag.itemList[i]);
}
}
}
拖入物体并赋值
创建ItemOnDrag 类并引用名命空间using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ItemOnDrag : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler{
public Transform OriginalParent;
public Inventory myBag;
private 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 != null)
{
if (eventData.pointerCurrentRaycast.gameObject.name == "Item Image")
{
transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform.parent.parent);
transform.position = eventData.pointerCurrentRaycast.gameObject.transform.parent.parent.position;
var temp = myBag.itemList[currentItemId];
myBag.itemList[currentItemId] = myBag.itemList[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotId];
myBag.itemList[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.itemList[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotId] = myBag.itemList[currentItemId];
if (eventData.pointerCurrentRaycast.gameObject.GetComponent<Slot>().slotId != currentItemId)
myBag.itemList[currentItemId] = null;
GetComponent<CanvasGroup>().blocksRaycasts = true ;
return;
}
}
transform.SetParent(OriginalParent);
transform.position = OriginalParent.position;
GetComponent<CanvasGroup>().blocksRaycasts = true;
}
}
拖入物体并赋值
创建MoveBag类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class MoveBag : MonoBehaviour,IDragHandler {
public Canvas canvas;
RectTransform currentRect;
public void OnDrag(PointerEventData eventData)
{
currentRect.anchoredPosition += eventData.delta;
}
void Awake()
{
currentRect = GetComponent<RectTransform>();
}
}
拖入物体并赋值
感谢捧场,写的不太好,希望大家理解。
感谢!!!