// %BANNER_BEGIN%
// ---------------------------------------------------------------------
// %COPYRIGHT_BEGIN%
// Copyright (c) (2018-2022) Magic Leap, Inc. All Rights Reserved.
// Use of this file is governed by the Software License Agreement, located here: https://www.magicleap.com/software-license-agreement-ml2
// Terms and conditions applicable to third-party materials accompanying this distribution may also be found in the top-level NOTICE file appearing herein.
// %COPYRIGHT_END%
// ---------------------------------------------------------------------
// %BANNER_END%
namespace UnityEngine.XR.MagicLeap
{
using System;
public abstract class MLAPIBase
{
///
/// The native handle ID for this API instance. Will be invalid until the API is started.
///
protected ulong Handle = Native.MagicLeapNativeBindings.InvalidHandle;
///
/// Lock used to keep api calls synchronous.
///
protected readonly object APILock = new object();
private bool isStarted = false;
public MLAPIBase()
{
StartInternal();
}
~MLAPIBase()
{
StopInternal();
}
///
/// Update function that will run once per Unity frame.
///
protected virtual void Update() { }
///
/// Callback sent to all MagicLeap APIs on application pause.
///
/// True if the application is paused, else False.
protected virtual void OnApplicationPause(bool pauseStatus) { }
///
/// Callback sent to all MagicLeap APIs on application focus event.
///
/// True if the application has focus, else False.
protected virtual void OnApplicationFocus(bool hasFocus) { }
private void StartInternal()
{
MLDevice.RegisterUpdate(Update);
MLDevice.RegisterApplicationPause(OnApplicationPause);
MLDevice.RegisterApplicationFocus(OnApplicationFocus);
MLDevice.RegisterDestroy(StopInternal);
isStarted = true;
}
private void StopInternal()
{
if (isStarted)
{
MLDevice.UnregisterUpdate(Update);
MLDevice.UnregisterApplicationPause(OnApplicationPause);
MLDevice.UnregisterApplicationFocus(OnApplicationFocus);
MLDevice.UnregisterDestroy(StopInternal);
isStarted = false;
}
}
}
}