Advertisement
evelynshilosky

TrashSlot - Part 11

Jun 3rd, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7.  
  8.  
  9. public class TrashSlot : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
  10. {
  11.  
  12.     public GameObject trashAlertUI;
  13.  
  14.     private Text textToModify;
  15.  
  16.     public Sprite trash_closed;
  17.     public Sprite trash_opened;
  18.  
  19.     private Image imageComponent;
  20.  
  21.     Button YesBTN, NoBTN;
  22.  
  23.     GameObject draggedItem
  24.     {
  25.         get
  26.         {
  27.             return DragDrop.itemBeingDragged;
  28.         }
  29.     }
  30.  
  31.     GameObject itemToBeDeleted;
  32.  
  33.  
  34.  
  35.     public string itemName
  36.     {
  37.         get
  38.         {
  39.             string name = itemToBeDeleted.name;
  40.             string toRemove = "(Clone)";
  41.             string result = name.Replace(toRemove, "");
  42.             return result;
  43.         }
  44.     }
  45.  
  46.  
  47.  
  48.     void Start()
  49.     {
  50.         imageComponent = transform.Find("background").GetComponent<Image>();
  51.  
  52.         textToModify = trashAlertUI.transform.Find("Text").GetComponent<Text>();
  53.  
  54.         YesBTN = trashAlertUI.transform.Find("yes").GetComponent<Button>();
  55.         YesBTN.onClick.AddListener(delegate { DeleteItem(); });
  56.  
  57.         NoBTN = trashAlertUI.transform.Find("no").GetComponent<Button>();
  58.         NoBTN.onClick.AddListener(delegate { CancelDeletion(); });
  59.  
  60.     }
  61.  
  62.  
  63.     public void OnDrop(PointerEventData eventData)
  64.     {
  65.         //itemToBeDeleted = DragDrop.itemBeingDragged.gameObject;
  66.         if (draggedItem.GetComponent<InventoryItem>().isTrashable == true)
  67.         {
  68.             itemToBeDeleted = draggedItem.gameObject;
  69.  
  70.             StartCoroutine(notifyBeforeDeletion());
  71.         }
  72.        
  73.     }
  74.  
  75.     IEnumerator notifyBeforeDeletion()
  76.     {
  77.         trashAlertUI.SetActive(true);
  78.         textToModify.text = "Throw away this " + itemName + "?";
  79.         yield return new WaitForSeconds(1f);
  80.     }
  81.  
  82.     private void CancelDeletion()
  83.     {
  84.         imageComponent.sprite = trash_closed;
  85.         trashAlertUI.SetActive(false);
  86.     }
  87.  
  88.     private void DeleteItem()
  89.     {
  90.         imageComponent.sprite = trash_closed;
  91.         DestroyImmediate(itemToBeDeleted.gameObject);
  92.         InventorySystem.Instance.ReCalculateList();
  93.         CraftingSystem.Instance.RefreshNeededItems();
  94.         trashAlertUI.SetActive(false);
  95.     }
  96.  
  97.     public void OnPointerEnter(PointerEventData eventData)
  98.     {
  99.  
  100.         if (draggedItem != null && draggedItem.GetComponent<InventoryItem>().isTrashable == true)
  101.         {
  102.             imageComponent.sprite = trash_opened;
  103.         }
  104.        
  105.     }
  106.  
  107.     public void OnPointerExit(PointerEventData eventData)
  108.     {
  109.         if (draggedItem != null && draggedItem.GetComponent<InventoryItem>().isTrashable == true)
  110.         {
  111.             imageComponent.sprite = trash_closed;
  112.         }
  113.     }
  114.  
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement