1. 程式人生 > >通過Unity預編譯檔案smcs.rsp檔案新增預編譯命令

通過Unity預編譯檔案smcs.rsp檔案新增預編譯命令

可以在Unity Assets目錄下建立smcs.rsp檔案,並向其中新增預編譯命令,其會在unity啟動時執行,比如新建一個smcs.rsp檔案,向其中新增內容:
-define:MYDEF
然後就可以在指令碼中加入巨集判斷:

if MYDEF

….

endif

其原理是啟動Unity時會執行unity目錄下的smcs.exe檔案並新增預編譯命令,也可以通過cmd執行smcs.exe逐個新增預編譯命令。
另外還有可以建立gmcs.rsp檔案,對應Editor指令碼中的預編譯命令。
詳細:
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#
/Assets/smcs.rsp

C# - Editor Scripts
/Assets/gmcs.rsp

UnityScript
/Assets/us.rsp

Boo
/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#語言中使用指標,必須標記為unsafe的,預設情況下unity中使用unsafe標記會報錯,可以在專案中新增smcs.rsp檔案並加入-unsafe預編譯命令,就可以編譯通過。