1+ using System . Collections . Generic ;
2+ using UnityEditor ;
3+ using UnityEngine ;
4+
5+ namespace UnityDebugModeDefineSymbol . Editor
6+ {
7+ internal static class DebugModeDefineSymbol
8+ {
9+ private static bool _debugIsOn ;
10+ private const string debugModeSymbol = "DEBUG_MODE_IN_USE" ;
11+
12+
13+ [ MenuItem ( "Tools/Debug Mode Definition/On" ) ]
14+ internal static void TurnOnDebugModeDefinition ( )
15+ {
16+ _debugIsOn = GetState ( ) ;
17+ if ( _debugIsOn )
18+ {
19+ EditorUtility . DisplayDialog ( "Debug mode definition is turned On" ,
20+ "DEBUG_MODE_IN_USE is already included in the scripting define symbols" , "OK" ) ;
21+ }
22+ else
23+ {
24+ if ( EditorUtility . DisplayDialog ( "Turn on the debug mode definition?" ,
25+ "If the debug mode enabled, all the code in the definition 'DEBUG_MODE_IN_USE' will be executed" , "Yes" , "No" ) )
26+ {
27+ _debugIsOn = true ;
28+ PlayerPrefs . SetInt ( "debugModeInUseKey" , 1 ) ;
29+ DefineSymbols ( ) ;
30+ }
31+ }
32+ }
33+ [ MenuItem ( "Tools/Debug Mode Definition/Off" ) ]
34+ internal static void TurnOffDebugModeDefinition ( )
35+ {
36+ _debugIsOn = GetState ( ) ;
37+ if ( ! _debugIsOn )
38+ {
39+ EditorUtility . DisplayDialog ( "Debug mode definition is already turned off" ,
40+ "The code in the 'DEBUG_MODE_IN_USE' definition is not executed" , "OK" ) ;
41+ }
42+ else
43+ {
44+ if ( EditorUtility . DisplayDialog ( "Turn off the debug mode definition?" ,
45+ "The code in the 'DEBUG_MODE_IN_USE' definition will not be execute" , "Yes" , "No" ) )
46+ {
47+ _debugIsOn = false ;
48+ PlayerPrefs . SetInt ( "debugModeInUseKey" , 0 ) ;
49+ DefineSymbols ( ) ;
50+ }
51+ }
52+ }
53+
54+ private static bool GetState ( ) => PlayerPrefs . GetInt ( "debugModeInUseKey" , 0 ) == 1 ;
55+
56+ private static void DefineSymbols ( )
57+ {
58+ BuildTargetGroup currentTarget = EditorUserBuildSettings . selectedBuildTargetGroup ;
59+
60+ List < string > scriptingSymbols =
61+ new List < string > ( PlayerSettings . GetScriptingDefineSymbolsForGroup ( currentTarget ) . Split ( ';' ) ) ;
62+
63+ if ( _debugIsOn )
64+ {
65+ if ( ! scriptingSymbols . Contains ( debugModeSymbol ) ) scriptingSymbols . Add ( debugModeSymbol ) ;
66+ else return ;
67+ }
68+ else
69+ {
70+ if ( scriptingSymbols . Contains ( debugModeSymbol ) ) scriptingSymbols . Remove ( debugModeSymbol ) ;
71+ else return ;
72+ }
73+
74+ string finalScriptingSymbols = string . Join ( ";" , scriptingSymbols . ToArray ( ) ) ;
75+ PlayerSettings . SetScriptingDefineSymbolsForGroup ( currentTarget , finalScriptingSymbols ) ;
76+ }
77+ }
78+ }
0 commit comments