- Home /
Does c# Marshaling work in the webplayer?
Hi, I'm trying to use Marshal.StructureToPtr to convert a structure before sending via a socket to another app. It works in the editor, but crashes on the Marshal.StructureToPtr call in the webplayer.
Heres some example code:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SENDPOS { public long SendId; public float x,y,z; };
SENDPOS PlayerPos;
byte[] arr;
int len;
IntPtr ptr;
//(in Start() )
len = Marshal.SizeOf(PlayerPos);
arr = new byte[len];
ptr = Marshal.AllocHGlobal(len);
//in Update()
Marshal.StructureToPtr( PlayerPos, ptr,false); //<-------- CRASH HERE
Marshal.Copy(ptr, arr, 0, len);
ClientSocket.Send(arr, len, SocketFlags.None);
Is marshaling supposed to work in the webplayer, or is this a Unity bug? Or am I doing something wrong in the above code?
Answer by AngryAnt · Dec 13, 2010 at 04:26 PM
Regardless of whether this should be enabled or not, crashing the browser is always a bug. Please report it using the bug reporter :)
Yep, had filed a bug report already. Seems like it's not supposed to be supported (as that would be unmanaged memory), but the current webplayer just crashes if you try to use it. The best workaround I've found so far is to use Buffer.BlockCopy and BitConverter.GetBytes to convert variables into a block of memory.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090826im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 3D Keep Crashing in every site! 1 Answer
Crash when closing our game in WebPlayer builds 2 Answers
Unity crashes Firefox 1 Answer
Assign www.assetBundle to new AssetBundle Freezes WebPlayer 0 Answers
Unity Web Player distorts objects 0 Answers