ItemInventory.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. public class ItemInventory : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler
  8. {
  9. public item item;
  10. [HideInInspector]
  11. public Transform parentAfterDrag;
  12. public Image image;
  13. InventoryManager inventoryManager = new InventoryManager();
  14. public void Set(item newItem, InventoryManager man)
  15. {
  16. inventoryManager = man;
  17. Debug.Log("Setting new item to this slot " + newItem.type, gameObject);
  18. item = newItem;
  19. image.sprite = newItem.image;
  20. }
  21. public void Clear()
  22. {
  23. // UnityEditor.EditorApplication.delayCall += () =>
  24. // {
  25. // DestroyImmediate(gameObject);
  26. // };
  27. }
  28. public void Drop()
  29. {
  30. inventoryManager.
  31. DropItem(item);
  32. Destroy(gameObject);
  33. }
  34. public void OnBeginDrag(PointerEventData eventData)
  35. {
  36. image.raycastTarget = false;
  37. parentAfterDrag = transform.parent;
  38. transform.SetParent(transform.parent.parent.parent.parent);
  39. }
  40. public void OnDrag(PointerEventData eventData)
  41. {
  42. transform.position = eventData.position;
  43. }
  44. public void OnEndDrag(PointerEventData eventData)
  45. {
  46. Debug.Log("Dropping into " + parentAfterDrag.name, parentAfterDrag.gameObject);
  47. if(parentAfterDrag == transform.parent)
  48. {
  49. Destroy(gameObject);
  50. }
  51. image.raycastTarget = true;
  52. transform.SetParent(parentAfterDrag);
  53. }
  54. public void OnPointerClick(PointerEventData eventData)
  55. {
  56. //inventoryManager.SelectItem(item);
  57. if (item.isUsable == false)return;
  58. if (inventoryManager.UseItem(item))
  59. {
  60. Destroy(gameObject);
  61. }
  62. }
  63. }