/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal.NREditor
{
using UnityEngine;
using UnityEditor;
using System;
/// Manager for nr editor scenes.
public class NREditorSceneManager
{
/// True if scene initialized.
private bool m_SceneInitialized;
/// True to unload unused assets.
private bool m_UnloadUnusedAssets;
/// True to apply appearance.
private bool m_ApplyAppearance;
/// True to apply properties.
private bool m_ApplyProperties;
/// The instance.
private static NREditorSceneManager m_Instance;
/// The update callback.
private EditorApplication.CallbackFunction m_UpdateCallback;
/// Gets the instance.
/// The instance.
public static NREditorSceneManager Instance
{
get
{
if (NREditorSceneManager.m_Instance == null)
{
Type typeFromHandle = typeof(NREditorSceneManager);
lock (typeFromHandle)
{
if (NREditorSceneManager.m_Instance == null)
{
NREditorSceneManager.m_Instance = new NREditorSceneManager();
}
}
}
return NREditorSceneManager.m_Instance;
}
}
/// Gets a value indicating whether the scene initialized.
/// True if scene initialized, false if not.
public bool SceneInitialized { get { return m_SceneInitialized; } }
///
/// Constructor that prevents a default instance of this class from being created.
private NREditorSceneManager()
{
//return;
m_UpdateCallback = new EditorApplication.CallbackFunction(EditorUpdate);
if (EditorApplication.update == null || !EditorApplication.update.Equals(m_UpdateCallback))
{
EditorApplication.update = (EditorApplication.CallbackFunction)System.Delegate.Combine(EditorApplication.update, m_UpdateCallback);
}
m_SceneInitialized = false;
}
/// Initializes the scene.
public void InitScene()
{
m_SceneInitialized = true;
}
/// Editor update.
public void EditorUpdate()
{
NRTrackableBehaviour[] trackables = GameObject.FindObjectsOfType();
if (m_ApplyAppearance)
{
UpdateTrackableAppearance(trackables);
m_ApplyAppearance = false;
}
if (m_ApplyProperties)
{
UpdateTrackableProperties(trackables);
m_ApplyProperties = false;
}
if (m_UnloadUnusedAssets)
{
Resources.UnloadUnusedAssets();
m_UnloadUnusedAssets = false;
}
}
/// Updates the trackable appearance described by trackables.
/// The trackables.
private void UpdateTrackableAppearance(NRTrackableBehaviour[] trackables)
{
if (!Application.isPlaying)
{
for (int i = 0; i < trackables.Length; i++)
{
NRTrackableBehaviour trackableBehaviour = trackables[i];
NRTrackableAccessor trackableAccessor = NRAccessorFactory.Create(trackableBehaviour);
if (trackableAccessor != null)
{
trackableAccessor.ApplyDataAppearance();
}
}
}
}
/// Updates the trackable properties described by trackables.
/// The trackables.
private void UpdateTrackableProperties(NRTrackableBehaviour[] trackables)
{
for (int i = 0; i < trackables.Length; i++)
{
NRTrackableBehaviour trackableBehaviour = trackables[i];
NRTrackableAccessor trackableAccessor = NRAccessorFactory.Create(trackableBehaviour);
if (trackableAccessor != null)
{
trackableAccessor.ApplyDataProperties();
}
}
}
/// Unload unused assets.
public void UnloadUnusedAssets()
{
m_UnloadUnusedAssets = true;
}
}
}