Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
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
Archives
Today
Total
관리 메뉴

게임 프로그래밍

[Unity] IDragHandler 본문

프로그래밍/유니티

[Unity] IDragHandler

Junwe 2020. 3. 10. 23:28

IDragHandler

 

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using UnityEngine;
using UnityEngine.EventSystems;
 
[RequireComponent(typeof(Image))]
public class DragMe : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    public bool dragOnSurfaces = true;
    
    private GameObject m_DraggingIcon;
    private RectTransform m_DraggingPlane;
 
    public void OnBeginDrag(PointerEventData eventData)
    {
        var canvas = FindInParents<Canvas>(gameObject);
        if (canvas == null)
            return;
 
        // We have clicked something that can be dragged.
        // What we want to do is create an icon for this.
        m_DraggingIcon = new GameObject("icon");
 
        m_DraggingIcon.transform.SetParent (canvas.transform, false);
        m_DraggingIcon.transform.SetAsLastSibling();
        
        var image = m_DraggingIcon.AddComponent<Image>();
 
        image.sprite = GetComponent<Image>().sprite;
        image.SetNativeSize();
        
        if (dragOnSurfaces)
            m_DraggingPlane = transform as RectTransform;
        else
            m_DraggingPlane = canvas.transform as RectTransform;
        
        SetDraggedPosition(eventData);
    }
 
    public void OnDrag(PointerEventData data)
    {
        if (m_DraggingIcon != null)
            SetDraggedPosition(data);
    }
 
    private void SetDraggedPosition(PointerEventData data)
    {
        if (dragOnSurfaces && data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null)
            m_DraggingPlane = data.pointerEnter.transform as RectTransform;
        
        var rt = m_DraggingIcon.GetComponent<RectTransform>();
        Vector3 globalMousePos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, data.position, data.pressEventCamera, out globalMousePos))
        {
            rt.position = globalMousePos;
            rt.rotation = m_DraggingPlane.rotation;
        }
    }
 
    public void OnEndDrag(PointerEventData eventData)
    {
        if (m_DraggingIcon != null)
            Destroy(m_DraggingIcon);
    }
 
    static public T FindInParents<T>(GameObject go) where T : Component
    {
        if (go == nullreturn null;
        var comp = go.GetComponent<T>();
 
        if (comp != null)
            return comp;
        
        Transform t = go.transform.parent;
        while (t != null && comp == null)
        {
            comp = t.gameObject.GetComponent<T>();
            t = t.parent;
        }
        return comp;
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

IBeginDragHandler,IDragHandler,IEndDragHandler인터페이스를 추가해준 다음 각각

OnBeginDrag(PointerEventData eventData), OnDrag(PointerEventData data), OnEndDrag(PointerEventData eventData)를 이용해서 사용할 수 있다.

이를 이용하면 drag & drop 같은 기능을 만들 수 있을것이다.

Comments