728x90
전에 타일맵을 생성하는 인스펙터 창을 제작하면서 이번에는 따로 인스펙터 창을 제작하지 않아도 간단하게 [Button]을 추가하면 인스펙터 창에서 실행할수 있도록 제작하려고 한다
결과
1. ButtonAttribute 만들기
using System;
using UnityEngine;
namespace PKW_Attributes
{
public enum ButtonEnableMode
{
// 항상
Always,
// 에디터 상에서만
Editor,
// Playmode 상에서만
Playmode
}
// 하나의 매소드를 대상으로 동작
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class ButtonAttribute : PropertyAttribute
{
public string ButtonName { get; }
public ButtonEnableMode ButtonEnableMode { get; private set; }
public ButtonAttribute(string buttonName = "", ButtonEnableMode ButtonEnableMode = ButtonEnableMode.Always)
{
this.ButtonName = buttonName;
this.ButtonEnableMode = ButtonEnableMode;
}
}
}
2. 인스펙터창에 띄우기
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace PKW_Attributes
{
[CustomEditor(typeof(MonoBehaviour), true)]
public class ButtonAttributeEditor : Editor
{
private MonoBehaviour monoBehaviour;
private void OnEnable()
{
monoBehaviour = target as MonoBehaviour;
}
public override void OnInspectorGUI()
{
// 원래 존재하는 Inspector를 그린다
DrawDefaultInspector();
//base.OnInspectorGUI(); 를 사용해도 상관 없다
// 모든 매소드를 확인해 본다
MethodInfo[] methods = monoBehaviour.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (MethodInfo method in methods)
{
ButtonAttribute buttonAttribute = Attribute.GetCustomAttribute(method, typeof(ButtonAttribute)) as ButtonAttribute;
if (buttonAttribute != null)
{
// 버튼 이름 설정
string buttonLabel = string.IsNullOrEmpty(buttonAttribute.ButtonName) ? method.Name : buttonAttribute.ButtonName;
if (GUILayout.Button(buttonLabel))
{
method.Invoke(monoBehaviour, null);
}
}
}
}
}
}
3. TestCode
using UnityEngine;
namespace PKW_Attributes
{
public class ButtonAttributeTest : MonoBehaviour
{
public int testInt = 0;
[Button("TestMethod")]
public void TestMethod()
{
Debug.Log("TestMethod");
}
public void TestMethod2()
{
Debug.Log("TestMethod2");
}
[Button("TestMethod3")]
public void TestMethod3()
{
testInt++;
Debug.Log("TestMethod3");
}
}
}
728x90
'unity > 유니티 기초' 카테고리의 다른 글
유니티 에러 - error CS0246: The type or namespace name 'CustomEditorAttribute' could not be found (are you missing a using directive or an assembly reference?) (0) | 2024.10.19 |
---|---|
keytool 문제 해결방법 - keytool은 내부또는외부명령,실행할수있는프로그램,또는배치파일이아닙니다. (0) | 2024.10.14 |
IEnumerator을 활용한 맵 생성 시뮬 (0) | 2024.08.19 |
유니티 vector2int (0) | 2024.08.15 |
Unity2D - Tilemap에서 플레이어가 움직이지 않을경우 (0) | 2024.08.09 |