using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ManualInventory : MonoBehaviour
{
public static ManualInventory instance;
//prefabs
public Transform prefabs;
public List<ManualSlot> manualSlots = new List<ManualSlot>();
public Transform draggingItem;
public ManualSlot enteredSlot;
void Awake()
{
instance = this;
}
void Start()
{
New_SlotMake(10);
//Debug.Log("ManulInventory Start!!!");
}
void Update()
{
if (Input.GetKeyDown("1"))
{
AddItem(ItemDatabase.instance.predict_rollItems);
}
if (Input.GetKeyDown("2"))
{
Init();
}
}
public void GetData(Toggle toggle)
{
Init();
if (toggle.isOn)
{
//Debug.Log("predict_rollItems Init!!!");
AddItem(ItemDatabase.instance.predict_rollItems);
}
else
{
AddItem(ItemDatabase.instance.Guidance_rollItems);
}
}
void AddItem(List<ManualRollItem> items)
{
for (int i = 0; i < items.Count; i++)
{
manualSlots[i].item = items[i];
ItemImageChange(manualSlots[i]);
}
}
public void ItemImageChange(ManualSlot _slot)
{
_slot.item.Seq_NM = _slot.transform.GetSiblingIndex();
if (_slot.item.itemValue == -1)
_slot.transform.GetChild(0).gameObject.SetActive(false);
else
{
_slot.transform.GetChild(0).gameObject.SetActive(true);
_slot.transform.GetChild(0).GetChild(0).gameObject.SetActive(true);
_slot.transform.GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>().text = _slot.item.Seq_NM + "," + _slot.item.Roll_NM;
}
}
void New_SlotMake( int yCount)
{
for (int y = 0; y < yCount; y++)
{
Transform newSlot = Instantiate(prefabs, transform);
newSlot.name = "Slot" + (y + 1);
newSlot.transform.GetChild(0).gameObject.SetActive(false);
manualSlots.Add(newSlot.GetComponent<ManualSlot>());
}
Init();
}
void Init()
{
foreach (var x in manualSlots)
{
x.item = new ManualRollItem();
x.transform.GetChild(0).gameObject.SetActive(false);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class ManualRollItem {
public string Roll_NM;
public int itemValue;
public int Seq_NM;
// Use this for initialization
public ManualRollItem(string _Roll_NM,int _itemValue,int _Seq_NM)
{
Roll_NM = _Roll_NM;
itemValue = _itemValue;
Seq_NM = _Seq_NM;
}
public ManualRollItem()
{
itemValue = -1;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ManualSlot : MonoBehaviour, IDragHandler, IPointerEnterHandler, IPointerExitHandler, IEndDragHandler
{
public ManualRollItem item;
public void OnDrag(PointerEventData data)
{
if (transform.childCount > 0)
{
transform.GetChild(0).parent = ManualInventory.instance.draggingItem;
}
ManualInventory.instance.draggingItem.GetChild(0).position = data.position;
}
public void OnPointerEnter(PointerEventData data)
{
ManualInventory.instance.enteredSlot = this;
}
public void OnPointerExit(PointerEventData data)
{
ManualInventory.instance.enteredSlot = null;
}
public void OnEndDrag(PointerEventData data)
{
ManualInventory.instance.draggingItem.GetChild(0).parent = transform;
transform.GetChild(0).localPosition = Vector3.zero;
//내용은 간단합니다. enteredSlot이 null이 아닐 때, 즉 비어있는 상태가 아닐 때 두 슬롯 간의 아이템 교환이 이루어지고 ItemImageChange 함수를 통해 아이템에 맞는 Image로 변경시켜 줍니다.
if (ManualInventory.instance.enteredSlot != null)
{
ManualRollItem tempItem = item;
item = ManualInventory.instance.enteredSlot.item;
ManualInventory.instance.enteredSlot.item = tempItem;
ManualInventory.instance.ItemImageChange(this);
ManualInventory.instance.ItemImageChange(ManualInventory.instance.enteredSlot);
}
}
}
댓글 없음:
댓글 쓰기