Search Unity

How do I enable unsafe code?

Discussion in 'Editor & General Support' started by homer_3, Nov 14, 2013.

  1. homer_3

    homer_3

    Joined:
    Jun 19, 2011
    Posts:
    69
    I've tried going to Assembly-CSharp->options->Build->General and checking the allow unsafe code checkbox. I tried Assembly-CSharp->options->Compiler and in the additional options text box, I tried adding both -unsafe and /unsafe. But I still get the error stating I need to allow unsafe code when I try building my app. Is there some other way I can enable unsafe code?
     
  2. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,960
    You can, but it will only make it run in the editor, still wont work when making a build, as far as i know there are no way of making unsafe code run in a build. : /
     
  3. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,371
    Make a text file named smcs.rsp (for .Net 2.0 Subset) or gmcs.rsp (for .Net 2.0) with a single line inside:
    Code (csharp):
    1. -unsafe
    and save in somewhere in your Assets folder. That's it.
     
  4. Zoro321

    Zoro321

    Joined:
    Nov 15, 2013
    Posts:
    2
    Hello,

    I am having the same problem.

    I tried placing those files named ``smcs.rsp`and gmcs.rsp` (containing the line `-unsafe`) in the Assets folder but I keep getting the compilation errors about the ``unsafe`thing.

    I am using Unity 4.2.2f1 (Pro version activated for a 30 days trial) and Visual Studio Express 2010 to compile my C++ dll library.

    My DLL library compiles fine and is being placed into my unity project folder/Plugins.

    I`ve written a small unity script that I have attached to a Cube object.

    Here is my c|<# code:

    using UnityEngine;
    using System.Collections;
    using System.Runtime.InteropServices;



    public class Joystick : MonoBehaviour {



    public struct SJoyXY
    {
    float x, y;
    // bool bInitialised;
    };

    public unsafe SJoyXY pos;

    [DllImport ("JoystickDLL")]
    private static extern SJoyXY* GetJoystickPosition();



    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    pos = GetJoystickPosition();
    Debug.Log( "Joystick position is : " + SJoyXY.x );

    }
    }


    I get the < following errors when compiling:

    c:\Users\Public\Documents\Unity Projects\JoystickDLL\Assets\Standard Assets\Scripts\Joystick.cs(23,23): Error CS0227: Unsafe code may only appear if compiling with /unsafe (CS0227) (Assembly-CSharp-firstpass)

    c:\Users\Public\Documents\Unity Projects\JoystickDLL\Assets\Standard Assets\Scripts\Joystick.cs(25,25): Error CS0214: Pointers and fixed size buffers may only be used in an unsafe context (CS0214) (Assembly-CSharp-firstpass)





    I`ve read alot of posts on the net, but non works with my system.

    I guess theren is something I do wrong but I can`t figure it out and I already spent too much time on it.
     
  5. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,371
    Compiling where? Both Visual Studio and MonoDevelop still need checked "Allow unsafe code" in project settings to successfully compile unsafe code. But since Unity compiles everything itself, it needs only smcs.rsp or gmcs.rsp.
     
  6. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,371
    Code (csharp):
    1.  
    2. public unsafe SJoyXY pos; // There's nothing unsafe here, you may remove this keyword.
    3.  
    4. [DllImport ("JoystickDLL")]
    5. private static extern SJoyXY* GetJoystickPosition();
    6.  
    7. ...
    8.  
    9. private void Update()
    10. {
    11.     pos = GetJoystickPosition();
    12.     Debug.Log("Joystick position is : " + SJoyXY.x); // Accessing a static field that doesn't exist
    13. }
    Does GetJoystickPosition really returns a pointer to SJoyXY? Inside Update it looks like it returns the whole structure. If it does return the whole structure, try this:

    Code (csharp):
    1. using System.Runtime.InteropServices;
    2. using UnityEngine;
    3.  
    4. public class Joystick : MonoBehaviour
    5. {
    6.     public struct SJoyXY
    7.     {
    8.         public float x;
    9.         public float y;
    10.     };
    11.  
    12.     public SJoyXY pos;
    13.  
    14.     [DllImport("JoystickDLL")]
    15.     private static extern SJoyXY GetJoystickPosition();
    16.  
    17.     private void Update()
    18.     {
    19.         pos = GetJoystickPosition();
    20.         Debug.Log("Joystick position is : " + pos.x);
    21.     }
    22. }
     
  7. Zoro321

    Zoro321

    Joined:
    Nov 15, 2013
    Posts:
    2
    Thank you very much Alexzzz; I got it !

    My problem was that I had created smcs.rsp with notepad and it had named it "smcs.rsp.txt".

    I also was missing the unsafe checkbox, that was searching in `Solution properties`instead of ``Project propertie" if "first pass c|...".

    For the poin<ters, you are definitely right. In my depair I had been changing my code here and there; my C++ function returns a pointer and I should use pointers everywhere.

    I`m soo happy !!!

    Thanks again !

    It seems Unity will get a new user :)
     
  8. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,061
    I'm failing to get this to work in Pro. I've added both .rsp files (also tried just one), have checked "allow unsafe code" in all three of the files in the solution explorer. No luck though. What am I missing?

     
  9. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,061
    Nevermind, it started working as soon as I commented out the unsafe code, saved it, then played and stopped it in the editor which triggered VS2012 to reload the solution. I then uncommented the unsafe code, saved it, and it worked.
     
    Last edited: Oct 23, 2014
  10. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    3,516
    You can run unsafe code from within a .NET DLL in the editor or builds without changing any configuration settings.
     
  11. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,061
    Good to know. Thanks.