우선 아래와같은 Enemy라는 스크립트가 있다고 한다면 아래처럼 인스펙터에서 보일것이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Enemy : MonoBehaviour {
    public MonsterType monsterType;
    public int hp;
    public float damage;
    public string tag;
    public bool canRun;
}
 
public enum MonsterType
{
    Slime,
    Ent,
    Goblin
}
cs




이 컴포넌트가 내가 원하는대로 보이게 하고 싶을때 커스텀에디터를 사용한다.

우선 새로 스크립트를 만든 후에 Editor를 상속받고 그 위에 아래와 같이 커스텀에디터 속성을 붙여준다.

1
2
3
4
using UnityEditor;
 
[CustomEditor(typeof(Enemy))]
public class EnemyEditor : Editor
cs


그리고 OnInspectorGUI()를 오버라이드 한 후에 내용을 작성하면 Enemy 컴포넌트는 이제 내가 작성한대로 보이게 된다.


Editor를 상속받은 클래스의 OnEnable()은 

인스펙터에서 해당 스크립트를 가진 오브젝트가 선택되어 인스펙터에서 보일때 활성화된다.



그렇게 작성한 커스텀에디터 예제이다.

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
 
// Editor를 상속받으면 에디터에서만 작동함
// Enemy 클래스는 내가만든 에디터로 제어하겠다.
[CustomEditor(typeof(Enemy))]
public class EnemyEditor : Editor
{
    // EnemyEditor와 Enemy는 별개의 클래스이므로 실제 선택된 Enemy를 찾아올수 있어야함
    public Enemy selected;
 
    // Editor에서 OnEnable은 실제 에디터에서 오브젝트를 눌렀을때 활성화됨
    private void OnEnable()
    {
        // target은 Editor에 있는 변수로 선택한 오브젝트를 받아옴.
        if (AssetDatabase.Contains(target))
        {
            selected = null;
        }
        else
        {
            // target은 Object형이므로 Enemy로 형변환
            selected = (Enemy)target;
        }
    }
 
    // 유니티가 인스펙터를 GUI로 그려주는함수
    public override void OnInspectorGUI()
    {
        if (selected == null)
            return;
 
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("****** 몬스터 정보 입력툴 ******");
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();
 
        // 씬 첫화면에 광고를 나오게한다. 뭘한다 등등 세팅을 쭉 띄워줄 수 있다.
        Color tempColor = Color.white;
        switch (selected.monsterType)
        {
            case MonsterType.Slime:
                tempColor = Color.yellow;
                break;
            case MonsterType.Ent:
                tempColor = Color.cyan;
                break;
            case MonsterType.Goblin:
                tempColor = Color.green;
                break;
            default:
                break;
        }
 
        GUI.color = tempColor;
        selected.monsterType = (MonsterType)EditorGUILayout.EnumPopup("몬스터 종류", selected.monsterType);
 
        GUI.color = Color.white;
        selected.hp = EditorGUILayout.IntField("몬스터 체력", selected.hp);
        if (selected.hp < 0)
            selected.hp = 0;
 
        selected.damage = EditorGUILayout.FloatField("몬스터 공격력", selected.damage);
        selected.tag = EditorGUILayout.TextField("설명", selected.tag);
 
        // Release 세팅하고 버튼누르면 모든변수가 다바뀌게. Test 세팅하면 그렇게 바뀌게 그런식으로 사용할 수 있음.
        if (GUILayout.Button("Resize"))
        {
            selected.transform.localScale = Vector3.one * Random.Range(0.5f, 1f);
        }
    }
}
 
cs


이렇게 커스텀으로 만들게 되면 위와 같이 enum 타입에 따라 다른색상을 줄 수도 있고.

int 범위 중에서 양수만 받아오게 할 수도 있을것이다. 이렇게 원하는대로 커스텀할 수 있다.


https://docs.unity3d.com/kr/current/Manual/editor-CustomEditors.html

Posted by misty_
,