/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
using UnityEngine;
using UnityEngine.SceneManagement;
namespace NRKernal.NRExamples
{
/// The examples hub.
public class ExamplesHub : SingletonBehaviour
{
/// The scenes.
private string[] m_Scenes = new string[] {
"HelloMR",
"ImageTracking",
"Input-ControllerInfo",
"Input-Interaction",
"RGBCamera",
"RGBCamera-Capture",
"RGBCamera-Record"
};
/// The current index.
private int m_CurrentIndex = 0;
/// Gets or sets the current index.
/// The current index.
public int CurrentIndex
{
get
{
return m_CurrentIndex;
}
private set
{
m_CurrentIndex = value;
if (m_CurrentIndex < 0 || m_CurrentIndex >= m_Scenes.Length)
{
m_CurrentIndex = 0;
}
}
}
/// True if is lock, false if not.
private bool m_IsLock = false;
/// Updates this object.
private void Update()
{
#if UNITY_EDITOR
if (Input.GetKeyDown(KeyCode.RightArrow))
{
LoadNextScene();
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
LoadLastScene();
}
#endif
if (NRInput.GetTouch().x > 0.8f)
{
LoadNextScene();
}
if (NRInput.GetTouch().x < -0.8f)
{
LoadLastScene();
}
}
/// Loads next scene.
public void LoadNextScene()
{
if (m_IsLock)
{
return;
}
m_IsLock = true;
CurrentIndex++;
if (CanSceneLoaded(m_Scenes[CurrentIndex]))
{
SceneManager.LoadScene(m_Scenes[CurrentIndex]);
}
Invoke("Unlock", 1f);
}
/// Loads last scene.
public void LoadLastScene()
{
if (m_IsLock)
{
return;
}
m_IsLock = true;
CurrentIndex--;
if (CanSceneLoaded(m_Scenes[CurrentIndex]))
{
SceneManager.LoadScene(m_Scenes[CurrentIndex]);
}
Invoke("Unlock", 1f);
}
/// Unlocks this object.
private void Unlock()
{
m_IsLock = false;
}
/// Determine if we can scene loaded.
/// The name.
/// True if we can scene loaded, false if not.
private bool CanSceneLoaded(string name)
{
return (SceneUtility.GetBuildIndexByScenePath(name) != -1) &&
!SceneManager.GetActiveScene().name.Equals(name);
}
}
}