Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use unsafe context in Unity

I want to use c++ code in c# for Unity using CLR.

The program works properly outside of unity, but inside of engine it gives me an error:
"cs0227: unsafe code requires the 'unsafe' command line option to be specified"

I am really confused, because the project builds successfully in visual studio (without any errors or warnings). I have "allow unsafe" button activated.

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class newspawn_real : MonoBehaviour {
    void Start () {
        unsafe
        {
            fixed (int * p = &bam[0, 0, 0])
            {
                CppWrapper.CppWrapperClass controlCpp = new CppWrapper.CppWrapperClass();
                controlCpp.allocate_both();
                controlCpp.fill_both();
                controlCpp.fill_wrapper();
            }
        }
    }
    // ...
}
like image 327
Davit Goderdzishvili Avatar asked Aug 24 '16 19:08

Davit Goderdzishvili


People also ask

How can use unsafe context in C#?

C# supports an unsafe context where we can write code whose security is unverifiable by the CLR. For example, by default, C# does not support pointer arithmetic to ensure type safety and security. However, in an unsafe context, we can use pointers. To denote an unsafe context in C#, we use the unsafe keyword.

How do you compile with unsafe?

For compiling unsafe code, you have to specify the /unsafe command-line switch with command-line compiler. Open Project properties by double clicking the properties node in the Solution Explorer. Click on the “Build” tab. Select the option "Allow unsafe code".

What is the use unsafe keyword?

The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.

How do I allow unsafe code in Visual Studio?

Using Unsafe Code in Visual Studio IDEDouble click properties in the Solution Explorer to open project properties. Click the Build tab and select the option “Allow Unsafe Code”.


2 Answers

update: on unity 2019 and forward, we don't have the 2.0 subset. use 2.0 Standard.

and although mcs.rsp works, there is a warning noting that mcs is obsolete, and we should use csc.rsp instead.

but it actually works !

like image 160
Shachar Oz Avatar answered Sep 21 '22 20:09

Shachar Oz


You have to explicitly enable unsafe code in Unity. You can follow the steps below:

1. First step, Change Api Compatibility Level to .NET 2.0 Subset.

API Compatibility level

2. Create a file in your <Project Path>/Assets directory and name it smcs.rsp then put -unsafe inside that file. Save and close that file.

create smcs.rsp file

  • Close and reopen Visual Studio and Unity Editor.
  • You must restart both of them.

It's worth noting that even after doing this and restarting both Unity and Visual Studio, if the problem is still there,

  • Rename the smcs.rsp file to csc.rsp, gmcs.rsp or mcs.rsp

Restart Unity Editor and Visual Studio each time until you get one that works. For more details about the file name to use, refer to Platform Dependent Compilation documentation.

Simple C# unsafe code that compiles after this.

public class newspawn_real : MonoBehaviour
{
    unsafe static void SquarePtrParam(int* p)
    {
        *p *= *p;
    }

    void Start()
    {
        unsafe
        {
            int i = 5;
            // Unsafe method: uses address-of operator (&):
            SquarePtrParam(&i);
            Debug.Log(i);
        }
    }
}
like image 42
Programmer Avatar answered Sep 20 '22 20:09

Programmer