はなちるのマイノート

Unityをメインとした技術ブログ。自分らしくまったりやっていきたいと思いますー!

【Unity】ライトテーマを利用しているかダークテーマを利用しているかをスクリプトで調べる・設定する方法(Preferencesのkeyの探し方)

はじめに

今回はUnity Editorのテーマをスクリプトから取得しようという記事になります。

GUI上での変更方法

書く必要はないかもしれませんが、GUI上での変更方法についても記載しておきます。

Macの場合はメニューバーよりUnity -> Settings...を選択しPreferencesを開き、Preferences -> General -> Editor ThemeLightDarkに変更します。

Editor Theme変更

スクリプトでの取得

Preferencesで設定されている値はEditorPrefsを利用することで取得・設定することができます。

結構見つけるのが大変でしたが、UserSkinというkeyにて取得することができます。

// Editor Themeを取得する
// 0 = Light, 1 = Dark
var userSkin = EditorPrefs.GetInt("UserSkin");

またEditorPrefs.SetIntを実行した後にInternalEditorUtility.SwitchSkinAndRepaintAllViews()を実行することでEditorに適応することができます。

// Editor Themeを設定する
EditorPrefs.SetInt("UserSkin", 1);
        
// Editor Themeを適応する
InternalEditorUtility.SwitchSkinAndRepaintAllViews();

Preferencesのkeyの探し方

以下のファイルにEditorPrefsのデータが格納されています。

  • Mac : ~/Library/Preferences/com.unity3d.UnityEditor5.x.plist
  • Windows : HKCU\Software\Unity Technologies\UnityEditor 5.x key

On macOS, EditorPrefs are stored in ~/Library/Preferences/com.unity3d.UnityEditor5.x.plist.

On Windows, EditorPrefs are stored in the registry under the HKCU\Software\Unity Technologies\UnityEditor 5.x key.

EditorPrefs - Unity スクリプトリファレンス

Macの場合はXCodeで開き、該当のそれっぽいkeyを探しましょう。

com.unity3d.UnityEditor5.x.plist

またUnityCsReferenceを見ることでkeyの予測の手助けになったりもします。
github.com