[Windows Phone] System.AccessViolationException

Hi!
I created a wp8 plugin for unity3d for loading an image from gallery. In plugin I created method LoadImageFromGalery() and event OnImageLoaded. In unity3d script I add listener for OnImageLoaded event. So when I call LoadImageFromGalery() from unity the event will fire. But if I try to create Texture2d in event handler OnImageLoadedEventHandler code will throw an AccessViolationException.

void OnImageLoadedEventHandler(object sender, LoadImageEventArgs e){

// in e all data are correct

texture = new Texture2D(e.Width, e.Height); // throw an AccessViolationException
texture.LoadImage(e.ImageData);

}

Thanks for answers!

My guess is the access violating exception is coming from e.Width and e.Height not actually the creation of the texture. Since you mentioned a plug-in, are you generating an event from the “Visual Studio” side? That would be the UI Thread. In that case, you need to ensure you execute any Unity code on the Unity thread.

On the Visual Studio side this is:
UnityPlayer.UnityApp.BeginInvoke

See: http://blogs.msdn.com/b/uk_faculty_connection/archive/2013/09/11/getting-started-building-windows-phone-games-with-unity.aspx for details.

If that’s not the issue, double-check your logs on the device itself. Remember to install Windows Phone Power Tools which will allow you to access the logs.