声明:本文为个人笔记,用于学习研究使用非商用,内容为个人研究及综合整理所得,若有违规,请联系,违规必改。
Unity功能——宏定义的使用
文章目录
- Unity功能——宏定义的使用
- 一.开发环境
- 二.问题描述
- 三.宏的使用原理及代码格式.
- 实现原理:
- 对应代码:
- 四.系统自带的宏关键词
- 4.1中文版本(机翻):
- 五.自定义宏
- 5.1 Unity中自定义宏操作:
- 六.总结
- 七.官网文档链接
一.开发环境
Unity 版本无限制
VS 版本无限制
二.问题描述
仅在对应平台(PC/Android/IOS等)运行对应代码
三.宏的使用原理及代码格式.
实现原理:
添加对应宏定义,达到定义内代码在对应平台运行.
对应代码:
在函数(方法)内使用# if +对应宏为开头,以 #endif为结尾,可以使用elif /else等.
#if UNITY_EDITOR
Debug.Log("Editor");
#elif UNITY_STANDALONE
Debug.Log("UNITY_STANDLONE");
#elif UNITY_ANDROID||UNITYIOS
Debug.Log("UNITY_ANDROID||UNITY_IOS");
#endif
Debug.Log("DoubleTap");
如图一所示.
图一
四.系统自带的宏关键词
Define | 功能 |
---|---|
UNITY_EDITOR | Scripting symbol to call Unity Editor scripts from your game code. |
UNITY_EDITOR_WIN | Scripting symbol for Editor code on Windows. |
UNITY_EDITOR_OSX | Scripting symbol for Editor code on Mac OS X. |
UNITY_EDITOR_LINUX | Scripting symbol for Editor code on Linux. |
UNITY_STANDALONE_OSX | Scripting symbol to compile or execute code specifically for Mac OS X (including Universal, PPC and Intel architectures). |
UNITY_STANDALONE_WIN | Scripting symbol for compiling/executing code specifically for Windows standalone applications. |
UNITY_STANDALONE_LINUX | Scripting symbol for compiling/executing code specifically for Linux standalone applications. |
UNITY_STANDALONE | Scripting symbol for compiling/executing code for any standalone platform (Mac OS X, Windows or Linux). |
UNITY_WII | Scripting symbol for compiling/executing code for the Wii console. |
UNITY_IOS | Scripting symbol for compiling/executing code for the iOS platform. |
UNITY_IPHONE | 已弃用。改用 UNITY_IOS。 |
UNITY_ANDROID | Scripting symbol for the Android platform. |
UNITY_LUMIN | Scripting symbol for the Magic Leap OS platform. You can also use PLATFORM_LUMIN. |
UNITY_TIZEN | Scripting symbol for the Tizen platform. |
UNITY_TVOS | Scripting symbol for the Apple TV platform. |
UNITY_WSA | Scripting symbol for Universal Windows Platform. Additionally, NETFX_CORE is defined when compiling C# files against .NET Core and using .NET scripting backend. |
UNITY_WSA_10_0 | Scripting symbol for Universal Windows Platform. Additionally WINDOWS_UWP is defined when compiling C# files against .NET Core. |
UNITY_WEBGL | Scripting symbol for WebGL. |
UNITY_FACEBOOK | Scripting symbol for the Facebook platform (WebGL or Windows standalone). |
UNITY_ANALYTICS | Scripting symbol for calling Unity Analytics methods from your game code. Version 5.2 and above. |
UNITY_ASSERTIONS | Scripting symbol for assertions control process. |
UNITY_64 | Scripting symbol for 64-bit platforms. |
4.1中文版本(机翻):
五.自定义宏
除了默认的宏定义外,可以通过Unity自定义宏
5.1 Unity中自定义宏操作:
BuildSettings(Ctrl+Shift+B)——PlayerSettings——OtherSettings——Scripting Define Symbols
多个宏定义用 ; (分号)隔开;
注意事项:打包(构建)测试时,如果使用了测试才用的自定义宏,正版发布版本别忘了取消.
六.总结
保持饥饿,保持愚蠢.
这世界唯一能够相信的就是你付出的努力和你走过的路.
七.官网文档链接
宏定义官方文档链接
https://docs.unity3d.com/Manual/PlatformDependentCompilation.html