PunStartup.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PunStartup.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Demos
  4. // </copyright>
  5. // <summary>
  6. // Used to setup the demo build settings and load the first demo scene (if imported into a new empty project).
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using UnityEditor.SceneManagement;
  14. using UnityEngine;
  15. using UnityEditor;
  16. using Photon.Pun;
  17. using Photon.Realtime;
  18. using ExitGames.Client.Photon;
  19. using UnityEngine.SceneManagement;
  20. namespace Photon.Pun.Demo.Hub
  21. {
  22. [InitializeOnLoad]
  23. public class PunStartup : MonoBehaviour
  24. {
  25. static PunStartup()
  26. {
  27. bool doneBefore = EditorPrefs.GetBool("PunDemosOpenedBefore");
  28. if (!doneBefore)
  29. {
  30. EditorApplication.update += OnUpdate;
  31. }
  32. }
  33. static void OnUpdate()
  34. {
  35. if (EditorApplication.isUpdating || Application.isPlaying)
  36. {
  37. return;
  38. }
  39. bool doneBefore = EditorPrefs.GetBool("PunDemosOpenedBefore");
  40. EditorPrefs.SetBool("PunDemosOpenedBefore", true);
  41. EditorApplication.update -= OnUpdate;
  42. if (doneBefore)
  43. {
  44. return;
  45. }
  46. if (string.IsNullOrEmpty(SceneManagerHelper.EditorActiveSceneName) && EditorBuildSettings.scenes.Length == 0)
  47. {
  48. LoadPunDemoHub();
  49. SetPunDemoBuildSettings();
  50. Debug.Log("No scene was open. Loaded PUN Demo Hub Scene and added demos to build settings. Ready to go! This auto-setup is now disabled in this Editor.");
  51. }
  52. }
  53. [MenuItem("Window/Photon Unity Networking/Configure Demos (build setup)", false, 5)]
  54. public static void SetupDemo()
  55. {
  56. SetPunDemoBuildSettings();
  57. }
  58. //[MenuItem("Window/Photon Unity Networking/PUN Demo Loader Reset")]
  59. //protected static void ResetDemoLoader()
  60. //{
  61. // EditorPrefs.DeleteKey("PunDemosOpenedBefore");
  62. //}
  63. public static void LoadPunDemoHub()
  64. {
  65. string scenePath = FindAssetPath("DemoHub-Scene t:scene");
  66. if (!string.IsNullOrEmpty(scenePath))
  67. {
  68. EditorSceneManager.OpenScene (scenePath);
  69. Selection.activeObject = AssetDatabase.LoadMainAssetAtPath (scenePath);
  70. }
  71. }
  72. /// <summary>Finds the asset path base on its name or search query: https://docs.unity3d.com/ScriptReference/AssetDatabase.FindAssets.html </summary>
  73. /// <returns>The asset path. String.Empty, if not found.</returns>
  74. /// <param name="asset">Asset filter for AssetDatabase.FindAssets.</param>
  75. public static string FindAssetPath(string asset)
  76. {
  77. string[] guids = AssetDatabase.FindAssets(asset, null);
  78. if (guids.Length < 1)
  79. {
  80. Debug.LogError("We have a problem finding the asset: " + asset);
  81. return string.Empty;
  82. } else
  83. {
  84. return AssetDatabase.GUIDToAssetPath(guids[0]);
  85. }
  86. }
  87. /// <summary>
  88. /// Finds scenes in "Assets/Photon Unity Networking/Demos/", excludes those in folder "PUNGuide_M2H" and applies remaining scenes to build settings. The one with "Hub" in it first.
  89. /// </summary>
  90. public static void SetPunDemoBuildSettings()
  91. {
  92. string _PunPath = string.Empty;
  93. string _thisPath = PhotonNetwork.FindAssetPath ("PunStartUp");
  94. _thisPath = Application.dataPath + _thisPath.Substring (6); // remove "Assets/"
  95. //_PunPath = PhotonEditorUtils.GetParent(_thisPath,"Photon");
  96. if (string.IsNullOrEmpty(_PunPath))
  97. {
  98. _PunPath = Application.dataPath+"/Photon";
  99. }
  100. // find path of pun guide
  101. string[] tempPaths = Directory.GetDirectories(_PunPath, "Demos*", SearchOption.AllDirectories);
  102. if (tempPaths == null)
  103. {
  104. return;
  105. }
  106. List<EditorBuildSettingsScene> sceneAr = new List<EditorBuildSettingsScene> ();
  107. // find scenes of guide
  108. foreach (string guidePath in tempPaths)
  109. {
  110. tempPaths = Directory.GetFiles (guidePath, "*.unity", SearchOption.AllDirectories);
  111. if (tempPaths == null || tempPaths.Length == 0)
  112. {
  113. return;
  114. }
  115. // add found guide scenes to build settings
  116. for (int i = 0; i < tempPaths.Length; i++)
  117. {
  118. //Debug.Log(tempPaths[i]);
  119. string path = tempPaths [i].Substring (Application.dataPath.Length - "Assets".Length);
  120. path = path.Replace ('\\', '/');
  121. //Debug.Log(path);
  122. if (path.Contains ("PUNGuide_M2H") || path.Contains("DemoLoadBalancing"))
  123. {
  124. continue;
  125. }
  126. // edited to avoid old scene to be included.
  127. if (path.Contains ("DemoHub-Scene"))
  128. {
  129. sceneAr.Insert (0, new EditorBuildSettingsScene (path, true));
  130. continue;
  131. }
  132. sceneAr.Add (new EditorBuildSettingsScene (path, true));
  133. }
  134. }
  135. EditorBuildSettings.scenes = sceneAr.ToArray();
  136. EditorSceneManager.OpenScene(sceneAr[0].path);
  137. }
  138. }
  139. }