HUDManager.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #if UNITY_5_3_OR_NEWER
  2. #define SCENE_MANAGER
  3. using UnityEngine.SceneManagement;
  4. #endif
  5. using System;
  6. using System.Collections;
  7. using UnityEngine;
  8. namespace ZenFulcrum.EmbeddedBrowser {
  9. /** Handles mouse hiding/locking and pause menu for the demo. */
  10. public class HUDManager : MonoBehaviour {
  11. public static HUDManager Instance { get; private set; }
  12. private bool haveMouse = false;
  13. public PointerUIGUI hud;
  14. public Browser HUDBrowser { get; private set; }
  15. public void Awake() {
  16. Instance = this;
  17. }
  18. public void Start() {
  19. HUDBrowser = hud.GetComponent<Browser>();
  20. HUDBrowser.RegisterFunction("unpause", args => Unpause());
  21. HUDBrowser.RegisterFunction("browserMode", args => LoadBrowseLevel(true));
  22. HUDBrowser.RegisterFunction("quit", args => Application.Quit());
  23. Unpause();
  24. #if UNITY_STANDALONE_LINUX
  25. StartCoroutine(Rehide());
  26. #endif
  27. //Update coin count on hud when user gets one
  28. PlayerInventory.Instance.coinCollected += count => HUDBrowser.CallFunction("setCoinCount", count);
  29. }
  30. private IEnumerator Rehide() {
  31. //Unity has bugs. Here's another workaround for another Unity bug.
  32. #if UNITY_5_5_OR_NEWER
  33. while (!UnityEngine.Rendering.SplashScreen.isFinished) yield return null;
  34. #else
  35. while (Application.isShowingSplashScreen) yield return null;
  36. #endif
  37. Cursor.visible = false;
  38. yield return new WaitForSeconds(.2f);
  39. Cursor.visible = true;
  40. yield return new WaitForSeconds(.2f);
  41. Cursor.visible = false;
  42. }
  43. public void Unpause() {
  44. Cursor.visible = false;
  45. Cursor.lockState = CursorLockMode.Locked;
  46. EnableUserControls(true);
  47. Time.timeScale = 1;
  48. haveMouse = true;
  49. HUDBrowser.CallFunction("setPaused", false);
  50. }
  51. public void Pause() {
  52. Cursor.visible = true;
  53. Cursor.lockState = CursorLockMode.None;
  54. haveMouse = false;
  55. Time.timeScale = 0;
  56. EnableUserControls(false);
  57. HUDBrowser.CallFunction("setPaused", true);
  58. }
  59. public void Update() {
  60. if (Input.GetKeyDown(KeyCode.Escape)) {
  61. if (haveMouse) Pause();
  62. else Unpause();
  63. }
  64. }
  65. public void Say(string html, float dwellTime) {
  66. HUDBrowser.CallFunction("say", html, dwellTime);
  67. }
  68. protected void EnableUserControls(bool enableIt) {
  69. //fixme: demo still uses the old input system
  70. #pragma warning disable 618
  71. FPSCursorRenderer.Instance.EnableInput = enableIt;
  72. #pragma warning restore 618
  73. var fpsInput = GetComponent<SimpleFPSController>();
  74. fpsInput.enabled = enableIt;
  75. hud.enableInput = !enableIt;
  76. }
  77. public void LoadBrowseLevel(bool force = false) {
  78. StartCoroutine(LoadLevel(force));
  79. }
  80. private IEnumerator LoadLevel(bool force = false) {
  81. if (!force) {
  82. yield return new WaitUntil(() => SayWordsOnTouch.ActiveSpeakers == 0);
  83. }
  84. Pause();
  85. #if SCENE_MANAGER
  86. SceneManager.LoadScene("SimpleBrowser");
  87. #else
  88. Application.LoadLevel("SimpleBrowser");
  89. #endif
  90. }
  91. }
  92. }