DemoScenesMenu.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using UnityEngine;
  10. using UnityEngine.SceneManagement;
  11. namespace NRKernal.NRExamples
  12. {
  13. /// <summary> Panel for editing the hidden debug. </summary>
  14. public class DemoScenesMenu : SingletonBehaviour<DemoScenesMenu>
  15. {
  16. /// <summary> The buttons root. </summary>
  17. public Transform m_ButtonsRoot;
  18. /// <summary> The buttons. </summary>
  19. private UserDefineButton[] Buttons;
  20. /// <summary> Starts this object. </summary>
  21. void Start()
  22. {
  23. Buttons = gameObject.GetComponentsInChildren<UserDefineButton>(true);
  24. m_ButtonsRoot.gameObject.SetActive(false);
  25. foreach (var item in Buttons)
  26. {
  27. item.OnClick += OnItemTriggerEvent;
  28. }
  29. GameObject.DontDestroyOnLoad(gameObject);
  30. }
  31. /// <summary> Executes the 'item trigger event' action. </summary>
  32. /// <param name="key"> The key.</param>
  33. private void OnItemTriggerEvent(string key)
  34. {
  35. if (key.Equals("InvisibleBtn"))
  36. {
  37. m_ButtonsRoot.gameObject.SetActive(!m_ButtonsRoot.gameObject.activeInHierarchy);
  38. }
  39. else if (CanSceneLoaded(key))
  40. {
  41. SceneManager.LoadScene(key);
  42. }
  43. }
  44. /// <summary> Determine if we can scene loaded. </summary>
  45. /// <param name="name"> The name.</param>
  46. /// <returns> True if we can scene loaded, false if not. </returns>
  47. private bool CanSceneLoaded(string name)
  48. {
  49. return (SceneUtility.GetBuildIndexByScenePath(name) != -1) &&
  50. !SceneManager.GetActiveScene().name.Equals(name);
  51. }
  52. }
  53. }