Unity功能——宏定义的使用

本文介绍Unity中宏定义的使用方法,包括如何通过宏定义实现平台特定代码的编译与执行,系统自带的宏关键词及其作用,以及如何自定义宏来满足特定需求。

声明:本文为个人笔记,用于学习研究使用非商用,内容为个人研究及综合整理所得,若有违规,请联系,违规必改。


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_EDITORScripting symbol to call Unity Editor scripts from your game code.
UNITY_EDITOR_WINScripting symbol for Editor code on Windows.
UNITY_EDITOR_OSXScripting symbol for Editor code on Mac OS X.
UNITY_EDITOR_LINUXScripting symbol for Editor code on Linux.
UNITY_STANDALONE_OSXScripting symbol to compile or execute code specifically for Mac OS X (including Universal, PPC and Intel architectures).
UNITY_STANDALONE_WINScripting symbol for compiling/executing code specifically for Windows standalone applications.
UNITY_STANDALONE_LINUXScripting symbol for compiling/executing code specifically for Linux standalone applications.
UNITY_STANDALONEScripting symbol for compiling/executing code for any standalone platform (Mac OS X, Windows or Linux).
UNITY_WIIScripting symbol for compiling/executing code for the Wii console.
UNITY_IOSScripting symbol for compiling/executing code for the iOS platform.
UNITY_IPHONE已弃用。改用 UNITY_IOS。
UNITY_ANDROIDScripting symbol for the Android platform.
UNITY_LUMINScripting symbol for the Magic Leap OS platform. You can also use PLATFORM_LUMIN.
UNITY_TIZENScripting symbol for the Tizen platform.
UNITY_TVOSScripting symbol for the Apple TV platform.
UNITY_WSAScripting 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_0Scripting symbol for Universal Windows Platform. Additionally WINDOWS_UWP is defined when compiling C# files against .NET Core.
UNITY_WEBGLScripting symbol for WebGL.
UNITY_FACEBOOKScripting symbol for the Facebook platform (WebGL or Windows standalone).
UNITY_ANALYTICSScripting symbol for calling Unity Analytics methods from your game code. Version 5.2 and above.
UNITY_ASSERTIONSScripting symbol for assertions control process.
UNITY_64Scripting 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ۓ明哲ڪ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值