mmorpg/Assets/Script/Inventory/ItemInventory.cs
2024-07-21 21:52:51 +05:30

78 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ItemInventory : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler
{
public item item;
[HideInInspector]
public Transform parentAfterDrag;
public Image image;
InventoryManager inventoryManager = new InventoryManager();
public void Set(item newItem, InventoryManager man)
{
inventoryManager = man;
Debug.Log("Setting new item to this slot " + newItem.type, gameObject);
item = newItem;
image.sprite = newItem.image;
}
public void Clear()
{
// UnityEditor.EditorApplication.delayCall += () =>
// {
// DestroyImmediate(gameObject);
// };
}
public void Drop()
{
inventoryManager.
DropItem(item);
Destroy(gameObject);
}
public void OnBeginDrag(PointerEventData eventData)
{
image.raycastTarget = false;
parentAfterDrag = transform.parent;
transform.SetParent(transform.parent.parent.parent.parent);
}
public void OnDrag(PointerEventData eventData)
{
transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("Dropping into " + parentAfterDrag.name, parentAfterDrag.gameObject);
if(parentAfterDrag == transform.parent)
{
Destroy(gameObject);
}
image.raycastTarget = true;
transform.SetParent(parentAfterDrag);
}
public void OnPointerClick(PointerEventData eventData)
{
//inventoryManager.SelectItem(item);
if (inventoryManager.UseItem(item))
{
Destroy(gameObject);
}
}
}