0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

inspectorの情報にヘルプとボタンを作る

Posted at

やりたいこと

image.png

ヘルプのテキスト情報とButtonを配置する。Test1ボタンをおすと関数が呼ばれる。

ヘルプのテキストを追加する。

ヘルプだけなら問題はないがヘルプとボタンをEditor拡張するため、複数拡張できるようにCanEditMultipleObjectsをクラスに追加する。

//複数選択有効
[CanEditMultipleObjects]
public class Test : MonoBehaviour
{

ヘルプを追加したいクラスにの上にEditorを継承したクラスを作成

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(Test))]
public class PlayerInputEditor : Editor
{
    public override void OnInspectorGUI()
    {
        var helpTxt = 
            "ヘルプ情報aaaaaaaaaaaaaaaaaaaaa\n" +
            "fdsasdfdddddddddddddddddddddddd\n";
        EditorGUILayout.HelpBox(helpTxt, MessageType.None);

        //元のInspectorを表示
        base.OnInspectorGUI();
        //スペースを追加
        GUILayout.Space(20);
    }
}
//複数選択有効
[CanEditMultipleObjects]
public class Test : MonoBehaviour

ustomEditor(typeof(Test)でEditor拡張したいクラスをしていする。

HelpBoxでテキストの表示

base.OnInspectorGUI();
もとのInspector情報が上書きされるので、元情報も表示する。

あとは好きにヘルプを修正する。

ボタンを追加する。

Attributeクラスを追加

    [SerializeField, Button("Test1")]
    private bool ButtonLabel;

最終的にこの形で使用する。

Attributeクラスを作成

    /// <summary>
    /// ButtonAttribute inspectorにボタンを追加するために必要
    /// </summary>
    public class ButtonAttribute : PropertyAttribute
    {
        public string methodName;
        public string buttonName;
        /// <summary>
        /// ボタンを押したときにメソッドを実行する。
        /// </summary>
        /// <param name="methodName">実行するメソッド名</param>
        /// <param name="buttonName">ボタンの表示名(省略可能)</param>
        /// <returns>void</returns>
        public ButtonAttribute(string methodName, string buttonName = null)
        {
            this.methodName = methodName;

            if (buttonName == null)
            {
                this.buttonName = methodName;
            }
            else
            {
                this.buttonName = buttonName;
            }

        }

ボタンの名前とメソッド名を変更できる機能も実装している。

この記事参考

EditorフォルダにButtonDrawerクラスを作成

UnityにはResoucesフォルダのようにEditorという名の特別なフォルダがある。
なければ自分でつくる。
image.png

[CustomPropertyDrawer(typeof(ButtonAttribute))]
public class ButtonDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        ButtonAttribute buttonAttribute = (ButtonAttribute)attribute;

        // プロパティ名のラベルを描画し、ラベル以外のrectを返す
        Rect rect_content = EditorGUI.PrefixLabel(position, label);

        // ボタンを描画
        if (GUI.Button(rect_content, buttonAttribute.buttonName))
        {
            try
            {
                // メソッド名からメソッドを取得、実行
                MethodInfo method = property.serializedObject.targetObject.GetType().GetMethod(buttonAttribute.methodName);
                method.Invoke(property.serializedObject.targetObject, null);
            }
            catch
            {
                Debug.Log(buttonAttribute.methodName + " を実行できません");
            }
        }
    }
}

typeof(ButtonAttribute)にさっき作ったクラスを指定
これで準備完了

つかう

//複数選択有効
[CanEditMultipleObjects]
public class Test : MonoBehaviour
{
    [SerializeField, Button("Test1")]
    private bool ButtonLabel;

    public void Test1()
    {
        Debug.Log("Test1_start");
    }

Test1()はpublicで設定。
image.png

やったね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?