我正在开发一个现有的移动应用程序。下面是一些类似的情况:
private void Example()
{
//some universal code
#if UNITY_EDITOR
return;
#endif
#pragma warning disable CS0162 // Unreachable code detected
//some mobile platform code
#pragma warning restore CS0162 // Unreachable code detected
}
我会像这样在过去实现它:
private void Example()
{
//some universal code
#if !UNITY_EDITOR
//some mobile platform code
#endif
}
UNITY_EDITOR版本看起来非常丑陋,我无法在函数的末尾添加通用代码。另一方面,当在编辑器中工作时,它的优点是能够找到对移动代码的引用。
我是不是漏掉了什么?有没有可能改变Visual Studio,让它在!UNITY_EDITOR代码中找到引用并突出显示代码?
发布于 2021-11-22 17:05:31
你说得对,这看起来确实很丑陋。在我的项目中,我发送了一个定义平台设置的文件,在这里我会这样利用它:
public static class ApplicationSettings
{
#if UNITY_EDITOR
public static bool IsUnityEditor = true;
#else
public static bool IsUnityEditor = false;
#endif
#if PLATFORM_STANDALONE_WIN
public static bool IsPlatformStandaloneWin = true;
#else
public static bool IsPlatformStandaloneWin = false;
#endif
... etc ...
}
然后,如果你的代码库确实基于平台做了特定的事情,那么就在这个条件上进行分支。
public void Example()
{
if (ApplicationSettings.IsPlatformStandaloneWin)
StandaloneWindowsExample(); // windows specific
else
EveryoneElseExample(); // other platforms:
... // code that applies to all platforms
}
或者甚至为您所支持的平台定义您自己的枚举,并打开它的值。
https://stackoverflow.com/questions/70066390
复制相似问题