Unity3d中C#使用指標(Unsafe)的辦法

來源:互聯網
上載者:User

標籤:des   style   http   color   io   os   使用   ar   for   

近日由於在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());

 }

   

來源文件 <http://www.j2megame.com/html/xwzx/ty/3652.html>

Unity3d中C#使用指標(Unsafe)的辦法(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.