/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal
{
using UnityEngine;
using System;
/// Used to drive the lifecycle.
[ScriptOrder(NativeConstants.NRKERNALUPDATER_ORDER)]
public class NRKernalUpdater : MonoBehaviour
{
/// The instance.
private static NRKernalUpdater m_Instance;
/// Gets the instance.
/// The instance.
public static NRKernalUpdater Instance
{
get
{
if (m_Instance == null && !m_IsDestroyed)
{
m_Instance = CreateInstance();
}
return m_Instance;
}
}
[RuntimeInitializeOnLoadMethod]
static void Initialize()
{
if (m_Instance == null)
{
m_Instance = CreateInstance();
}
}
/// Creates the instance.
/// The new instance.
private static NRKernalUpdater CreateInstance()
{
GameObject updateObj = new GameObject("NRKernalUpdater");
GameObject.DontDestroyOnLoad(updateObj);
return updateObj.AddComponent();
}
/// Event queue for all listeners interested in OnPreUpdate events.
public static event Action OnPreUpdate;
/// Event queue for all listeners interested in OnUpdate events.
public static event Action OnUpdate;
/// Event queue for all listeners interested in OnPostUpdate events.
public static event Action OnPostUpdate;
#if DEBUG_PERFORMANCE
long lastFrame = 0;
#endif
/// Updates this object.
private void Update()
{
#if DEBUG_PERFORMANCE
long curFrame = System.DateTime.Now.Ticks;
long duration = curFrame - lastFrame;
#endif
OnPreUpdate?.Invoke();
OnUpdate?.Invoke();
OnPostUpdate?.Invoke();
#if DEBUG_PERFORMANCE
long curFrameEnd = System.DateTime.Now.Ticks;
long curFrameDur = curFrameEnd - curFrame;
NRDebugger.Info("[Performance] Main update: frameAll={0}Tick, frameUpdate={1}", duration, curFrameDur);
lastFrame = curFrame;
#endif
}
private static bool m_IsDestroyed = false;
private void OnDestroy()
{
m_Instance = null;
m_IsDestroyed = true;
}
}
}