LoginSignup
3
1

More than 5 years have passed since last update.

エディタスクリプトでPlayerSettingsのScripting Backendの値を取得したり更新する

Posted at

はじめに

PlayerSettingsクラスあたりにIL2CPPとそうじゃないのを切り替えるためのプロパティないかと調べていたらなかったので、無理やり値を取ったり更新する処理を組んでみる。

あくまで無理矢理です。本当にこれでいいのかは自己判断してください

取得と設定の関数を探す

Google先生に聞いたら、フォーラムにそれらしきものを見つける。
http://answers.unity3d.com/questions/1037288/how-do-i-check-for-the-il2cpp-scripting-backendset.html

PlayerSettings.SetPropertyIntで設定、PlayerSettings.GetPropertyIntで取得、と。
つまり、直接設定を更新するためのプロパティが公開されていないので、このようなやり方になる模様。

SetScriptingBackendProperty.cs
PlayerSettings.SetPropertyInt("ScriptingBackend",
    (int)ScriptingImplementation.IL2CPP, 
    EditorUserBuildSettings.selectedBuildTargetGroup
);

まだ動かない

で、エディタスクリプトでSetPropertyIntGetPropertyIntを使ってみたのだけれど、コーンソールに「初期化されていないプロパティだゾ」と真っ赤なエラーログ。

初期化必要ようです。
エラーログに「InitializePropertyInt」で初期化しろって書いてあるけど・・・。

この関数も公開されていない。

初期化の関数を無理やり動かす

ILSpyでUnityEditor.PlayerSettingsクラスの中身を見てみる。image

いた。こいつ。
(Scripting Backendの項目はScriptingImplementationという列挙型使っている)

さて、こいつをReflectionで呼び出す。

CallInitializePropertyEnum.cs
    private static void InitScriptingBackendProperty()
    {
        var typeObject = typeof(PlayerSettings);
        var method = typeObject.GetMethod("InitializePropertyEnum", BindingFlags.Static | BindingFlags.NonPublic, null, new System.Type[] { typeof(string), typeof(object), typeof(BuildTargetGroup) }, null);

        method.Invoke(null, new object[] { "ScriptingBackend", (object)ScriptingImplementation.Mono2x, (object)EditorUserBuildSettings.selectedBuildTargetGroup });
    }

動作確認。
あとは、さっきのSetPropertyInt読んであげれば、エディタスクリプトで一応更新できたように見える。
ビルドをすると、ちゃんとコンバートされているのも確認。

最後に

昔のメカニムとかメッシュのロード時のGameObjectの最適化あたりみたいに、そのうちプロパティが公開されるのかな。

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