カラスの目はよく見るとかわいい

技術系ブログ。Unity、GoogleAppsScript、C#、VisualStudioCodeを中心に投稿しています。

エディタ拡張の例 入力支援を実装する

UnityのCustomEditorを使って入力支援を実装する

f:id:Rokkotsu:20210612113507g:plain
custom editor unity
using System.Collections.Generic;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif


public class Test : MonoBehaviour
{
    public string fruit = "test";
    [HideInInspector]
    public int editorSelectedIndex;
    [HideInInspector]
    public string[] dropdownItems = new string[] { "Apple", "Orange", "Banana" };

}


#if UNITY_EDITOR
[CustomEditor(typeof(Test))]
public class LocalizeTextEditor : Editor
{
    private Test test;

    private void OnEnable()
    {
        this.test = target as Test;
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        DrawDefaultInspector();
        EditorGUI.BeginChangeCheck();

        var selectedIndex = this.test.editorSelectedIndex;
        List<string> list = new List<string>();
//ここを変更すればテキストファイルから取ってきたりできる
        list.AddRange(this.test.dropdownItems);
        var displayOptions = list.ToArray();
        var index = displayOptions.Length > 0 ? EditorGUILayout.Popup("入力支援", selectedIndex, displayOptions)
            : -1;

        if (EditorGUI.EndChangeCheck())
        {
            var objectToUndo = this.test;
            var name = "Foo";
            Undo.RecordObject(objectToUndo, name);
            this.test.editorSelectedIndex = index;
            this.test.fruit = list[index];
        }

        serializedObject.ApplyModifiedProperties();
    }
}
#endif