유니티 엔진 (Unity Engine)

[Unity] 유니티 엔진으로 툴 제작 시 에디터 클릭 이벤트 후킹하기

원소랑 2024. 3. 21. 17:03
728x90

Unity Engine 에디터 내에 Scene View 를 활용해서 툴을 만들 때, 마우스 좌클릭을 기본 도구에서의 선택 도구 외에 다른 용도로 사용하려고 하면 Unity Engine 의 Event 를 핸들링 할 필요가 있다.

Event.current 가 에디터의 UnityGUI 이벤트가 되는데, 이 이벤트가 마우스 좌클릭일 때 Use() 를 호출해서 사용처리 해버리면 선택 도구로 작동하는 것을 후킹할 수 있다.

if (Event.current.type != EventType.MouseDown)
    return;

if (Event.current.button != 0)
    return;

Event.current.Use();

// 툴에서 원하는 동작 처리.

 

Event.current.button 의 값은 마우스 버튼

// button 0 좌클릭
// button 1 우클릭

// button 2 미들클릭

참조 : https://docs.unity3d.com/ScriptReference/Event-button.html

 

아래는 UnityEngine의 EventType 공식 문서.

https://docs.unity3d.com/2019.4/Documentation/ScriptReference/EventType.html

 

Unity - Scripting API: EventType

Use this to tell which type of event has taken place in the GUI. Types of Events include mouse clicking, mouse dragging, button pressing, the mouse entering or exiting the window, and the scroll wheel as well as others mentioned below. See Also: Event.type

docs.unity3d.com

 

아래 코드를 추가하면 Editor 에 열려있는 씬에서 오브젝트를 선택되지 않게 할 수 있다.

private void OnSceneGUI()
{
    HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
}

 

728x90
반응형