Advertisement
graywolf69

ItemData Script

Sep 18th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5.  
  6. public class ItemData : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler {
  7.  
  8.     public Item item;
  9.     public int amount;
  10.     public int slot;
  11.  
  12.     private Inventory inv;
  13.     private Tooltip tooltip;
  14.     private Vector2 offset;
  15.  
  16.     void Start() {
  17.        
  18.         inv = GameObject.Find("Inventory").GetComponent<Inventory>();
  19.         tooltip = inv.GetComponent<Tooltip> ();
  20.  
  21.     }
  22.  
  23.     public void OnPointerDown (PointerEventData eventData) {
  24.        
  25.         if (item != null) {
  26.             offset = eventData.position - new Vector2 (this.transform.position.x, this.transform.position.y);
  27.             tooltip.Activate (item);
  28.         }
  29.  
  30.     }
  31.  
  32.     public void OnBeginDrag (PointerEventData eventData) {
  33.  
  34.         if (item != null) {
  35.             if (Input.GetMouseButton (0) && !Input.GetMouseButton (1) && !Input.GetMouseButton (2)) {
  36.                 Debug.Log ("Left Mouse Button Down");
  37.                 this.transform.SetParent (this.transform.parent.parent.parent);
  38.                 this.transform.position = eventData.position - offset;
  39.                 GetComponent<CanvasGroup> ().blocksRaycasts = false;
  40.             }
  41.             else if (!Input.GetMouseButton (0) && Input.GetMouseButton (1) && !Input.GetMouseButton (2)) {
  42.                 Debug.Log ("Right Mouse Button Down");
  43.                 this.transform.SetParent (this.transform.parent.parent.parent);
  44.                 this.transform.position = eventData.position - offset;
  45.                 GetComponent<CanvasGroup> ().blocksRaycasts = false;
  46.             }
  47.             else if (!Input.GetMouseButton (0) && !Input.GetMouseButton (1) && Input.GetMouseButton (2)) {
  48.                 Debug.Log ("Middle Mouse Button Down");
  49.                 this.transform.SetParent (this.transform.parent.parent.parent);
  50.                 this.transform.position = eventData.position - offset;
  51.                 GetComponent<CanvasGroup> ().blocksRaycasts = false;
  52.             }
  53.         }
  54.     }
  55.  
  56.     public void OnDrag (PointerEventData eventData) {
  57.  
  58.         if (item != null) {
  59.             this.transform.position = eventData.position - offset;
  60.         }
  61.  
  62.     }
  63.  
  64.     public void OnEndDrag (PointerEventData eventData) {
  65.  
  66.         this.transform.SetParent(inv.inventorySlots[slot].transform);
  67.         this.transform.position = inv.inventorySlots[slot].transform.position;
  68.         GetComponent<CanvasGroup>().blocksRaycasts = true;
  69.  
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement