[Unity6] Custom Editor
์นดํ ๊ณ ๋ฆฌ: Go Unity
TextField
Button
Color
GUIContent
GUIStyle
GUILayoutOption
Custom Editor
1.ย Editor ํด๋
Editor API ์ฌ์ฉ์ ์ํด ์ ๋ํฐ์์ ์ ๊ณตํ๋ ํด๋
Runtime์ ์๋ํ์ง ์๋๋ค.
Assets ํด๋ ํ์๋ผ๋ฉด Editor ํด๋์ ์์น๋ ์ค์ํ์ง ์๋ค (์ฌ๋ฌ ๊ฐ ์์ฑ ๊ฐ๋ฅ)
Editor๊ฐ ์๋ ๋ค๋ฅธ ํด๋์ Editor, EditorWindow ํด๋์ค๋ฅผ ์์๋ฐ์ ์คํฌ๋ฆฝํธ ์ ์ฅ ์ ์๋ฌ ๋ฐ์
ย
2.ย Editor Default Resources ํด๋
Resources ํด๋์ ๋ง์ฐฌ๊ฐ์ง๋ก Custrom Editor์์๋ง ์ฌ์ฉํ๋ ๋ฆฌ์์ค ์ ์ฅ ํด๋.
EditorGUIUtility.Load(string path) โsample.pngโ๋ก ๋ถ๋ฌ์ฌ ์ ์์ต๋๋ค.
์ฌ์ฉ๋๋ ํด๋์ค
์๋ํฐ ํ์ฅ์ ์ํ UI ์ถ๋ ฅ์ ์ฌ์ฉํ๋ ํด๋์ค
GUI, GUILayout, EditorGUI, EditorGUILayout
ย
GUI : ๊ฐ์ฅ ์ค๋๋ GUI ์์คํ
์ฃผ๋ก OnGUI() ๋ฉ์๋๋ฅผ ํตํด ์ฌ์ฉ
GUILayout : ์๋์ ์ธ ๋ ์ด์์์ ์ฌ์ฉํด UI ์์๋ฅผ ๋ฐฐ์นํ ๋ ์ฌ์ฉ
Runtime(O) / button(O)
ย
EditorGUI : ์๋ํฐ ์๋์ฐ ๋ฐ ์๋ํฐ ํ๊ฒฝ์์ ์ฌ์ฉํ๋ ์ ๋ํฐ GUI ์์
EditorGUILayout : EditorGUI์ ๋ ์ด์์ ๋ฒ์ ์๋์ ์ธ ๋ ์ด์์์ ์ฌ์ฉํด UI ๋ฐฐ์น
Runtime(X) / button(X)
ย
GUI / Layout
GUI, EditorGUI : Rect(x,y,width,heigh)๋ฅผ ํตํด UI์ ์์น ์ค์ , ๋์ด ๋์ด๋ฅผ ์ง์ ์ค์
GUILayout, EditorGUILayout : UI ์์น, ํฌ๊ธฐ๋ฅผ ์๋์ผ๋ก ์ค์ , ์ ๋ ฌ, (๋์ด : 18, ์ฝ๋ ์์ฑ ์์ผ๋ก ์ ๋ ฌ)
ย GUILayoutOption, Space(float Width)๋ฅผ ์ฌ์ฉํด ๊ฐ๊ฒฉ ์กฐ์
TestEditorWindow
SetUp() : ์๋ํฐ ์ฐฝ ์ ๋ชฉ, ํฌ๊ธฐ ์ค์
OnGUI() : GUI ์ถ๋ ฅ
TextField Button Color GUIContent GUIStyle GUILayoutOption
TestEditorWindow
using UnityEngine;
using UnityEditor;
public class TestEditorWindow : EditorWindow
{
private static TestEditorWindow window;
// ํ์ฌ๋ ์์๋ก ์ฌ๊ธฐ์ ๋ณ์๋ฅผ ๋์์ง๋ง ์ผ๋ฐ์ ์ผ๋ก ๋ฐ์ดํฐ๋ ์ธ๋ถ์์ ๊ฐ์ ธ์ ์ฌ์ฉ
private string textGUI = "GUI.TextField()";
private string textGUILayout = "GUILayout.TextField()";
private string textEditorGUI = "EditorGUI.TextField()";
private string textEditorGUILayout = "EditorGUILayout.TextField()";
[MenuItem("Window/Unitynote/EmptyWindow")]
private static void Setup()
{
window = GetWindow<TestEditorWindow>();
// SceneView ์ ํฌํจ๋๋๋ก ์ค์
// window = GetWindow<TestEditorWindow>(typeof(SceneView));
// ์๋์ฐ ์ ๋ชฉ(Title) ์ค์
window.titleContent = new GUIContent("Unitynote");
// ์๋์ฐ ์ต์/์ต๋ ํฌ๊ธฐ ์ค์
window.minSize = new Vector2(300, 300);
window.maxSize = new Vector2(1920, 1080);
}
private void OnEnable()
{
Debug.Log("OnEnable() : ์๋์ฐ๊ฐ ์ด๋ฆด ๋ 1ํ ํธ์ถ");
}
private void OnGUI()
{
Color defaultContentColor = GUI.contentColor;
GUI.contentColor = Color.red;
// ์๋์ ๊ฐ์ด new๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ์ง ์๊ณ ์ฐธ์กฐํด์ ์ฌ์ฉํ๋ฉด ์ ๋ํฐ ์๋ํฐ ์คํ์ผ ์ ์ฒด๊ฐ ๋ณ๊ฒฝ๋๋ค.
// GUIStyle customLabelStyle = EditorStyles.label;
GUIStyle customLabelStyle = new GUIStyle(EditorStyles.label);
customLabelStyle.alignment = TextAnchor.MiddleCenter;
customLabelStyle.fontStyle = FontStyle.BoldAndItalic;
customLabelStyle.fontSize = 20;
GUIStyle customLabelStyle2 = new GUIStyle(GUI.skin.label);
customLabelStyle2.fontStyle = FontStyle.Bold;
GUIContent LabelContent = new GUIContent("GUI.Label()", "[GUIContent Hover]");
GUI.Label(new Rect(0, 130, 300, 20), LabelContent, customLabelStyle);
GUILayout.Label("GUILayout.Label()", customLabelStyle2);
EditorGUI.LabelField(new Rect(0, 150, 300, 20), "EditorGUI.LabelField()");
EditorGUILayout.LabelField("EditorGUILayout.LabelField()");
GUI.contentColor = defaultContentColor;
Color defaultBackgroundColor = GUI.backgroundColor;
GUI.backgroundColor = Color.blue;
textGUI = GUI.TextField(new Rect(0, 170, 300, 20), textGUI);
textGUILayout = GUILayout.TextField(textGUILayout, GUILayout.Height(50), GUILayout.MaxWidth(500));
textEditorGUI = EditorGUI.TextField(new Rect(0, 190, 300, 20), "EditorGUI : ", textEditorGUI);
textEditorGUILayout = EditorGUILayout.TextField("EditorGUILayout : ", textEditorGUILayout);
GUI.backgroundColor = defaultBackgroundColor;
GUIContent btnContent = new GUIContent("GUI.Button()", "[GUIContent Hover]");
if (GUI.Button(new Rect(0, 210, 300, 20), btnContent))
{
Debug.Log("GUI.Button() Click");
}
if (GUILayout.Button("GUILayout.Button()"))
{
Debug.Log("GUILayout.Button() Click");
}
}
}
/* Editor Default Resources
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class TestEditorWindow
{
static TestEditorWindow()
{
var tex = EditorGUIUtility.Load("SampleTex.png");
Debug.Log(tex);
}
}*/
/* GUIContent
GUILayout.Label("์ถ๋ ฅ ๋ด์ฉ");
GUIContent content = new GUIContent("์ถ๋ ฅ ๋ด์ฉ", "๋ง์ฐ์ค๋ฅผ Hoverํ์ ๋ ์ถ๋ ฅํ ์ค๋ช
");
GUILayout.Label(content);
*/
Label
Lable, LabelField
GUI.Label(Rect rect,string text)
GUILayout.LabelField(string text)
Label
GUI.Label(new Rect(0, 130, 300, 20), "GUI.Label()", customLabelStyle);
GUILayout.Label("GUILayout.Label()", customLabelStyle2);
EditorGUI.LabelField(new Rect(0, 150, 300, 20), "EditorGUI.LabelField()");
EditorGUILayout.LabelField("EditorGUILayout.LabelField()");
TextField
string result = GUI.TextField(Rect rect, string text);
์
๋ ฅ ํ๋ ์ถ๋ ฅ, ํ๋์ ์
๋ ฅํ ๋ด์ฉ์ด ๋ฐํ๋๋ค.
ย
private string textGUI = โGUI.TextField()โ;
textGUI = GUI.TextField(new Rect(0, 170, 300, 20), textGUI);
ย
ย
string result = GUILayout.TextField(Rect rect, string label, string text);
Label๊ณผ ์
๋ ฅ ํ๋ ์ถ๋ ฅ, ํ๋์ ์
๋ ฅํ ๋ด์ฉ์ด ๋ฐํ๋๋ค.
ย
private string textGUILayout = โGUILayout.TextField()โ;
textGUILayout = GUILayout.TextField(textGUILayout, GUILayout.Height(50), GUILayout.MaxWidth(500));
TextField
textGUI = GUI.TextField(new Rect(0, 170, 300, 20), textGUI);
textGUILayout = GUILayout.TextField(textGUILayout, GUILayout.Height(50), GUILayout.MaxWidth(500));
textEditorGUI = EditorGUI.TextField(new Rect(0, 190, 300, 20), "EditorGUI : ", textEditorGUI);
textEditorGUILayout = EditorGUILayout.TextField("EditorGUILayout : ", textEditorGUILayout);
Button
GUI๋ง ๊ฐ๋ฅ Editor(x)
bool isClick = Button(Rect rect, string text)
๋ฒํผ ์ถ๋ ฅ ๋ฒํผ ์
๋ ฅ ์ฌ๋ถ๊ฐ ๋ฐํ๋ฉ๋๋ค.
Button
GUIContent btnContent = new GUIContent("GUI.Button()", "[GUIContent Hover]");
if (GUI.Button(new Rect(0, 40, 300, 20), btnContent))
{
Debug.Log("GUI.Button() Click");
}
if (GUILayout.Button("GUILayout.Button()"))
{
Debug.Log("GUILayout.Button() Click");
}
Color
๊ธฐ๋ณธ ์์ ์ ์ฅ - GUI ์์ ๋ณ๊ฒฝ - ๋ณ๊ฒฝ๋ ์์์ผ๋ก ์ถ๋ ฅ - GUI ๊ธฐ๋ณธ ์์์ผ๋ก ๋ณต๊ตฌ
Color
// ๊ธ ์์ ๋ณ๊ฒฝ
Color defaultContentColor = GUI.contentColor; // ๊ธฐ๋ณธ ์์ ์ ์ฅ
GUI.contentColor = Color.red; // GUI ์์ ๋ณ๊ฒฝ
GUI.Label(new Rect(0, 0, 300, 20), "GUI.Label()", customLabelStyle); // ๋ณ๊ฒฝ๋ ์์์ผ๋ก ์ถ๋ ฅ
GUI.contentColor = defaultContentColor; // GUI ๊ธฐ๋ณธ ์์์ผ๋ก ๋ณต๊ตฌ
// ๋ฐฐ๊ฒฝ ์ ๋ณ๊ฒฝ
Color defaultBackgroundColor = GUI.backgroundColor;
GUI.backgroundColor = Color.blue;
textGUI = GUI.TextField(new Rect(0, 30, 300, 20), textGUI);
GUI.backgroundColor = defaultBackgroundColor;
GUIContent
UI์ ๋ง์ฐ์ค๋ฅผ hover ํ์ ๋ ์ถ๋ ฅํ ์ค๋ช
์ค์
Label, TextField ๋ฑ๊ณผ ๊ฐ์ด ํ
์คํธ๋ฅผ ์ถ๋ ฅํ๋ ๋ชจ๋ ๋ฉ์๋์์ string, GUIContent๋ฅผ ์ ํํด์ ์ฌ์ฉ ๊ฐ๋ฅ
GUIContent textandhovertext = new GUIContent(โTextโ, โHoverTextโ);
GUIContent
GUIContent LabelContent = new GUIContent("GUI.Label()", "[GUIContent Hover]");
GUI.Label(new Rect(0, 130, 300, 20), LabelContent, customLabelStyle);
GUIContent btnContent = new GUIContent("GUI.Button()", "[GUIContent Hover]");
if (GUI.Button(new Rect(0, 210, 300, 20), btnContent))
{
Debug.Log("GUI.Button() Click");
}
GUIStyle
ํน์ style์ ์ค์ ํ๊ณ ์ํ๋ UI์ ์ ์ฉํด ๋ณ๊ฒฝ
new๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ์ง ์๊ณ ์ฐธ์กฐํด์ ์ฌ์ฉํ๋ฉด ์ ๋ํฐ ์๋ํฐ ์คํ์ผ ์ ์ฒด๊ฐ ๋ณ๊ฒฝ๋๋ค.
GUIStyle
// ์๋์ ๊ฐ์ด new๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ์ง ์๊ณ ์ฐธ์กฐํด์ ์ฌ์ฉํ๋ฉด ์ ๋ํฐ ์๋ํฐ ์คํ์ผ ์ ์ฒด๊ฐ ๋ณ๊ฒฝ๋๋ค.
// GUIStyle customLabelStyle = EditorStyles.label;
GUIStyle customLabelStyle = new GUIStyle(EditorStyles.label);
customLabelStyle.alignment = TextAnchor.MiddleCenter;
customLabelStyle.fontStyle = FontStyle.BoldAndItalic;
customLabelStyle.fontSize = 20;
GUIStyle customLabelStyle2 = new GUIStyle(GUI.skin.label);
customLabelStyle2.fontStyle = FontStyle.Bold;
GUI.Label(new Rect(0, 30, 300, 20), "GUI.Label()", customLabelStyle);
GUILayout.Label("GUILayout.Label()", customLabelStyle2);
GUILayoutOption
Layout์ ์ต์, ์ต๋ ํฌ๊ธฐ ๋ฑ์ ์ค์ ํ ๋ ์ฌ์ฉ
Width, minWidth, MaxWidth
Height, minHeight, maxHeight
ExpandWidth ๋จ๋ ๊ณต๊ฐ๋งํผ UI๋ฅผ ์ฑ์์ ์ถ๋ ฅ
ExpandHeight ๋จ๋ ๊ณต๊ฐ๋งํผ UI๋ฅผ ์ฑ์์ ์ถ๋ ฅ
Color
textGUILayout = GUILayout.TextField(textGUILayout);
textGUILayout = GUILayout.TextField(textGUILayout, GUILayout.Height(50), GUILayout.MaxWidth(500));
์ด๊ฒ์ ๊ฒ ๋ฉ๋ชจ
๊ธฐ๋ณธ์ ์ผ๋ก Layout์ ์ฌ์ฉํ๊ณ ์ํฉ์ ๋ง๊ฒ GUI/Editor, ๊ธฐ๋ณธ/Layout ๊ฒฐ์ ํด์ ์ฐ๋ฉด ๋ ๊ฑฐ ๊ฐ๋ค.
์ก๋ด, ์ผ๊ธฐ?
Custom Editor๋ก ๊ฐ๋ฐ ํจ์จ์ฑ์ ๋์ผ ์ ์๋ค.
๋๊ธ๋จ๊ธฐ๊ธฐ