Skip to content

Instantly share code, notes, and snippets.

@derwodaso
Created March 14, 2017 10:34
Show Gist options
  • Save derwodaso/ffd6ca4541dff60f4d06d8243182a802 to your computer and use it in GitHub Desktop.
Save derwodaso/ffd6ca4541dff60f4d06d8243182a802 to your computer and use it in GitHub Desktop.
Disable / Enable ScriptingDefineSymbolds from menu
/* To avoid performance issues caused by string constructions when logging stuff
* to the console, simply surround the code with the the following:
*
* #if DEBUG_LOGGING
* Debug.Log("my" + numer.ToString("0.00") " super" + " expensive " + " string building code");
* #endif
*
* When creating a non-debug build or testing the game for performance, simply disable
* the debug messages from the Unity menu (menu name can be simply changed below).
*/
using UnityEditor;
using System.Collections.Generic;
class ScriptingDefineSymbolToggler
{
// Change the following string to match your needs...
const string MENU_FOLDERS = "Megagon Industries/Console/";
const string MENU_ITEM_NAME = "Debug Logging";
// If you change the following symbol, make sure to also adjust it below
const string DEBUG_LOGGING_SYMBOL = "DEBUG_LOGGING";
// Show menu entry to disable symbol (but keep public method always available)
#if DEBUG_LOGGING
[MenuItem(MENU_FOLDERS + "Disable " + MENU_ITEM_NAME)]
#endif
public static void DebugLoggingOff()
{
SetSymbol(DEBUG_LOGGING_SYMBOL, false);
}
// Show menu entry to enable symbol (but keep public method always available)
#if !DEBUG_LOGGING
[MenuItem(MENU_FOLDERS + "Enable " + MENU_ITEM_NAME)]
#endif
public static void DebugLoggingOn()
{
SetSymbol(DEBUG_LOGGING_SYMBOL, true);
}
public static void SetSymbol(string symbol, bool active)
{
// Get current list of scripting define symbols
BuildTargetGroup buildTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup);
List<string> symbols = new List<string>(defines.Split(';'));
// Enable / Disable the passed in symbol
if(active)
{
if(!symbols.Contains(symbol))
symbols.Add(symbol);
}
else
{
if(symbol.Contains(symbol))
symbols.Remove(symbol);
}
// Build string from list of symbols separated by ';'
defines = "";
for(int i = 0; i < symbols.Count; i++)
{
defines += symbols[i];
if(i < symbols.Count - 1)
defines += ";";
}
// Apply new symbol list
PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, defines);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment