【发布时间】:2017-01-01 02:33:31
【问题描述】:
我想使用 CLR 将 c++ code 中的 c# 用于 Unity。
该程序在 unity 之外正常工作,但在引擎内部却给我一个错误:
“cs0227: unsafe code requires the 'unsafe' command line option to be specified”
我真的很困惑,因为该项目在 Visual Studio 中成功构建(没有任何错误或警告)。我激活了“允许 不安全”按钮。
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();
}
}
}
// ...
}
【问题讨论】:
-
如果有什么不清楚的地方请告诉我,我会改正的。
-
由于 Unity 是一个黑盒,我们并不真正知道在转换代码时支持哪些 C# 功能。粗略的猜测是不安全的东西不是。您是否考虑过使用
Marshal类方法? -
你的意思是dll导入吗? ' [DllImport("C++ unitshi.dll")] ' static extern int add(int a, int b);'
-
不,我的意思是所有指针类型和指针算法,他们当然必须为网络播放器画一条线。但似乎@Programmer 的答案允许你这样做。