본문 바로가기

프로그래밍/Unity

[Unity] Mouse 이벤트 (다운, 업, 클릭, 드래그)

[Unity] Mouse 이벤트 (다운, 업, 클릭, 드래그)

 

 

마우스 다운, 업 이벤트

- PointerEventData로 pointerIdposition 등을 알수 있다.

 

 

using UnityEngine;

using UnityEngine.EventSystems;

 

public class Example : MonoBehaviour, IPointerDownHandler, IPointerUpHandler

{

    //Detect current clicks on the GameObject (the one with the script attached)

    public void OnPointerDown(PointerEventData pointerEventData)

    {

        //Output the name of the GameObject that is being clicked

        Debug.Log(name + "Game Object Click in Progress");

    }

 

    //Detect if clicks are no longer registering

    public void OnPointerUp(PointerEventData pointerEventData)

    {

        Debug.Log(name + "No longer being clicked");

    }

}

 


 

마우스 클릭

 

 

public class ExampleClass : MonoBehaviour

{

    void Update()

    {

        if (Input.GetMouseButtonDown(0))

            Debug.Log("Pressed primary button.");

 

        if (Input.GetMouseButtonDown(1))

            Debug.Log("Pressed secondary button.");

 

        if (Input.GetMouseButtonDown(2))

            Debug.Log("Pressed middle click.");

    }

}

 


 

마우스 업

 

 

public class Example : MonoBehaviour

{

    // Detects clicks from the mouse and prints a message

    // depending on the click detected.

 

    void Update()

    {

        if (Input.GetMouseButtonUp(0))

        {

            Debug.Log("Pressed left click.");

        }

        if (Input.GetMouseButtonUp(1))

        {

            Debug.Log("Pressed right click.");

        }

        if (Input.GetMouseButtonUp(2))

        {

            Debug.Log("Pressed middle click.");

        }

    }

}

 


 

마우스 드래그

 

 

// Enable the selection of a ScrollView which can be dragged.

// This script only chooses the ScrollView child of the Canvas.

 

using UnityEngine;

using UnityEngine.EventSystems;

using UnityEngine.UI;

 

public class ExampleScript : MonoBehaviour, IBeginDragHandler

{

    private GameObject m_DraggingIcon;

 

    public void Start()

    {

        m_DraggingIcon = new GameObject("icon");

    }

 

    public void OnBeginDrag(PointerEventData data)

    {

        Debug.Log("OnBeginDrag");

 

        var canvas = FindInParents<Canvas>(gameObject);

        if (canvas == null)

            return;

 

        m_DraggingIcon.transform.SetParent(canvas.transform, false);

        m_DraggingIcon.transform.SetAsLastSibling();

 

        Debug.Log("Dragging started");

    }

 

    // locate and return the Canvas

    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;

    }

}


using UnityEngine;

using System.Collections;

using UnityEngine.EventSystems; // Required when using event data

 

public class ExampleClass : MonoBehaviour, IDragHandler // required interface when using the OnDrag method.

{

    //Do this while the user is dragging this UI Element.

    public void OnDrag(PointerEventData data)

    {

        Debug.Log("Currently dragging " + this.name);

    }

}

 


using UnityEngine;

using System.Collections;

using UnityEngine.EventSystems; // Required when using event data

 

public class ExampleClass : MonoBehaviour, IEndDragHandler // required interface when using the OnEndDrag method.

{

    //Do this when the user stops dragging this UI Element.

    public void OnEndDrag(PointerEventData data)

    {

        Debug.Log("Stopped dragging " + this.name + "!");

    }

}