近日由於在U3D項目中要使用到數據傳遞(C++ DLL的數據傳遞給U3D中的C#),其中涉及到需要使用C#的指針。直接編譯會出現以下錯誤Unsafe code requires the 'unsafe' command line option to be specified。
下面是我總結的解決辦法:
1.去除MONO編輯器中的Unsafe錯誤,Assembly-CSharp鼠標右鍵找到Options->Build->General 。Allow 'unsafe' code 打鈎。這個只能去除MONO報錯,但是依然無法運行。
2.首先看下面一段比較長的
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".
在你的Assets目錄下面添加smcs.rsp文件,里面只加一行字不要有空格 -unsafe。 OK搞定。記得一定要重啟Unity3d, 因為這個預編譯是在啟動U3D時候運行的。工程文件名別帶中文。
原理是編輯器中的smcs.exe 添加編譯命令,也可以在CMD下運行編輯器目錄下的smcs.exe 逐個添加,會很累的。
測試代碼:
unsafe void test () {
int i=10;
int k;
int *j=&i;
k=*j+1;
print("unsafe test " + k.ToString());
}