gpt4 book ai didi

c# - C++ DLL 和 C# Unity 之间的集成

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:43:43 26 4
gpt4 key购买 nike

我在使用 C++ Dll 与 Unity 集成时遇到问题。我在网上找到了一些代码(文章末尾的链接),但它不起作用。

这是我的C++ DLL代码(我想给unity发送一个有一些点的结构):

struct Pontos {
int i;
float f;
};
Pontos **pontos;
DllExport bool SetPoints(Pontos *** a, int *i)
{

pontos = new Pontos*[4];
for (int j = 0; j < 4; j++) {
pontos[j] = new Pontos; // Actually create each object.
pontos[j]->i = j;
pontos[j]->f = (float)j;
}
*a = pontos;
*i = 4;
return true;
}

从统一代码中,我得到以下错误:

No MonoBehaviour scripts in the file, or their names do not match the file name

我不知道这是什么意思。在这里,我尝试获取这些点并将它们保存在 c# 中:

[DllImport("dll")]
private static extern bool SetPoints(out IntPtr ptrResultVerts, out int resultVertLength);
public struct Pontos
{
int i;
float f;
};
void Start()
{
IntPtr ptrNativeData = IntPtr.Zero;
int itemsLength = 0;

bool success = SetPoints(out ptrNativeData, out itemsLength);
if (!success)
{
return;
}

Pontos[] SArray = new Pontos[itemsLength]; // Where the final data will be stored.
IntPtr[] SPointers = new IntPtr[itemsLength];

Debug.Log("Length: " + itemsLength); // Works!
Marshal.Copy(ptrNativeData, SPointers, 0, itemsLength); // Seems not to work.

for (int i = 0; i < itemsLength; i++)
{
Debug.Log("Pointer: " + SPointers[i]);
SArray[i] = (Pontos)Marshal.PtrToStructure(SPointers[i], typeof(Pontos));
}

我从 Can't marshal array of stucts from C++ to C# in Unity 得到了这段代码.

最佳答案

您必须导入指定调用约定的dll

[DllImport("MyDLL.dll", CallingConvention = CallingConvention.Cdecl)]

我认为没有错误

No MonoBehaviour scripts in the file, or their names do not match the file name

与您的代码有任何关系。检查您的类(class)名称和脚本名称。它们对应吗?

最后,我不确定方法签名中的 out 修饰符,因为我从不需要在 C++ 端处理三重指针。如果您的目标是填充 Pontos 结构数组,那么您不需要它。

另见 Pass structure (or class) from C++ dll to C# (Unity 3D)

关于c# - C++ DLL 和 C# Unity 之间的集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43743481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com