유니티에서 사용자 정의 Inspector 만들기(에디터 스크립트)

유니티에서 사용자 정의 Inspector 만들기(에디터 스크립트)

editor scripting

learn.unity.com/tutorial/editor-scripting?language=en

CustomEditors

docs.unity3d.com/Manual/editor-CustomEditors.html

 

1)스크립트를 생성한다(test.cs) 

 

2)스크립트의 에디터 스크립트를 생성한다(testEditor.cs)

에디터 스크립트에 네임스페이스를 추가한다.
using UnityEditor; 

 

에디터스크립트는 Editor 클래스로부터 상속받는다.
public class LevelScriptEditor : Editor 
{
}


어트리뷰트를 추가한다(CustomEditor)
[CustomEditor(typeof(test))]

 

testEditor.cs(에디터 스크립트)는 test.cs와 구별하여 Editor 폴더에 위치시킨다.

 

3)test 스크립트에 변수를 선언하고 씬의 게임오브젝트에 추가한다.

 

4)현재 에디터 스크립트의 GUI 코드가 존재하지않기때문에 필드를 표시하지않는다.

 

 

 

5)아래 에디터 스크립트의 EditorGUILayout.LabelField 읽기전용 함수에서 인스펙터의 Level 필드명에 객체의 변수(t.i)를 표시한다.

 

OnInspectorGUI()

OnInspectorGUI 함수는 선택한 오브젝트가 인스펙터 윈도우에 표시될때 호출되는 함수이다.
public override void OnInspectorGUI()   
{
}

 

target(The object being inspected)

target는 선택되어 인스펙터 윈도우에 표시되는 객체이다. 

https://docs.unity3d.com/ScriptReference/Editor-target.html

 

 

6)아래 에디터 스크립트의 EditorGUILayout.IntField 함수에서 텍스트 입력을 위한 정수형 필드를 인스펙터에 표시하고 사용자가 입력한 값을 변수에 저장한다.

https://docs.unity3d.com/ScriptReference/EditorGUILayout.IntField.html

 

7)인스펙터에서 필드에 정수값을 입력하고 게임을 시작하면 해당 값을 출력한다.

 

8)test.cs와 testEditor.cs 예시

test.cs

 

testEditor.cs

 

에디터 스크립트에서 인스펙터에 정수형 입력 필드 표시

https://docs.unity3d.com/ScriptReference/EditorGUILayout.IntField.html

 

에디터 스크립트에서 인스펙터에 슬라이더 표시

https://docs.unity3d.com/ScriptReference/EditorGUILayout.Slider.html

 

에디터 스크립트에서 인스펙터에 enum 팝업 표시

https://docs.unity3d.com/ScriptReference/EditorGUILayout.EnumPopup.html

 

에디터 스크립트에서 인스펙터에 토글 체크박스 표시

https://docs.unity3d.com/ScriptReference/EditorGUILayout.Toggle.html

 

 

 

9)스크립트에 존재하는 모든 필드를 표시하도록 DrawDefaultInspector함수를 사용할수있다.

https://docs.unity3d.com/ScriptReference/Editor.DrawDefaultInspector.html

 

기타

OnGUI()

docs.unity3d.com/ScriptReference/MonoBehaviour.OnGUI.html

 

에디터에  헬프박스 표시하기

https://docs.unity3d.com/ScriptReference/EditorGUILayout.HelpBox.html

 

reset함수

docs.unity3d.com/ScriptReference/MonoBehaviour.Reset.html

 

댓글

Designed by JB FACTORY