2016-08-24 8 views
3

CLR을 사용하여 단일성을 위해 C++에서 C++ 코드를 사용하고 싶습니다. 프로젝트가 Visual Studio에서 (오류 또는 경고없이) 성공적으로 빌드하기 때문에안전하지 않은 코드 사용 방법 Unity

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"

것은 난 정말 혼란 스러워요. "안전하지 않음"버튼이 활성화되었습니다.

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(); 
} 
+0

불명확 한 점이 있으면 알려주십시오. –

+1

Unity가 블랙 박스이기 때문에, 코드가 변환 될 때 어떤 C# 기능이 지원되는지 알지 못합니다. 거친 추측은 안전하지 않은 것들이 아닙니다. 'Marshal' 클래스 메소드 사용을 고려 했습니까? – Aybe

+0

dll 가져 오기를 의미합니까? '[DllImport ("C++ unitshi.dll")]' static extern int add (int a, int b); ' –

답변

9

Unity에 unsafe 코드를 명시 적으로 설정해야합니다. 다음 단계를 따르십시오.

. 첫 번째 단계, 변경 API 호환성 수준 to .NET 2.0 하위 집합.

2

enter image description here

. <Project Path>/Assets 디렉토리에 파일을 만들고 이름을 smcs.rsp으로 지정한 다음 -unsafe을 해당 파일에 넣습니다. 해당 파일을 저장하고 닫습니다.

enter image description here

닫기 및 Visual Studio와 단결을 다시 엽니 다.모두을 다시 시작해야합니다.

그것은 주목할 가치가 심지어이 일을하고 renamesmcs.rsp 파일 csc.rsp, 또는 gmcs.rsp하고, 다시 시작 모두 단결 및 Visual Studio하지만 문제가 여전히 후 작동 할 때까지 매회 다시 시작하십시오. 비록, smcs.rsp 처음으로해야합니다.

간단한 C#이 이후에 컴파일하는 안전하지 않은 코드.

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); 
     } 
    } 
} 

편집 : 최신 유니티 버전에 대한

은 파일 이름이 mcs.rsp해야한다. 다른 모든 것은 동일하게 유지됩니다.

+1

대단히 감사합니다! 이것은 지구상에서 가장 좋은 포럼입니다. –