커스텀에디터의 정석 - 인스펙터 윈도우
// 기본 에디터 구조
Cube cube;
void OnEnable()
{
cube = target as Cube;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
DrawDefaultInspector();
cube.textField = EditorGUILayout.TextField(cube.textField);
cube.textField = EditorGUILayout.TextField("텍스트", cube.textField);
cube.textField = EditorGUILayout.TextField(cube.textField, GUILayout.Width(100));
cube.textField = EditorGUILayout.TextField(cube.textField, GUILayout.Height(100));
cube.textField = EditorGUILayout.TextField(cube.textField, GUILayout.Width(100), GUILayout.Height(100));
cube.textField = EditorGUILayout.TextField(cube.textField, GUILayout.ExpandWidth(false));
cube.textField = EditorGUILayout.TextField(cube.textField, GUILayout.ExpandHeight(false));
}
------------------
// 박스
GUILayout.Box("박스");
GUILayout.Box(Resources.Load<Texture2D>("Mountain"), GUILayout.Width(100), GUILayout.Height(100));
// Expand가 기본 false
GUILayout.Box("박스", GUILayout.ExpandWidth(true), GUILayout.MinWidth(300), GUILayout.MaxWidth(600));
GUILayout.Box("박스", GUILayout.ExpandHeight(true), GUILayout.MinHeight(300), GUILayout.MaxHeight(600));
GUILayout.Box("박스", GUILayout.Width(300), GUILayout.Height(300));
------------------
// 버튼
// Expand가 기본 true
GUILayout.Button("버튼", GUILayout.MinWidth(300), GUILayout.MaxWidth(600));
GUILayout.Button("버튼", GUILayout.ExpandWidth(false));
if (GUILayout.Button("버튼")) Debug.Log("반응");
if (GUILayout.RepeatButton("연속 버튼")) Debug.Log("반응"); // 누르고 움직이면
if (EditorGUILayout.DropdownButton(new GUIContent("드롭다운 버튼"), FocusType.Keyboard)) Debug.Log("반응");
------------------
// 토글
bool toggle;
bool posGroupEnabled = true;
bool[] pos = new bool[] { true, true, true };
public override void OnInspectorGUI()
{
toggle = GUILayout.Toggle(toggle, "토글");
toggle = EditorGUILayout.ToggleLeft("토글", toggle);
toggle = EditorGUILayout.Toggle("토글", toggle);
posGroupEnabled = EditorGUILayout.BeginToggleGroup("Align position", posGroupEnabled);
pos[0] = EditorGUILayout.Toggle("x", pos[0]);
pos[1] = EditorGUILayout.Toggle("y", pos[1]);
pos[2] = EditorGUILayout.Toggle("z", pos[2]);
EditorGUILayout.EndToggleGroup();
}
------------------
// 라벨
int intField;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.LabelField("라벨");
EditorGUILayout.SelectableLabel("복사가능 라벨");
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("앞 라벨");
intField = EditorGUILayout.IntField(intField);
EditorGUILayout.EndHorizontal();
}
------------------
// 텍스트필드
Cube cube;
void OnEnable()
{
cube = target as Cube;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
cube.text = GUILayout.TextField(cube.text);
cube.text = EditorGUILayout.TextField("텍스트", cube.text);
cube.text = EditorGUILayout.PasswordField("비밀번호", cube.text);
cube.text = EditorGUILayout.DelayedTextField("딜레이 텍스트", cube.text);
}
------------------
// 텍스트 에리어
Cube cube;
void OnEnable()
{
cube = target as Cube;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
cube.text = GUILayout.TextArea(cube.text);
cube.text = EditorGUILayout.TextArea(cube.text, GUILayout.Height(50));
}
------------------
// 윈도우
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class CubeWindowEditor : EditorWindow
{
[MenuItem("Examples/MyWindow")]
static void Init()
{
CubeWindowEditor window = GetWindow<CubeWindowEditor>(typeof(Cube));
window.minSize = new Vector2(100, 100);
window.maxSize = new Vector2(500, 500);
window.Show();
}
void OnGUI()
{
if (GUILayout.Button("버튼")) Debug.Log("반응");
if (GUILayout.RepeatButton("연속 버튼")) Debug.Log("반응"); // 누르고 움직이면
if (EditorGUILayout.DropdownButton(new GUIContent("드롭다운 버튼"), FocusType.Keyboard)) Debug.Log("반응");
}
}
------------------
// 슬라이더
Cube cube;
void OnEnable()
{
cube = target as Cube;
}
public override void OnInspectorGUI()
{
cube.floatValue = GUILayout.HorizontalSlider(cube.floatValue, 0f, 10f, GUILayout.Height(20));
cube.floatValue = GUILayout.VerticalSlider(cube.floatValue, 10f, 0f, GUILayout.Height(100));
cube.floatValue = EditorGUILayout.Slider("Slider", cube.floatValue, 0f, 10f);
cube.intValue = EditorGUILayout.IntSlider(cube.intValue, 0, 10);
}
------------------
// 스크롤바
cube.floatValue = GUILayout.HorizontalScrollbar(cube.floatValue, 2f, 0f, 10f);
cube.floatValue = GUILayout.VerticalScrollbar(cube.floatValue, 2f, 10f, 0f);
------------------
// 스크롤 뷰
Vector2 scroll;
public override void OnInspectorGUI()
{
scroll = EditorGUILayout.BeginScrollView(scroll, GUILayout.Width(100), GUILayout.Height(100));
GUILayout.Button("버튼", GUILayout.Width(150), GUILayout.Height(100));
GUILayout.Button("버튼", GUILayout.Height(100));
EditorGUILayout.EndScrollView();
}
-----------------
// 기타 입력
float floatField;
float delayedFloatField;
int intField;
int delayedIntField;
double doubleField;
double delayedDoubleField;
long longField;
Vector2 vector2Field;
Vector2Int vector2IntField;
Vector3 vector3Field;
Vector3Int vector3IntField;
Vector4 vector4Field;
Color colorField;
Gradient gradientField = new Gradient();
AnimationCurve curveField;
Bounds boundsField;
BoundsInt boundsIntField;
Rect rectField;
RectInt rectIntField;
enum EnumFlags { 플래그0, 플래그1, 플래그2 };
EnumFlags enumFlagsField;
Object objectField;
string tagField;
int layerField;
string[] masks = new string[] { "마스크1", "마스크2", "마스크3" };
int maskField;
public override void OnInspectorGUI()
{
EditorGUILayout.HelpBox("HelpBox", MessageType.Info);
EditorGUILayout.Knob(new Vector2(30f, 30f), 50f, 20f, 80f, "Knob", Color.black, Color.grey, true);
floatField = EditorGUILayout.FloatField("Float", floatField);
delayedFloatField = EditorGUILayout.DelayedFloatField("DelayedFloat", delayedFloatField);
intField = EditorGUILayout.IntField("Int", intField);
delayedIntField = EditorGUILayout.DelayedIntField("DelayedInt", delayedIntField);
doubleField = EditorGUILayout.DoubleField("Double", doubleField);
delayedDoubleField = EditorGUILayout.DelayedDoubleField("DelayedDouble", delayedDoubleField);
longField = EditorGUILayout.LongField("Long", longField);
vector2Field = EditorGUILayout.Vector2Field("Vector2", vector2Field);
vector2IntField = EditorGUILayout.Vector2IntField("Vector2Int", vector2IntField);
vector3Field = EditorGUILayout.Vector3Field("Vector3", vector3Field);
vector3IntField = EditorGUILayout.Vector3IntField("Vector3Int", vector3IntField);
vector4Field = EditorGUILayout.Vector4Field("Vector4", vector4Field);
colorField = EditorGUILayout.ColorField("Color", colorField);
gradientField = EditorGUILayout.GradientField("Gradient", gradientField);
curveField = EditorGUILayout.CurveField("Curve", curveField);
boundsField = EditorGUILayout.BoundsField("Bounds", boundsField);
boundsIntField = EditorGUILayout.BoundsIntField("BoundsInt", boundsIntField);
rectField = EditorGUILayout.RectField("Rect", rectField);
rectIntField = EditorGUILayout.RectIntField("RectInt", rectIntField);
enumFlagsField = (EnumFlags)EditorGUILayout.EnumFlagsField("EnumFlags", enumFlagsField);
objectField = EditorGUILayout.ObjectField(objectField, typeof(Object), true);
tagField = EditorGUILayout.TagField("Tag", tagField);
layerField = EditorGUILayout.LayerField("Layer", layerField);
maskField = EditorGUILayout.MaskField("Mask", maskField, masks);
-----------------
// 시리얼라이즈 프로퍼티
SerializedProperty tr;
void OnEnable()
{
tr = serializedObject.FindProperty("tr");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.PropertyField(tr, new GUIContent("Transform"));
serializedObject.ApplyModifiedProperties();
}
-----------------
// 비긴 엔드
[MenuItem("Examples/MyWindow")]
static void Init()
{
CubeWindowEditor window = GetWindow<CubeWindowEditor>(typeof(Cube));
window.Show();
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(10, 10, 100, 100));
GUILayout.Button("버튼");
GUILayout.Button("버튼");
GUILayout.EndArea();
}
// 인스펙터에서
GUILayout.BeginHorizontal("box");
GUILayout.Button("버튼", GUILayout.Width(150), GUILayout.Height(100));
GUILayout.Space(20);
GUILayout.FlexibleSpace();
EditorGUILayout.Separator();
GUILayout.Button("버튼", GUILayout.Width(150),GUILayout.Height(100));
GUILayout.EndHorizontal();
GUILayout.BeginVertical("box");
GUILayout.Button("버튼", GUILayout.Width(150), GUILayout.Height(100));
GUILayout.Button("버튼", GUILayout.Height(100));
GUILayout.EndVertical();
AnimBool showFade;
void OnEnable()
{
showFade = new AnimBool(false);
showFade.valueChanged.AddListener(Repaint);
}
public override void OnInspectorGUI()
{
showFade.target = EditorGUILayout.ToggleLeft("페이드하기", showFade.target);
if (EditorGUILayout.BeginFadeGroup(showFade.faded))
{
EditorGUI.indentLevel++; // 자식처럼 들어감
EditorGUILayout.LabelField("색깔");
EditorGUILayout.LabelField("텍스트");
EditorGUILayout.LabelField("숫자");
EditorGUI.indentLevel--;
}
EditorGUILayout.EndFadeGroup();
}
bool showPosition = true;
public override void OnInspectorGUI()
{
showPosition = EditorGUILayout.BeginFoldoutHeaderGroup(showPosition, "펼치기");
if (showPosition && Selection.activeTransform)
Selection.activeTransform.position = EditorGUILayout.Vector3Field("Position", Selection.activeTransform.position);
EditorGUILayout.EndFoldoutHeaderGroup();
}
-------------------
//그리드
public int selGridInt = 0;
public string[] selStrings = new string[] { "그리드0", "그리드1", "그리드2", "그리드3" };
public Texture2D[] selTextures;
cube.selGridInt = GUILayout.SelectionGrid(cube.selGridInt, cube.selTextures, 2, GUILayout.Width(400), GUILayout.Height(400));
if(GUI.changed)로 판단가능
EditorGUI.BeginChangeCheck();
EditorGUI.EndChangeCheck();
-------------------
// 빌드타겟
BuildTargetGroup selectedBuildTargetGroup = EditorGUILayout.BeginBuildTargetSelectionGrouping();
if (selectedBuildTargetGroup == BuildTargetGroup.Android)
{
EditorGUILayout.LabelField("안드로이드에 관한 것");
}
if (selectedBuildTargetGroup == BuildTargetGroup.Standalone)
{
EditorGUILayout.LabelField("PC에 관한 것");
}
EditorGUILayout.EndBuildTargetSelectionGrouping();
-------------------
// 툴바
/* 팝업 2번째까지 같음
public int selIndex;
public string text0, text1;
Cube cube;
void OnEnable()
{
cube = target as Cube;
}
*/
EditorGUI.BeginChangeCheck();
cube.selIndex = GUILayout.Toolbar(cube.selIndex, new string[] { "0", "1" });
if (EditorGUI.EndChangeCheck()) GUI.FocusControl(null);
switch (cube.selIndex)
{
case 0: cube.text0 = EditorGUILayout.TextField("0", cube.text0); break;
case 1: cube.text1 = EditorGUILayout.TextField("1", cube.text1); break;
}
-------------------
//팝업
cube.selIndex = EditorGUILayout.Popup(cube.selIndex, new string[] { "0", "1" });
switch (cube.selIndex)
{
case 0: cube.text0 = EditorGUILayout.TextField(cube.text0); break;
case 1: cube.text1 = EditorGUILayout.TextField(cube.text1); break;
}
cube.selIndex = EditorGUILayout.IntPopup(cube.selIndex, new string[] { "0", "1" }, new int[] { 10, 100 });
switch (cube.selIndex)
{
case 10: cube.text0 = EditorGUILayout.TextField(cube.text0); break;
case 100: cube.text1 = EditorGUILayout.TextField(cube.text1); break;
}
/*
public string text0, text1;
public enum State { Idle, Walk };
public State state;
*/
cube.state = (Cube.State)EditorGUILayout.EnumPopup(cube.state);
switch (cube.state)
{
case Cube.State.Idle:
cube.text0 = EditorGUILayout.TextField(cube.text0);
break;
case Cube.State.Walk:
cube.text1 = EditorGUILayout.TextField(cube.text1);
break;
}
-------------------
//기타
// 재생, 중지 예제
string label = EditorApplication.isPlaying ? "Stop" : "Play";
if (GUILayout.Button(label))
EditorApplication.ExecuteMenuItem("Edit/Play");
// 색깔 바꾸기 예제
GUI.backgroundColor = Color.Yellow;
GUI.backgroundColor = Color.White;
-------------------
//셀 버튼 바꾸기 예제
public int size = 8;
public string[] Cell = new string[64];
public string[] CellState = new string[] { "A", "B", "C" };
Cube cube;
void OnEnable()
{
cube = target as Cube;
}
public override void OnInspectorGUI()
{
int index = 0;
for (int i = 0; i < cube.size; i++)
{
GUILayout.BeginHorizontal();
for (int j = 0; j < cube.size; j++)
{
if (GUILayout.Button(cube.Cell[index], GUILayout.Width(50), GUILayout.Height(50)))
{
if (cube.Cell[index] == cube.CellState[0])
cube.Cell[index] = cube.CellState[1];
else if (cube.Cell[index] == cube.CellState[1])
cube.Cell[index] = cube.CellState[2];
else if (cube.Cell[index] == cube.CellState[2])
cube.Cell[index] = cube.CellState[0];
}
index++;
}
GUILayout.EndHorizontal();
}
}
----------------------
// 텍스트에셋 예제
string text;
TextAsset txtAsset;
Vector2 scroll;
public override void OnInspectorGUI()
{
txtAsset = (TextAsset)EditorGUILayout.ObjectField(txtAsset, typeof(TextAsset), true);
if (txtAsset != null)
{
text = txtAsset.text;
scroll = EditorGUILayout.BeginScrollView(scroll);
text = EditorGUILayout.TextArea(text, GUILayout.Height(600));
EditorGUILayout.EndScrollView();
}
}
---------------------
// 애니메이션 커브 예제
Cube cube;
void OnEnable()
{
cube = target as Cube;
}
public override void OnInspectorGUI()
{
cube.curveX = EditorGUILayout.CurveField("Animation on X", cube.curveX);
cube.SetCurves(cube.curveX);
}
큐브는
public AnimationCurve curveX = AnimationCurve.Linear(0,0,1,1);
public void SetCurves(AnimationCurve xC)
{
curveX = xC;
}
void Update()
{
transform.position = new Vector3(curveX.Evaluate(Time.time), 0 ,0);
}