gpt4 book ai didi

c# - Interop 导致 Unity 崩溃

转载 作者:太空狗 更新时间:2023-10-29 17:24:28 27 4
gpt4 key购买 nike

是否有可能将 C/C++ 函数回调到 Unity 脚本中,前提是您可以从脚本创建一个新线程?我试过了,但是脚本一执行,Unity 就崩溃了.

我用谷歌搜索并找到了 this thread这说

Unity is not callback nor threadsafe, any access to classes that extend UnityEngine.Object is only allowed from within the same thread as unity scripts are running in, not asyncronous from other threads nor from asyncrnous callbacks from COM / OS async operations

If thats the case for you there are two possible ways:

(1) Write a wrapper that gets these callbacks and queues the stuff and then expose a function that allows unity to request the next event / dataobject or whatever in a blocking, unthreaded form (2) Make the callbacks call into a static function on something extending from System.Object and write the same kind of logic as above to request the information on classes extending UnityEngine.Object

但我认为如果我创建一个线程并回调到该线程,就可以了,对吧?我这样想是因为我读过类似 this one 的线程介绍了如何使 C 函数回调 C# 函数。所以我推断,如果我创建一个新线程,它就不再是 Unity,它将只是单声道和 C#。

这是我导致 Unity 崩溃的代码:

C++代码:

#include <iostream>
// #include "stdafx.h"

typedef int (__stdcall * Callback)(const char* text);

Callback Handler = 0;

extern "C" __declspec(dllexport)
void __stdcall SetCallback(Callback handler) {
Handler = handler;
}

extern "C" __declspec(dllexport)
void __stdcall TestCallback() {
int retval = Handler("hello world");
}

C#代码:

using UnityEngine;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;


class UnManagedInterop : MonoBehaviour {
private delegate int Callback(string text);
private Callback mInstance; // Ensure it doesn't get garbage collected


public void Test() {
mInstance = new Callback(Handler);
SetCallback(mInstance);
TestCallback();
}

private int Handler(string text) {
// Do something...
print(text);
return 42;
}

[DllImport("test0")]
private static extern void SetCallback(Callback fn);
[DllImport("test0")]
private static extern void TestCallback();

void Start()
{
Thread oThread = new Thread(new ThreadStart(Test));

// Start the thread
oThread.Start();


}
}

最佳答案

答案是!

我在 2012 年 8 月 8 日再次测试,使用 Unity 3.5.2f2,Pro 许可证。感谢@hatboyzero 的评论,我找到了 this example .

尽管我问题中的代码不起作用,但以下代码有效:

// C#
using System.Runtime.InteropServices;

class Demo {
delegate int MyCallback1 (int a, int b);

[DllImport ("MyRuntime")]
extern static void RegisterCallback (MyCallback1 callback1);

static int Add (int a, int b) { return a + b; }
static int Sub (int a, int b) { return a - b; }

void Init ()
{
// This one registers the method "Add" to be invoked back by C code
RegisterCallback (Add);
}
}


// C
typedef int (*callback_t) (int a, int b);
static callback_t my_callback;

void RegisterCallback (callback_t cb)
{
my_callback = cb;
}

int InvokeManagedCode (int a, int b)
{
if (my_callback == NULL){
printf ("Managed code has not initialized this library yet");
abort ();
}
return (*my_callback) (a, b);
}

我不必像教程建议的那样嵌入 MonoRuntime。上面两段代码解决了我的问题。

关于c# - Interop 导致 Unity 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10524365/

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