123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
-
- namespace NRKernal
- {
- using UnityEngine;
- using System;
-
- [ScriptOrder(NativeConstants.NRKERNALUPDATER_ORDER)]
- public class NRKernalUpdater : MonoBehaviour
- {
-
- private static NRKernalUpdater m_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();
- }
- }
-
-
- private static NRKernalUpdater CreateInstance()
- {
- GameObject updateObj = new GameObject("NRKernalUpdater");
- GameObject.DontDestroyOnLoad(updateObj);
- return updateObj.AddComponent<NRKernalUpdater>();
- }
-
- public static event Action OnPreUpdate;
-
- public static event Action OnUpdate;
-
- public static event Action OnPostUpdate;
- #if DEBUG_PERFORMANCE
- long lastFrame = 0;
- #endif
-
- 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;
- }
- }
- }
|