- Home /
Is it impossible to use the code with keyword "unsafe" in C#?
Hi,
Is it impossible to use the code with keyword "unsafe" from C# in Unity3d? I have to use the code with "unsafe". Have anybody idea?
Thanks a lot, Max
I second @SpikeX's question as to what your goal is - are you trying to use some third-party code in a .dll (which requires Pro)?
@$$anonymous$$ax, unless you're writing Halo 3, you really don't need to worry about performance yet :) C# (and JS) is compiled before final execution, and runs quite well. Check the performance threads to see.
@$$anonymous$$ax, also you should checkmark the answer - even if it's not the one you wanted, it is correct. :)
Answer by bamboo · Nov 19, 2013 at 02:49 PM
Yes. Create a smcs.rsp file (or gmcs.rsp if you are using the full .NET 2.0 profile) in Assets/ with -unsafe in it.
Are you sure that works? On which platforms have you tested it?
@ibankzero you said tested in ios & android, what did you actually test? did you try to run unsafe code in your game?
Answer by qJake · Jul 25, 2010 at 11:51 PM
No, you probably can't, since all unsafe code requires the /unsafe
compile-time directive, and you can't specify those in Unity.
MSDN:
To compile unsafe code, you must specify the /unsafe compiler option. Unsafe code is not verifiable by the common language runtime.
http://msdn.microsoft.com/en-us/library/chfa2zb8(VS.71).aspx
What purpose would you even have for using unsafe
in your game? I'm pretty sure there's another way to do what you're trying to do.
To use pointers. Thus we get a better performance. Example:
{ ... return (double)&value; }
I don't think you'll get that much more performance by using pointers, and it's not worth the trouble. Besides, everything in Unity uses floats, which are more efficient (and take up less memory) than doubles. You probably don't want to be using doubles -or- pointers.
Your answer

Follow this Question
Related Questions
Method contains unsupported native code 1 Answer
platform dependencies using InteropServices.StructLayout Explicit 1 Answer
About UNSAFE CODE 0 Answers
Does Unity define a Preprocessor Directive for 'UNSAFE'? 0 Answers
Compiler options, unsafe 2 Answers