/*
* Copyright (C) 2021 because-why-not.com Limited
*
* Please refer to the license.txt for license information
*/
using System;
using System.Runtime.InteropServices;
using UnityEngine;
namespace Byn.Awrtc.Unity
{
///
/// Unity helper functions that are used in multiple parts of the asset
///
public static class UnityHelper
{
///
/// WARNING: This function is inherently unsafe and can cause crashes easily.
/// It pins an Color32 array returned by unity and gives the IntPtr to the
/// raw memory to a callback. It makes sure the memory is released.
/// Avoid this method whenever possible.
///
/// If you end up here investigating a crash the reason is likely here or in the
/// callback being called.
///
/// Array of Color32 structs
/// Callback receiving the IntPtr to the raw memory and its byte length
///
public static void PtrFromColor32(Color32[] arg, Action callback)
{
GCHandle handle = GCHandle.Alloc(arg, GCHandleType.Pinned);
try
{
uint structSize = (uint)Marshal.SizeOf(typeof(Color32));
uint size = ((uint)arg.Length) * structSize;
callback(handle.AddrOfPinnedObject(), size);
}
finally
{
handle.Free();
}
}
}
}