可以在Unity Assets目錄下創(chuàng)建smcs.rsp文件,并向其中添加預(yù)編譯命令,其會(huì)在unity啟動(dòng)時(shí)執(zhí)行,比如新建一個(gè)smcs.rsp文件,向其中添加內(nèi)容:
-define:MYDEF
然后就可以在腳本中加入宏判斷:
#if MYDEF
....
#endif
其原理是啟動(dòng)Unity時(shí)會(huì)執(zhí)行unity目錄下的smcs.exe文件并添加預(yù)編譯命令,也可以通過(guò)cmd運(yùn)行smcs.exe逐個(gè)添加預(yù)編譯命令。
另外還有可以創(chuàng)建gmcs.rsp文件,對(duì)應(yīng)Editor腳本中的預(yù)編譯命令。
詳細(xì):
Custom Preprocessor Directives
It is also possible to define your own preprocessor directives to control which code gets included when compiling. To do this you must add in the "Assets/" folder a text file with the extra directives. The name of the file depends on the language you are using :
C#
<Project Path>/Assets/smcs.rsp
C# - Editor Scripts
<Project Path>/Assets/gmcs.rsp
UnityScript
<Project Path>/Assets/us.rsp
Boo
<Project Path>/Assets/boo.rsp
As an example, if you include the single line '-define:UNITY_DEBUG' in your smcs.rsp file the define UNITY_DEBUG will exist as a global define for C# scripts, except for Editor scripts.
Every time you make make changes to the .rsp files a recompilation needs to be done for them to be effective. You can do this by updating or reimporting a single script (.js, .cs or .boo) file.
The usage of the .rsp files is described in the help of the smcs application, included in the Editor installation folder. You can get more information by running : "smcs -help".
比如如果想要在C#語(yǔ)言中使用指針,必須標(biāo)記為unsafe的,默認(rèn)情況下unity中使用unsafe標(biāo)記會(huì)報(bào)錯(cuò),可以在項(xiàng)目中添加smcs.rsp文件并加入-unsafe預(yù)編譯命令,就可以編譯通過(guò)。