LoginSignup
0
1

UPM形式のUnityパッケージをインストールしたときにScripting Define Symbolsを自動設定したい

Last updated at Posted at 2024-02-17

下記のコードをUnity Package内のEditorディレクトリに内に含めることで、パッケージインストール時やSwitch Platform時に現在のプラットフォームのProject SettingsのScripting Define Symbolsにシンボルを追加することができる

using System.Linq;
using UnityEditor;

[InitializeOnLoad]
class AddDefineSymbols
{
    // Scripting Define Symbol list to add
    static readonly string[] symbolsToAdd = {
            "UNITYGLTF_FORCE_DEFAULT_IMPORTER_ON",         // Enable UnityGLTF import
            "GLTFAST_FORCE_DEFAULT_IMPORTER_OFF",          // Disable GLTFast import
            "UNIGLTF_DISABLE_DEFAULT_GLTF_IMPORTER",    // Disable importer in UniVRM
            "UNIGLTF_DISABLE_DEFAULT_GLB_IMPORTER",     // Disable importer in UniVRM
        };

    // This method will be called on load of Unity Editor
    static AddDefineSymbols()
    {
        AddDefineSymbolsToPlayerSettings(symbolsToAdd);
    }

    private static void AddDefineSymbolsToPlayerSettings(string[] symbols)
    {
        // Get the current list of defined symbols for the active build target
        string definesString = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup);
        var allDefines = definesString.Split(';').ToList();
        bool symbolsAdded = false;

        foreach (var symbol in symbols)
        {
            if (!allDefines.Contains(symbol))
            {
                allDefines.Add(symbol);
                symbolsAdded = true;
            }
        }

        // Only update the define symbols if new symbols were actually added
        if (symbolsAdded)
        {
            PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, string.Join(";", allDefines.ToArray()));
        }
    }
}

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