Is there any Unity-specific reason that we should avoid unsafe code for pointers etc? I've enabled unsafe compilation for csharp in the compiler options and everything seems to work OK - just wanting to make sure Thx shaun
If you want to publish to the web player you should not use unsafe code as it will be disabled. If you are only doing standalone, thats fine.
Can u tell me please how do you enable unsafe compilation in the compiler options? Thanks a lot, Tomas
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: Code (csharp): my @base_args=($mono_path, $mcs_path, '-debug', '-unsafe', '-target:library', '-nowarn:0169', '-out:'.$output, ); Cheers Shaun
Is it still possible to run unmanaged code in stand-alone applications? Because i just tried to manipulate the compile-all.pl file as described, but doing so has no effect at all. Even deleting the script does not hinder Unity to compile scripts. Is there any other way to specify additional compiler command line options?
As the changes to that file affect the parameters passed to the mono compiler you'll likely need to force all of your scripts to recompile (I do this using the 'touch' command from osx/cygwin on each file to update its modification times). Not sure what your environment might be like, but any way of updating the modification time of the files (like modifying them for example) will make that happen.
Unity Pro supports unsafe code in all its various guises. Unsafe code does not work with the web player due to security reasons.
to be more precise about webplayer: Anything relying on P/Invoke is disabled and cut, not only unsafe code
Whilst that is true, the OP was asking specifically about unsafe code. We could go in to a long dissertation about what is and isn't possible in the web player, including speed and performance differences, and all it would do is muddy the point that the OP wanted to know if unsafe code is possible in Unity.
You can try using smcs.rsp/gmcs.rsp Basically add a file inside Assets/ called smcs.rsp/gmcs.rsp (It seems to be different per person). Inside that file you can add custom compiler commands, such as includes, defines, or flags. I've tested both defines and warnings as errors flag and they work. All you would need to do in there is add -unsafe. The thread that lead me that file was: http://forum.unity3d.com/threads/71445-How-To-Set-Project-Wide-pragma-Directives-with-JavaScript And it should have enough info to start testing it out.
This answers another question I had (http://answers.unity3d.com/questions/397565/enable-implicit-duck-typing-in-boo.html) but is this only possible in Pro?
We are improving the documentation on Custom Defines, and it's important to update these threads with the correct way of using them in the current versions of Unity. If you want to modify only global defines, you should use Scripting Define Symbols in Player Settings, because this will cover all the compilers. If you choose the .rsp files instead, you'll have to provide one file for every compiler Unity uses, and you won't know when one or another compiler is used. To do this you must add a text file with the extra directives to the "Assets/" folder: [table="width: 500"] [tr] [td]C#[/td] [td]<Project Path>/Assets/smcs.rsp[/td] [/tr] [tr] [td]C# - Editor Scripts[/td] [td]<Project Path>/Assets/gmcs.rsp[/td] [/tr] [tr] [td]UnityScript[/td] [td]<Project Path>/Assets/us.rsp[/td] [/tr] [tr] [td]Boo[/td] [td]<Project Path>/Assets/boo.rsp[/td] [/tr] [/table] 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 changes to .rsp files you will need to recompile for them to be effective. You can do this by updating or reimporting a single script (.js, .cs or .boo) file. All this information is being added to our documentation.
I need the global define '-unsafe' to work with .Net 2.0. It works fine when defined in '/Assets/smcs.rsp' under .Net 2.0 subset, but not full .Net 2.0. Why? And is there a workaround?
Ah, got it. I need to name the file 'gmcs.rsp' instead. http://answers.unity3d.com/questions/23307/is-it-impossible-to-use-the-code-with-keyword-unsa.html