• Unity3d中C#使用指针(Unsafe)的办法(转)


    近日由于在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>

  • 相关阅读:
    bzoj3531[Sdoi2014]旅行
    bzoj3212 Pku3468 A Simple Problem with Integers 线段树
    bzoj1858[Scoi2010]序列操作 线段树
    bzoj2243[SDOI2011]染色 树链剖分+线段树
    bzoj3038上帝造题的七分钟2
    bzoj1036[ZJOI2008]树的统计Count 树链剖分+线段树
    bzoj3211花神游历各国 线段树
    bzoj4596[Shoi2016]黑暗前的幻想乡 Matrix定理+容斥原理
    bzoj3129[Sdoi2013]方程 exlucas+容斥原理
    刷题总结——寻宝游戏(bzoj3991 dfs序)
  • 原文地址:https://www.cnblogs.com/zsb517/p/4012858.html
Copyright © 2020-2023  润新知