ToDemoHubButton.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="ToDemoHubButton.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Demos
  4. // </copyright>
  5. // <summary>
  6. // Present a button on all launched demos from hub to allow getting back to the demo hub.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10. using UnityEngine;
  11. using UnityEngine.SceneManagement;
  12. using UnityEngine.EventSystems;
  13. namespace Photon.Pun.Demo.Hub
  14. {
  15. /// <summary>
  16. /// Present a button on all launched demos from hub to allow getting back to the demo hub.
  17. /// </summary>
  18. public class ToDemoHubButton : MonoBehaviour
  19. {
  20. private static ToDemoHubButton instance;
  21. CanvasGroup _canvasGroup;
  22. public static ToDemoHubButton Instance
  23. {
  24. get
  25. {
  26. if (instance == null)
  27. {
  28. instance = FindObjectOfType(typeof (ToDemoHubButton)) as ToDemoHubButton;
  29. }
  30. return instance;
  31. }
  32. }
  33. public void Awake()
  34. {
  35. if (Instance != null && Instance != this)
  36. {
  37. Destroy(gameObject);
  38. }
  39. }
  40. // Use this for initialization
  41. public void Start()
  42. {
  43. DontDestroyOnLoad(gameObject);
  44. _canvasGroup = GetComponent<CanvasGroup>();
  45. }
  46. public void Update()
  47. {
  48. bool sceneZeroLoaded = false;
  49. #if UNITY_5 && !UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2 || UNITY_5_3_OR_NEWER
  50. sceneZeroLoaded = SceneManager.GetActiveScene().buildIndex == 0;
  51. #else
  52. sceneZeroLoaded = Application.loadedLevel == 0;
  53. #endif
  54. if (sceneZeroLoaded && _canvasGroup.alpha!= 0f)
  55. {
  56. _canvasGroup.alpha = 0f;
  57. _canvasGroup.interactable = false;
  58. }
  59. if (!sceneZeroLoaded && _canvasGroup.alpha!= 1f)
  60. {
  61. _canvasGroup.alpha = 1f;
  62. _canvasGroup.interactable = true;
  63. }
  64. }
  65. public void BackToHub()
  66. {
  67. PhotonNetwork.Disconnect();
  68. SceneManager.LoadScene(0);
  69. }
  70. }
  71. }