How To Add Custom Define Symbols (Platform Dependent Compilation)
Updated 23 days ago
A Custom Define Symbols Utility
Here is the script:

Why use it?

Have you ever wanted to create your own Define Symbol to avoid errors if a specific plugin was not installed? Our team at BlackBox has. While working in a project that has many submodules and custom plugins, it's important to avoid requiring everyone to download a specific asset.

What is it?

The script allows you to call DefineSymbolsUtility.AddDefineSymbol() from any Editor script.

How does it work?

The script uses PlayerSettings.GetScriptingDefineSymbolsForGroup() and then splits up the string to a list. After the list is created it adds or removes your defined symbol.
Checking for a required file is your responsibility.
That said, we added some class to check for specific files: EditorFileUtility.FileExistsInAbsolutePath() checks for a path. the start path is the project root folder (outside of the Assets). You can also check for a valid folder in your assets by calling EditorFileUtility.ValidFolder()
You can use the code mentioned above as a condition to add or remove a definition. I would recommend creating a script in the Editor folder called [your-plugin-here]DefineSymbol.cs and add the following:
#if ![your-plugin-DefineSymbol] using System.Collections; using Common.EditorUtilities; using UnityEditor; using UnityEngine; [UnityEditor.InitializeOnLoad] public static class [your-plugin-here]DefineSymbol { static [your-plugin-here]DefineSymbol() { if (AssetDatabaseUtility.HasAsset("YOUR ASSET") || EditorFileUtility.ValidFolder("YOUR FOLDER")) { DefineSymbolsUtility.AddDefineSymbol("[your-plugin-DefineSymbol]"); Debug.Log("Added your-plugin Define Symbol."); } } } #endif

And that's it! You will now have your custom definition added if the condition is met.
Need help with your project? As a Live Help Expert I enjoy solving development roadblocks. If you have a question or find yourself stuck, please reach out. My sessions provide free consultation and a 100 satisfaction guarantee.
Adrian Babilinski
Unity Developer | XR Guru - Programmer
Krystian Babilinski
Unity Developer - Programmer