我开发了一个基于.NET 4.7的统一插件,并希望通过事件发送图像。到目前为止,该方法有效。 但是,从插件库的另一个线程触发此事件时,出现错误:
GetGraphicsFormat_Native_TextureFormat can only be called from the main thread.
如何解决此问题?我的代码:
// .NET PLUGIN
public delegate void StreamHandler(MemoryStream stream);
public event StreamHandler stream;
public void helloWorld()
{
thread = new Thread(dowork);
bm = new Bitmap("c://tests//3by2.jpg");
bm2 = new Bitmap("c://tests//ar-05.jpg");
memoryStream = new MemoryStream();
memoryStream2 = new MemoryStream();
bm.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
bm2.Save(memoryStream2, System.Drawing.Imaging.ImageFormat.Jpeg);
thread.Start();
}
private void dowork()
{
for (int i = 0; i < 10; i++)
{
this.stream(memoryStream);
System.Threading.Thread.Sleep(100);
this.stream(memoryStream2);
System.Threading.Thread.Sleep(100);
}
}
// Unity ---------------------------------------
Class1 class1 = new Class1();
class1.Log += new Class1.LogHandler(Logger);
class1.stream += new Class1.StreamHandler(streamhandle);
class1.helloWorld();
void streamhandle(MemoryStream ms)
{
Texture2D texture = new Texture2D(128, 128);
texture.LoadImage(ms.ToArray());
m_RawImage= RawImage.FindObjectOfType<RawImage>();
m_RawImage.texture = texture;
}