-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDragAndDrop.cs
55 lines (48 loc) · 1.37 KB
/
DragAndDrop.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
public class DragAndDrop : MonoBehaviour
{
public Item DraggedItem { get; private set; }
public int Quantity { get; private set; }
private Image _draggedIcon;
public IItemContainer Source { get; private set; }
public int SourceSlot { get; private set; }
private Vector3 _mousePosition;
void Awake()
{
_draggedIcon = GetComponent<Image>();
}
// Update is called once per frame
void Update()
{
if(DraggedItem != null)
{
_mousePosition = Input.mousePosition;
//_mousePosition = Camera.main.ScreenToWorldPoint(_mousePosition);
_draggedIcon.transform.position = _mousePosition;
}
}
public void Drag(Item item, int quantity, IItemContainer source, int sourceSlot)
{
Source = source;
SourceSlot = sourceSlot;
DraggedItem = item;
Quantity = quantity;
_draggedIcon.sprite = DraggedItem.Sprite;
_draggedIcon.enabled = true;
}
public void Drop(bool success)
{
if (!success && Source != null)
{
Source.Drop(SourceSlot);
Source = null;
}
DraggedItem = null;
_draggedIcon.sprite = null;
_draggedIcon.enabled = false;
}
}