PunCockpitEmbed.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PunCockpitEmbed.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Networking Demos
  4. // </copyright>
  5. // <summary>
  6. // Use this in scenes you want to leave Control for connection and pun related commands to Cockpit.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10. using System.Collections;
  11. using UnityEngine;
  12. using UnityEngine.SceneManagement;
  13. using Photon.Pun.UtilityScripts;
  14. namespace Photon.Pun.Demo.Cockpit
  15. {
  16. /// <summary>
  17. /// Use this in scenes you want to leave Control for connection and pun related commands to Cockpit.
  18. /// It requires ConnectAndJoinRandom, which it will control for connecting should the Cockpit scene not be present or succesfully loaded.
  19. /// </summary>
  20. public class PunCockpitEmbed : MonoBehaviourPunCallbacks
  21. {
  22. string PunCockpit_scene = "PunCockpit-Scene";
  23. public bool EmbeddCockpit = true;
  24. public string CockpitGameTitle = "";
  25. public GameObject LoadingIndicator;
  26. public ConnectAndJoinRandom AutoConnect;
  27. void Awake()
  28. {
  29. if (LoadingIndicator != null)
  30. {
  31. LoadingIndicator.SetActive(false);
  32. }
  33. }
  34. // Use this for initialization
  35. IEnumerator Start()
  36. {
  37. PunCockpit.Embedded = EmbeddCockpit;
  38. PunCockpit.EmbeddedGameTitle = CockpitGameTitle;
  39. //Debug.Log (SceneManager.GetSceneByName (PunCockpit_scene).IsValid());
  40. SceneManager.LoadScene(PunCockpit_scene, LoadSceneMode.Additive);
  41. yield return new WaitForSeconds(1f);
  42. if (SceneManager.sceneCount == 1)
  43. {
  44. AutoConnect.ConnectNow();
  45. if (LoadingIndicator != null)
  46. {
  47. LoadingIndicator.SetActive(true);
  48. }
  49. }
  50. else
  51. {
  52. Destroy(AutoConnect);
  53. }
  54. yield return 0;
  55. }
  56. #region MonoBehaviourPunCallbacks implementation
  57. public override void OnJoinedRoom()
  58. {
  59. //Debug.Log("OnJoinedRoom", this);
  60. if (LoadingIndicator != null)
  61. {
  62. LoadingIndicator.SetActive(false);
  63. }
  64. if (PunCockpit.Instance != null)
  65. {
  66. //Debug.Log("switch to minimal panel", this);
  67. PunCockpit.Instance.SwitchtoMinimalPanel();
  68. }
  69. }
  70. #endregion
  71. }
  72. }