使用すると下画像のようにInspectorにボタンを追加します。
以下 サンプルコード
Sample.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Test
{
public class Sample : MonoBehaviour
{
#if UNITY_EDITOR
public void SetInspectorUI()
{
// ここに処理を記載
Debug.Log("On Click");
}
#endif
}
#if UNITY_EDITOR
[CustomEditor(typeof(Sample))]
public class SampleEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Set UI"))
{
Sample t = target as Sample;
t.SetInspectorUI();
}
}
}
#endif
}
UIを取得するならこういう書き方かな?というサンプル
Sample.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Test
{
public class Sample : MonoBehaviour
{
public Text A;
public Text B;
public Text C;
#if UNITY_EDITOR
public void SetInspectorUI()
{
var texts = GetComponentsInChildren<Text>();
foreach (var t in texts)
{
if (t.name == "A")
{
A = t;
}
else if (t.name == "B")
{
B = t;
}
else if (t.name == "C")
{
C = t;
}
}
}
#endif
}
#if UNITY_EDITOR
[CustomEditor(typeof(Sample))]
public class SampleEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Set UI"))
{
Sample t = target as Sample;
t.SetInspectorUI();
}
}
}
#endif
}