기본 정렬만 하는 인벤토리 시스템은 구현은 몇번했는데 이번에 만드는 게임이 스타듀밸리, 마인크래프트류의 드래그 드롭 이벤트를 통해 인벤을 정리하고 정렬하는 기능이 있어서 이번에 구현했다.
지난번에 비슷하게 슬릇을 드래그하는 기능을 했었을땐 뭔가 어설프게 구현했는데 이번엔 그때의 경험도있고 많은것을 배워서 좀더 원활하게 구현한것 같다.
코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UI_Inventory : MonoBehaviour
{
public GameObject inventoryPanel;
public List<UI_ItemSlot> slots = new List<UI_ItemSlot>();
public GameObject[] playerInventory;
private void Awake()
{
playerInventory = new GameObject[10];
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
ToggleInventory();
}
}
public void AddItem(GameObject go)
{
for (int i = 0; i < playerInventory.Length; i++)
{
if (playerInventory[i] == null)
{
playerInventory[i] = go;
return;
}
}
return;
}
public void SwapInventory(int from, int to)
{
playerInventory.ArraySwap(from, to);
}
public void ToggleInventory()
{
if (!inventoryPanel.activeSelf)
{
inventoryPanel.SetActive(true);
Refresh();
}
else
{
inventoryPanel.SetActive(false);
}
}
void Refresh()
{
for (int i = 0; i < slots.Count; i++)
{
if (playerInventory[i] != null)
slots[i].SetItem(playerInventory[i]);
}
}
}
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
public static class Extension
{
public static void ArraySwap<T>(this T[] array, int from, int to)
{
T tmp = array[from];
array[from] = array[to];
array[to] = tmp;
}
}
using UnityEngine.UI;
public class UI_DragSlot : UI_Base
{
static public UI_DragSlot instance;
public UI_ItemSlot itemSlot;
public Image dragImage;
enum Images
{
testImage,
}
public override void Init()
{
Bind<Image>(typeof(Images));
instance = this;
dragImage = GetImage((int)Images.testImage);
ResetDragSlot();
}
public void ResetDragSlot()
{
dragImage.color = Util.SetColorAlpha(dragImage, 0);
dragImage.sprite = null;
}
public void SetDragSlot(Image _image, float alpha)
{
dragImage.sprite = _image.sprite;
dragImage.color = Util.SetColorAlpha(dragImage, alpha);
}
}
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class UI_ItemSlot : UI_Base, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler
{
public UI_Inventory inventory;
public int index;
public Image myImage;
enum Images
{
testImage,
}
public override void Init()
{
Bind<Image>(typeof(Images));
myImage = GetImage((int)Images.testImage);
}
public void OnBeginDrag(PointerEventData eventData)
{
if (myImage.sprite != null)
{
UI_DragSlot.instance.itemSlot = this;
UI_DragSlot.instance.SetDragSlot(myImage, 1);
UI_DragSlot.instance.transform.position = eventData.position;
}
}
public void OnDrag(PointerEventData eventData)
{
if (myImage.sprite != null)
{
UI_DragSlot.instance.transform.position = eventData.position;
}
}
public void OnDrop(PointerEventData eventData)
{
if (UI_DragSlot.instance.itemSlot != null)
{
ChangeSlot();
}
}
public void OnEndDrag(PointerEventData eventData)
{
UI_DragSlot.instance.ResetDragSlot();
UI_DragSlot.instance.itemSlot = null;
}
private void ChangeSlot()
{
//data is null
if (myImage.sprite == null)
{
Image image = UI_DragSlot.instance.dragImage;
myImage.sprite = image.sprite;
myImage.color = Util.SetColorAlpha(image, 1);
UI_DragSlot.instance.itemSlot.myImage.color = Util.SetColorAlpha(image, 0);
UI_DragSlot.instance.itemSlot.myImage.sprite = null;
}
// data is not null
else
{
Image image = UI_DragSlot.instance.dragImage;
UI_DragSlot.instance.itemSlot.myImage.sprite = myImage.sprite;
myImage.sprite = image.sprite;
}
inventory.SwapInventory(index, UI_DragSlot.instance.itemSlot.index);
}
public void SetItem(GameObject go)
{
if (myImage.sprite == null)
{
myImage.sprite = go.GetComponentInChildren<SpriteRenderer>().sprite;
myImage.color = Util.SetColorAlpha(myImage, 1);
}
}
public void SetEmpty()
{
myImage.sprite = null;
myImage.color = Util.SetColorAlpha(myImage, 0);
}
}