1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Unity】Editor拡張の設定保存&メニューバーにチェック表示

Posted at

はじめに

少し前に下記2つの記事で紹介したEdidor拡張をまとめてパッケージにして公開しました。

そのときに加えた改良点のご紹介です。

拡張機能のOn/Off状態を記憶

もともと下記に記載の手順で拡張機能のOn/Offを切り替えられるようにしていました。

しかしこれだけだと、起動する度に初期設定に戻ってしまって鬱陶しく感じることもあるかと思い、設定を記憶するようにしました。

保存

下記のコードでユーザ環境に値を保存することができます。

ユーザ環境に値を設定するコード
EditorUserSettings.SetConfigValue(キー, ));

こんな風にコンテキストメニューの該当項目が選択されたときに選択状態を保存するようにしました。

コンテキストメニューが選択されたら選択状態を保存
private const string ParentMenuPath = "GameObject/Switch Icon Visibility/";

private const string ComponentIconMenuPath = ParentMenuPath + "Component Icon";

private static bool componentIconEnabled = true;
 
[MenuItem(ComponentIconMenuPath, false, 10000)]
private static void ToggleComponentIconEnabled() {
    componentIconEnabled = !componentIconEnabled;

    // 更新処理

    // 状態保持
    EditorUserSettings.SetConfigValue(ComponentIconMenuPath, componentIconEnabled.ToString());
}

キーは任意の文字列で大丈夫ですが、ハードコーディングを極力避けるのと、定数を増やしたくなかったのでコンテキストメニューのパスを保持していた定数を利用することにしました。

読込

下記のコードで保存された値を読み込むことができます。

ユーザ環境から値を読み出すコード
EditorUserSettings.GetConfigValue(キー);
エディタ起動時に設定値を読み込む
private const string ParentMenuPath = "GameObject/Switch Icon Visibility/";

private const string ComponentIconMenuPath = ParentMenuPath + "Component Icon";

private const string MissingIndicatorMenuPath = ParentMenuPath + "Missing Indicator";

[InitializeOnLoadMethod]
private static void Initialize() {
    // セーブしていた値をロード
    var componentIconEnabledSetting = EditorUserSettings.GetConfigValue(ComponentIconMenuPath);
    var missingIndicatorEnabledSetting = EditorUserSettings.GetConfigValue(MissingIndicatorMenuPath);

    var componentIconEnabled = componentIconEnabledSetting == null || !string.Equals(componentIconEnabledSetting, false.ToString(), System.StringComparison.OrdinalIgnoreCase);
    var missingIndicatorEnabled = missingIndicatorEnabledSetting == null || !string.Equals(missingIndicatorEnabledSetting, false.ToString(), System.StringComparison.OrdinalIgnoreCase);

    // 設定値に応じた処理
}

拡張機能のOn/Offを可視化

もともとコンテキストメニューの選択でトグルで拡張機能のOn/Offが切り替わるようにしていました。
しかし、Onでも表示するものがない状態のときは、「表示がないのは対象物がないからのか、今Off状態だからなのかどっち?」となって数回メニューをクリックして確認することになり不便を感じていました。

コンテキストメニュー上ではメニュー項目にチェックを入れることはできないようなのですが、メニューバーの方では下記のメソッドでチェックOn/Offを行えます。

メニューバーのメニュー項目のチェックOn/Offを行うコード
Menu.SetChecked(メニューパス, チェック状態);

2つの拡張機能のメニュー選択時に、チェック表示を切り替えるようにしました。

メニュー選択時にチェック表示も切替
private const string ParentMenuPath = "GameObject/Switch Icon Visibility/";

private const string ComponentIconMenuPath = ParentMenuPath + "Component Icon";

private const string MissingIndicatorMenuPath = ParentMenuPath + "Missing Indicator";

private static bool componentIconEnabled = true;

private static bool missingIndicatorEnabled = true;
 
[MenuItem(ComponentIconMenuPath, false, 10000)]
private static void ToggleComponentIconEnabled() {
    componentIconEnabled = !componentIconEnabled;
    UpdateStatus();

    // 状態保持
    EditorUserSettings.SetConfigValue(ComponentIconMenuPath, componentIconEnabled.ToString());
}

private static void UpdateStatus() {
    // 更新処理

    Menu.SetChecked(ComponentIconMenuPath, componentIconEnabled);
    Menu.SetChecked(MissingIndicatorMenuPath, missingIndicatorEnabled);
}

こんな風にチェックが付きます。

メニューバーのメニュー項目にチェックが入っている状態.png

コンテキストメニューでもチェックが付いたらいいんですけどね。

参考プロジェクト

このEditor拡張のプロジェクトは下記に置いているので、必要に応じて参考にしてください。

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?