
유니티에서 컨텍스트 메뉴(ContextMenu) 는, 컴포넌트 이름 우측에 세로로 돌아간 ... 아이콘을 클릭했을 때 등장하는 메뉴입니다. Context Menu 를 직역하면 "맥락 메뉴" 정도인데, 인스펙터에 보여지는 해당 컴포넌트의 맥락에 맞는 메뉴라는 뜻.

스크립트에 ContextMenu 속성(Attribute)을 입력해서 이 "맥락 메뉴"항목을 간단하게 추가할 수 있고, 각종 편의기능 등을 구현해서 활용할 수 있습니다.
아래처럼, 컨텍스트 메뉴를 선택했을 때 호출할 함수 위에 [ContextMenu("Menu Name")] 을 입력해주기만 하면 됩니다. 그럼 함수 내용이 즉시 실행됩니다. 함수는 비정적 함수(non-static) 함수여야 합니다.
using UnityEngine;
public class ContextTesting : MonoBehaviour
{
/// Add a context menu named "Do Something" in the inspector
/// of the attached script.
[ContextMenu("Do Something")]
void DoSomething()
{
Debug.Log("Perform operation");
}
}
위 코드로 컨텍스트 메뉴를 추가하고 실행해보면, 로그창에 아래와 같이 로그가 바로 출력되는 결과를 확인할 수 있습니다.

보통 이 컨텍스트메뉴는, 붙여진 오브젝트와 관련된 자동화 등에 주로 사용됩니다. 현재 셋팅된 주변 정보나 컴포넌트 등의 셋팅을 참조해서 컴포넌트에 적절한 값을 계산해서 셋팅할 때 활용하면 편리합니다. 저도 SerializedField 값들을 하나하나 손으로 입력하기보다는 계산과 셋팅을 자동화할 때 주로 활용합니다.
Unity - Scripting API: ContextMenu
In the inspector of the attached script. When the user selects the context menu, the function will be executed. This is most useful for automatically setting up Scene data from the script. The function has to be non-static.
docs.unity3d.com
원본 백링크
https://m.blog.naver.com/sorang226/222917798280
[Unity] Component의 컨텍스트 메뉴(ContextMenu) 만들기
유니티에서 컨텍스트 메뉴(ContextMenu) 는, 컴포넌트 이름 우측에 세로로 돌아간 ... 아이콘을 클릭했을...
blog.naver.com
'유니티 엔진 (Unity Engine)' 카테고리의 다른 글
Unity 게임을 살리는 메모리 관리와 최적화 (G-CON X IGC 오지현 에반젤리스트) (0) | 2022.11.23 |
---|---|
Unity 플랫포머 샘플 게임 구동 해보고 살펴보기 (0) | 2022.11.22 |
[Unity] 에셋스토어 에셋 구입 시 Tax Number? (0) | 2022.11.03 |
[Unity] PlayerPref 레지스트리 저장 경로 (0) | 2022.07.26 |
SmoothDamp 는 어떻게 구현돼있을까? (0) | 2022.05.24 |
셰이더 입문서 추천 "아티스트를 위한 유니티 URP 셰이더 입문" (0) | 2022.03.29 |
강력 추천 유니티 입문서 "유니티 교과서" 개정5판 (0) | 2022.03.14 |