I develop a plugin based on .NET 4.7 for unity and want to send images via events. This works so far. But when firing this events from another thread of my plugin library I get the error:
GetGraphicsFormat_Native_TextureFormat can only be called from the main thread.
How can I workaround this problem? My Code:
// .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;
}