Skip to content

Commit 7723dc1

Browse files
committed
update
1 parent 013ed01 commit 7723dc1

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.DS_Store
2+
*.meta
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "Unity.DebugModeDefineSymbol.Editor"
3+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
11
# UnityDebugModeDefineSymbol
2-
A useful tool for running specific code
2+
A useful tool for running specific code <br>
3+
[What it is?](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if)
4+
5+
6+
### Compatibility
7+
| Unity | Compatible |
8+
|:-------:|:----------:|
9+
| 2020 |:white_check_mark:|
10+
| 2019 |:white_check_mark:|
11+
12+
## How to use
13+
1. Add source code of this repo to your project
14+
2. Manage debug mode by following this path `Tools -> Debug Mode Definition ->` `On` / `Off`
15+
<img width="372" alt="choiceDiaologWindow" src="https://user-images.githubusercontent.com/29813954/110636484-fa65bd00-81b4-11eb-88e9-c0b55ad82a21.png">
16+
17+
3. For code that should be executed only when the debug mode is turned on, put it in the definition like this:
18+
19+
```c#
20+
// ... code somewhere
21+
#if DEBUG_MODE_IN_USE
22+
// code here will be executed if the debug mode is turned on
23+
#endif
24+
// ... code somewhere
25+
```

0 commit comments

Comments
 (0)