Macro definitions can easily distinguish the codes used in different situations.
The more classic ones are UNITY_EDITOR
this kind
#if UNITY_EDITOR
Debug.Log("当前是编辑器环境");
#else
Debug.Log("当前不是编辑器环境");
#endif
Developers can also define their own macros for some control, as shown in the following figure:
However, it is a bit troublesome to manually add and modify by yourself every time, so there is a way to dynamically set macro definitions through code. It is convenient for developers to turn on and off a certain function at any time.
The core interfaces are:
PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup targetGroup);
and
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup targetGroup, string defines);
public class TestEditor : MonoBehaviour
{
#if !TEST_DEFINE_SYMBOLS
[MenuItem("TestTools/[OFF] TestDefineSymbols", false, 1000)]
public static void MenuTestDefineSymbols()
{
// 动态获取 Android 平台的宏定义
string tmpSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
if (string.IsNullOrEmpty(tmpSymbols))
{
tmpSymbols += "TEST_DEFINE_SYMBOLS";
}
else
{
tmpSymbols += ";TEST_DEFINE_SYMBOLS";
}
// 动态修改宏定义
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, tmpSymbols);
}
#else
[MenuItem("TestTools/[ON] TestDefineSymbols", false, 1000)]
public static void MenuTestDefineSymbols()
{
// 动态获取 Android 平台的宏定义
string tmpSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
string[] symbolArray = tmpSymbols.Split(';');
for (int i = 0; i < symbolArray.Length; i++)
{
if(symbolArray[i] == "TEST_DEFINE_SYMBOLS")
{
symbolArray[i] = "";
}
}
tmpSymbols = "";
for (int i = 0; i < symbolArray.Length; i++)
{
if (string.IsNullOrEmpty(symbolArray[i])) continue;
if(tmpSymbols == "")
{
tmpSymbols += symbolArray[i];
}
else
{
tmpSymbols += ";";
tmpSymbols += symbolArray[i];
}
}
// 动态修改宏定义
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, tmpSymbols);
}
#endif
#if !TEST_DEFINE_SYMBOLS_CHECK
[MenuItem("TestTools/CheckDefineSymbols", false, 1000)]
public static void MenuTestDefineSymbols_Check()
{
// 动态获取 Android 平台的宏定义
string tmpSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
if (string.IsNullOrEmpty(tmpSymbols))
{
tmpSymbols += "TEST_DEFINE_SYMBOLS_CHECK";
}
else
{
tmpSymbols += ";TEST_DEFINE_SYMBOLS_CHECK";
}
// 动态修改宏定义
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, tmpSymbols);
// 设置勾选状态
Menu.SetChecked("TestTools/CheckDefineSymbols", true);
}
#else
[MenuItem("TestTools/CheckDefineSymbols", false, 1000)]
public static void MenuTestDefineSymbols_Check()
{
// 动态获取 Android 平台的宏定义
string tmpSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
string[] symbolArray = tmpSymbols.Split(';');
for (int i = 0; i < symbolArray.Length; i++)
{
if(symbolArray[i] == "TEST_DEFINE_SYMBOLS_CHECK")
{
symbolArray[i] = "";
}
}
tmpSymbols = "";
for (int i = 0; i < symbolArray.Length; i++)
{
if (string.IsNullOrEmpty(symbolArray[i])) continue;
if(tmpSymbols == "")
{
tmpSymbols += symbolArray[i];
}
else
{
tmpSymbols += ";";
tmpSymbols += symbolArray[i];
}
}
// 动态修改宏定义
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, tmpSymbols);
// 取消勾选状态
Menu.SetChecked("TestTools/CheckDefineSymbols", false);
}
#endif
}
The above are two ways of expression and their corresponding codes. Here we take the Android platform as an example. You can also rewrite or use macro commands to configure multi-platform styles according to your own needs. You can refer to the following figure:
Finally, it should be noted that every time the macro command is changed, it needs to wait for the system to recompile. If other operations are performed without waiting for the compilation to complete, it may cause bugs due to the macro command not being added or deleted.