1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

Unsafe code in Unity

Discussion in 'Scripting' started by shaun, Nov 1, 2007.

  1. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    725
    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. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    3,709
    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.
     
  3. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    725
    I guessed that might be the case. Thanks
     
  4. Tomas

    Tomas

    Joined:
    Mar 30, 2007
    Posts:
    21
    Can u tell me please how do you enable unsafe compilation in the compiler options?

    Thanks a lot,
    Tomas :)
     
  5. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    725
    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):
    1. my @base_args=($mono_path, $mcs_path, '-debug', '-unsafe', '-target:library', '-nowarn:0169', '-out:'.$output,  );
    Cheers
    Shaun
     
  6. Pia

    Pia

    Joined:
    Feb 16, 2009
    Posts:
    78
    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. Clueless

    Clueless

    Joined:
    Nov 21, 2009
    Posts:
    16
    Hi I'm also looking for an answer to this... any news on a solution?

    Cheers,
     
  8. ocuspocus

    ocuspocus

    Joined:
    Jul 29, 2009
    Posts:
    13
    I think you can use unsafe code with C#, but you need the pro license of the engine.
     
  9. ryan_scott

    ryan_scott

    Joined:
    Jun 12, 2009
    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.
     
  10. Sprocket

    Sprocket

    Joined:
    Sep 18, 2010
    Posts:
    29
    I am wondering how to make this change in Unity 3.0 Pro (Windows platform). Any ideas?
     
    Last edited: Nov 6, 2010
  11. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    760
    Even for web?
     
  12. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,674
    Unity Pro supports unsafe code in all its various guises. Unsafe code does not work with the web player due to security reasons.
     
  13. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,596
    to be more precise about webplayer: Anything relying on P/Invoke is disabled and cut, not only unsafe code
     
  14. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,674
    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. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    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.
     
  16. Foam

    Foam

    Joined:
    Jun 13, 2012
    Posts:
    322
  17. unimechanic

    unimechanic

    Joined:
    Jan 9, 2013
    Posts:
    155
    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.
     
    _Apokalipse_ likes this.
  18. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,596
    is gmcs still being used in the case of full .net 2.0 where as smcs is used on subset 2.0?
     
  19. CarlEmail

    CarlEmail

    Joined:
    Jun 30, 2006
    Posts:
    378
    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?
     
    BlueBirds likes this.
  20. CarlEmail

    CarlEmail

    Joined:
    Jun 30, 2006
    Posts:
    378
  21. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    108
    Just as a note for future readers: "Assets/boo.rsp" doesn't do anything, but "Assets/booc.rsp" does.
     
  22. weituotian

    weituotian

    Joined:
    Jun 24, 2015
    Posts:
    4