Unity Community

Register or Sign In:

+ Reply to Thread
Results 1 to 15 of 15

  1. Location
    Cmune HQ
    Posts
    716

    Unsafe code in Unity

    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

  2. Unity CTO
    Location
    Unity Technologies HQ, Copenhagen
    Posts
    3,478
    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.
    Joachim Ante


  3. Location
    Cmune HQ
    Posts
    716
    I guessed that might be the case. Thanks


  4. Location
    Rhode Island
    Posts
    21
    Can u tell me please how do you enable unsafe compilation in the compiler options?

    Thanks a lot,
    Tomas


  5. Location
    Cmune HQ
    Posts
    716
    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:  
    1. my @base_args=($mono_path, $mcs_path, '-debug', '-unsafe', '-target:library', '-nowarn:0169', '-out:'.$output,  );

    Cheers
    Shaun

  6. PiA PiA is offline

    Posts
    59
    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?


  7. Posts
    16
    Hi I'm also looking for an answer to this... any news on a solution?

    Cheers,


  8. Posts
    13
    I think you can use unsafe code with C#, but you need the pro license of the engine.


  9. Posts
    7
    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.
    "Order and Protection. In order that they may rot in comfort." - Miller


  10. Posts
    29
    I am wondering how to make this change in Unity 3.0 Pro (Windows platform). Any ideas?
    Last edited by Sprocket; 11-06-2010 at 07:28 AM.


  11. Location
    Melbourne, Australia
    Posts
    532
    Quote Originally Posted by ocuspocus View Post
    I think you can use unsafe code with C#, but you need the pro license of the engine.
    Even for web?


  12. Posts
    1,609
    Unity Pro supports unsafe code in all its various guises. Unsafe code does not work with the web player due to security reasons.


  13. Location
    Zόrich, Switzerland
    Posts
    25,088
    to be more precise about webplayer: Anything relying on P/Invoke is disabled and cut, not only unsafe code


  14. Posts
    1,609
    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.


  15. Location
    Montreal, Canada
    Posts
    1,437
    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/714...ith-JavaScript

    And it should have enough info to start testing it out.