/// <summary> /// Converts Color32 the array to byte array by using Marshal.Copy. /// </summary> /// <param name="colors">Input Color32 array.</param> /// <param name="bytes">Output byte array.</param> public static void Color32ArrayToByteArray(Color32[] colors, out byte[] bytes) { int length = Marshal.SizeOf(typeof(Color32)) * colors.Length; bytes = new byte[length]; Marshal.Copy(CvUtil.GetIntPtr(colors), bytes, 0, length); }
/// <summary> /// Converts Byte array to Unity Color32 array. /// </summary> /// <param name="bytes">Input Byte array.</param> /// <param name="colors">Output Color32 array.</param> public static void ByteArrayToColor32Array(byte[] bytes, out Color32[] colors) { int size = bytes.Length; colors = new Color32[size / 4]; System.IntPtr ptr = CvUtil.GetIntPtr(bytes); for (int i = 0; i < size / 4; i++) { colors[i] = (Color32)Marshal.PtrToStructure(ptr, typeof(Color32)); ptr = new System.IntPtr(ptr.ToInt32() + 4); } }