通過Unity預(yù)編譯文件smcs.rsp文件添加預(yù)編譯命令

可以在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ù)編譯命令,也可以通過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#語言中使用指針,必須標(biāo)記為unsafe的,默認(rèn)情況下unity中使用unsafe標(biāo)記會(huì)報(bào)錯(cuò),可以在項(xiàng)目中添加smcs.rsp文件并加入-unsafe預(yù)編譯命令,就可以編譯通過。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容