PunSceneSettings.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="PunSceneSettings.cs" company="Exit Games GmbH">
  3. // PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Optional lowest-viewID setting per-scene. So PhotonViews don't get the same ID.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // ----------------------------------------------------------------------------
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using UnityEditor;
  14. using UnityEngine;
  15. namespace Photon.Pun
  16. {
  17. [Serializable]
  18. public class SceneSetting
  19. {
  20. public SceneAsset sceneAsset;
  21. public string sceneName;
  22. public int minViewId;
  23. }
  24. [HelpURL("https://doc.photonengine.com/en-us/pun/current/getting-started/feature-overview#scene_photonviews_in_multiple_scenes")]
  25. public class PunSceneSettings : ScriptableObject
  26. {
  27. #if UNITY_EDITOR
  28. // Suppressing compiler warning "this variable is never used". Only used in the CustomEditor, only in Editor
  29. #pragma warning disable 0414
  30. [SerializeField]
  31. bool SceneSettingsListFoldoutOpen = true;
  32. #pragma warning restore 0414
  33. #endif
  34. [SerializeField]
  35. public List<SceneSetting> MinViewIdPerScene = new List<SceneSetting>();
  36. private const string SceneSettingsFileName = "PunSceneSettingsFile.asset";
  37. // we use the path to PunSceneSettings.cs as path to create a scene settings file
  38. private static string punSceneSettingsCsPath;
  39. public static string PunSceneSettingsCsPath
  40. {
  41. get
  42. {
  43. if (!string.IsNullOrEmpty(punSceneSettingsCsPath))
  44. {
  45. return punSceneSettingsCsPath;
  46. }
  47. // Unity 4.3.4 does not yet have AssetDatabase.FindAssets(). Would be easier.
  48. var result = Directory.GetFiles(Application.dataPath, "PunSceneSettings.cs", SearchOption.AllDirectories);
  49. if (result.Length >= 1)
  50. {
  51. punSceneSettingsCsPath = Path.GetDirectoryName(result[0]);
  52. punSceneSettingsCsPath = punSceneSettingsCsPath.Replace('\\', '/');
  53. punSceneSettingsCsPath = punSceneSettingsCsPath.Replace(Application.dataPath, "Assets");
  54. // AssetDatabase paths have to use '/' and are relative to the project's folder. Always.
  55. punSceneSettingsCsPath = punSceneSettingsCsPath + "/" + SceneSettingsFileName;
  56. }
  57. return punSceneSettingsCsPath;
  58. }
  59. }
  60. private static PunSceneSettings instanceField;
  61. public static PunSceneSettings Instance
  62. {
  63. get
  64. {
  65. if (instanceField != null)
  66. {
  67. return instanceField;
  68. }
  69. instanceField = (PunSceneSettings)AssetDatabase.LoadAssetAtPath(PunSceneSettingsCsPath, typeof(PunSceneSettings));
  70. if (instanceField == null)
  71. {
  72. instanceField = CreateInstance<PunSceneSettings>();
  73. #pragma warning disable 0168
  74. try
  75. {
  76. AssetDatabase.CreateAsset(instanceField, PunSceneSettingsCsPath);
  77. }
  78. catch (Exception e)
  79. {
  80. #if PHOTON_UNITY_NETWORKING
  81. Debug.LogError("-- WARNING: PROJECT CLEANUP NECESSARY -- If you delete pun from your project, make sure you also clean up the Scripting define symbols from any reference to PUN like 'PHOTON_UNITY_NETWORKING ");
  82. #endif
  83. }
  84. #pragma warning restore 0168
  85. }
  86. return instanceField;
  87. }
  88. }
  89. public static int MinViewIdForScene(string sceneName)
  90. {
  91. if (string.IsNullOrEmpty(sceneName))
  92. {
  93. return 1;
  94. }
  95. PunSceneSettings pss = Instance;
  96. if (pss == null)
  97. {
  98. Debug.LogError("pss cant be null");
  99. return 1;
  100. }
  101. foreach (SceneSetting setting in pss.MinViewIdPerScene)
  102. {
  103. if (setting.sceneName.Equals(sceneName))
  104. {
  105. return setting.minViewId;
  106. }
  107. }
  108. return 1;
  109. }
  110. public static void SanitizeSceneSettings()
  111. {
  112. if (Instance == null)
  113. {
  114. return;
  115. }
  116. #if UNITY_EDITOR
  117. foreach (SceneSetting sceneSetting in Instance.MinViewIdPerScene)
  118. {
  119. if (sceneSetting.sceneAsset == null && !string.IsNullOrEmpty(sceneSetting.sceneName))
  120. {
  121. string[] guids = AssetDatabase.FindAssets(sceneSetting.sceneName + " t:SceneAsset");
  122. foreach (string guid in guids)
  123. {
  124. string path = AssetDatabase.GUIDToAssetPath(guid);
  125. if (Path.GetFileNameWithoutExtension(path) == sceneSetting.sceneName)
  126. {
  127. sceneSetting.sceneAsset =
  128. AssetDatabase.LoadAssetAtPath<SceneAsset>(
  129. AssetDatabase.GUIDToAssetPath(guid));
  130. // Debug.Log("SceneSettings : ''"+sceneSetting.sceneName+"'' scene is missing: Issue corrected",Instance);
  131. break;
  132. }
  133. }
  134. //Debug.Log("SceneSettings : ''"+sceneSetting.sceneName+"'' scene is missing",Instance);
  135. continue;
  136. }
  137. if (sceneSetting.sceneAsset != null && sceneSetting.sceneName!= sceneSetting.sceneAsset.name )
  138. {
  139. // Debug.Log("SceneSettings : '"+sceneSetting.sceneName+"' mismatch with sceneAsset: '"+sceneSetting.sceneAsset.name+"' : Issue corrected",Instance);
  140. sceneSetting.sceneName = sceneSetting.sceneAsset.name;
  141. continue;
  142. }
  143. }
  144. #endif
  145. }
  146. }
  147. }