おもちゃバコ

中身スカスカ♡

UnityでEditor拡張に入門した

こんにち母の日

今回はUnityのEditor拡張に入門したので,備忘録として記事にします.
しっかりした記事ではなく,早見表っぽい感じです.

参考サイト

49.233.81.186
とても詳細に書かれているので,このブログを見るより効果的です.

Editorディレクト

UnityEditorはエディタとランタイムでAPIのすみわけを行っているため,Editor拡張を行う際はEditorフォルダ内にスクリプトを作成すること.

f:id:lambda410:20210416224742p:plain
フォルダ階層

Editorフォルダに含めない場合は,下記のコードで囲う.

#if UNITY_EDITOR
~
#endif

標準Editor拡張

f:id:lambda410:20210416232024p:plain
標準Editor拡張欲張りセット

データ管理

  1. EditorPrefs
     プロジェクトをまたいでデータ保存したいときに使用.
  2. EditorUserSettings.Set/GetConfigValue
     プロジェクトで共有可能なデータを暗号化して保存する.
  3. ScriptableObject(下記に詳細ソースコード)
     プロジェクトで共有可能なデータを保存し,チーム内で共有,大量のデータ保存に適している.(マスタデータやEditor拡張で作成したデータの保存など.)
     以下,簡単な使用手順.
     1. インスタンス
     2. アセットとして保存
  4. SerializedObject
     シリアライズ化(データ構造やオブジェクト状態をUnityが保存して再構成可能な形に変換するプロセス)されたデータを加工したもの.

EditorGUI

f:id:lambda410:20210417163642p:plain
EditorGUI欲張りセット

EditorWindow

 EditorWindowの利用
 1. EditorWindowクラスを継承したクラスを作成
 2. EditorWindowを表示するためのトリガメニューを追加
 3. EditorWindowの表示

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEditor;

// 1. EditorWindowクラスを継承したクラスを作成
public class EditorWindowSample : EditorWindow
{
    // 2. EditorWindowを表示するためのトリガメニューを追加
    [MenuItem("EditorExtSample/Example2")]
    static void Open()
    {
        // 3. EditorWindowの表示
        var sample_window = CreateInstance<EditorWindowSample>();
        sample_window.Show();
    }
}

 いろいろなEditorWindow
 1. Show
  デフォルト
 2. ShowUtility
  タブウィンドウとして扱えない.
 3. ShowPopup
  ウィンドウタイトルと閉じるボタンがない.
 4. PopupWindow
  Popupの汎用版.
 5. ShowAuxWindow
  タブウィンドウとして扱えない.フォーカスを変えると削除される.
 6. ShowAsDropDown
  画面サイズに収まる位置に移動するPopup.
 7. ScriptableWizard
  何かを作るときに使用する.
 8. PreferenceItem
  Unity Preferencesにメニューを追加する.
 9. IHasCustomMenu
  メニューを追加する.

CustomEditor

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEditor;

[CustomEditor(typeof(TestPlayer))]
public class TestPlayerInspector : Editor
{
    TestPlayer m_player = null;

    // ゲームオブジェクトがアクティブ時に実行
    private void OnEnable()
    {
        m_player = (TestPlayer)target; // コンポーネント取得
    }

    public override void OnInspectorGUI()
    {
        EditorGUI.BeginChangeCheck();

        var pow = EditorGUILayout.IntSlider("Pow", m_player.Power, 0, 100);

        if (EditorGUI.EndChangeCheck())
        {
            // Undo登録
            Undo.RecordObject(m_player, "Cange Power");
            m_player.Power = pow;
        }
    }

    // プレビュー表示
    public override bool HasPreviewGUI()
    {
        return true;
    }
    // プレビュー名
    public override GUIContent GetPreviewTitle()
    {
        return new GUIContent("XYZ");
    }
    // プレビュー設定
    public override void OnPreviewSettings()
    {
        GUIStyle pre_label = new GUIStyle("preLabel");
        GUIStyle pre_button = new GUIStyle("preButton");

        GUILayout.Label("LABEL", pre_label);
        GUILayout.Button("BUTTON", pre_button);
    }
    // プレビュー表示
    public override void OnPreviewGUI(Rect r, GUIStyle background)
    {
        GUI.Box(r, "Preview");
    }
}