Dav*_*ili 6 c# c++ clr unsafe unity-game-engine
我想在c#中使用c ++代码来使用CLR进行统一.
该程序在统一之外正常工作,但在引擎内部它给我一个错误:
"cs0227:不安全的代码需要指定'unsafe'命令行选项"
我真的很困惑,因为该项目在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();
}
Run Code Online (Sandbox Code Playgroud)
Pro*_*mer 27
您必须unsafe在Unity中明确启用代码.您可以按照以下步骤操作:
1.第一步,将Api兼容级别更改 为.NET 2.0子集.
2.在<Project Path>/Assets目录中创建一个文件,并将其命名为smcs.rsp,然后将其-unsafe放入该文件中.保存并关闭该文件.
关闭并重新打开Visual Studio和Unity. 你必须重新启动它们.
值得一提的是这样做的,重新开始又团结和Visual Studio,但问题仍然存在,甚至后,重命名的smcs.rsp文件CSC.RSP,或gmcs.rsp每一次,直到你得到一个重新启动作品.虽然,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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
对于最新的Unity版本,文件名应为mcs.rsp.其他一切都是一样的.
小智 6
更新:在 Unity 2019 及以后版本中,我们没有 2.0 子集。使用2.0标准。
尽管 mcs.rsp 可以工作,但有一条警告指出 mcs 已过时,我们应该使用 csc.rsp 代替。
但它确实有效!
| 归档时间: |
|
| 查看次数: |
13243 次 |
| 最近记录: |