Unity3D Unsafe code requires the `unsafe' comm

2019-04-28 21:41发布

问题:

i am using Unity3D 4.3 and calling a DLL that i created. when trying to call the only function it have which is this:

void GetModelReferences(int &nVertices, float * vertices, int &nTriangles, int * triangles, float * normals, float * uvCoordinates);

unity gives me an error:

Unsafe code requires the `unsafe' command line option to be specified

so in my MonoDevelop i opened : Project->Assembly-Csharp options and turned on the unsafe mode.

it reduces part of the errors but this last one wont go away

Unsafe code requires the `unsafe' command line option to be specified

what shoud i do?

回答1:

Go to your project properties page and check under Build the checkbox Allow unsafe code. This should solve your problem.



回答2:

Here is what worked for me with Unity3D 5.3:
- If in your Unity Build Setting->Player Setting->Other Settings, "API Compatibility Level" is ".Net 2.0", create a file under your Unity Asset folder called gmcs.rsp, add this following line to the file:
-unsafe
Close Unity and open it again.
- If the "API Compatibility Level" is ".Net 2.0 Subset", the above file name should be: smcs.rsp.

See the picture bellow to find the location of ""API Compatibility Level" setting in Unity.



回答3:

The answer is given here: http://answers.unity3d.com/questions/804103/how-to-enable-unsafe-and-use-pointers.html

You need to add a file "smcs.rsp" (or "gmcs.rsp") in your "Assets" directory, which contains the line:

-unsafe


回答4:

Have you tried this ?

look at the fifth comment

Firstly show the contents of Unity.App and drill down to Contents/Frameworks/MonoCompiler.framework.

Open the file called compile_any.pl and in the section "# Run the csharp compiler" change @Base_Args options as below:

my @base_args=($mono_path, $mcs_path, '-debug', '-unsafe', '-target:library', '-nowarn:0169', '-out:'.$output, );



回答5:

You can change the settings through the MonoDevelop: go to the solution files tree -> Solution -> your expand your solution -> Assempely-CSharp -> Right Click -> options -> Build -> General -> Language options -> enable "Allow 'unsafe' code"