PunSceneSettingsInspector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="PunSceneSettingsInspector.cs" company="Exit Games GmbH">
  3. // PhotonNetwork Framework for Unity - Copyright (C) 2019 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Custom inspector for the PunSceneSettings component.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // ----------------------------------------------------------------------------
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using UnityEditor;
  15. using UnityEngine;
  16. namespace Photon.Pun
  17. {
  18. [CustomEditor(typeof(PunSceneSettings))]
  19. internal class PunSceneSettingsInspector : Editor
  20. {
  21. private PunSceneSettings m_Target;
  22. private bool isOpen;
  23. private List<string> _duplicateScenesDefinition;
  24. private List<int> _duplicateViewIdDefinition;
  25. private SerializedProperty listProperty;
  26. private SerializedProperty _sceneSettings_i;
  27. private SerializedProperty sceneNameProperty;
  28. private SerializedProperty sceneAssetProperty;
  29. private SerializedProperty minViewIdProperty;
  30. private bool _firstTime;
  31. public override void OnInspectorGUI()
  32. {
  33. this.m_Target = (PunSceneSettings) this.target;
  34. // error checking
  35. _duplicateScenesDefinition = m_Target.MinViewIdPerScene.GroupBy(x => x.sceneName)
  36. .Where(g => g.Count() > 1)
  37. .Select(y => y.Key)
  38. .ToList();
  39. _duplicateViewIdDefinition = m_Target.MinViewIdPerScene.GroupBy(x => x.minViewId)
  40. .Where(g => g.Count() > 1)
  41. .Select(y => y.Key)
  42. .ToList();
  43. DrawSceneSettingsList();
  44. foreach (string dup in _duplicateScenesDefinition)
  45. {
  46. EditorGUILayout.LabelField("Found duplicates for scene",dup);
  47. }
  48. foreach (SceneSetting sceneSettings in m_Target.MinViewIdPerScene)
  49. {
  50. if (_duplicateViewIdDefinition.Contains(sceneSettings.minViewId))
  51. {
  52. GUILayout.Label("Found view Id duplicates '"+sceneSettings.minViewId+"' for scene: " +sceneSettings.sceneName);
  53. }
  54. if (sceneSettings.minViewId > PhotonNetwork.MAX_VIEW_IDS)
  55. {
  56. GUILayout.Label(sceneSettings.sceneName+" view Id can not exceed the max view Id "+PhotonNetwork.MAX_VIEW_IDS);
  57. }
  58. if (sceneSettings.minViewId < 1)
  59. {
  60. GUILayout.Label(sceneSettings.sceneName+" view Id can not be less than 1");
  61. }
  62. if (sceneSettings.sceneAsset == null && !string.IsNullOrEmpty(sceneSettings.sceneName))
  63. {
  64. GUILayout.Label("'"+sceneSettings.sceneName+"' scene is missing in the project");
  65. }
  66. }
  67. _firstTime = false;
  68. }
  69. private void OnEnable()
  70. {
  71. _firstTime = true;
  72. }
  73. private void DrawSceneSettingsList()
  74. {
  75. GUILayout.Space(5);
  76. // check for changes ( from undo for example)
  77. this.serializedObject.Update();
  78. listProperty = this.serializedObject.FindProperty("MinViewIdPerScene");
  79. if (listProperty == null)
  80. {
  81. return;
  82. }
  83. float containerElementHeight = 44;
  84. float containerHeight = listProperty.arraySize * containerElementHeight;
  85. isOpen = PhotonGUI.ContainerHeaderFoldout("Scene Settings (" + listProperty.arraySize + ")", this.serializedObject.FindProperty("SceneSettingsListFoldoutOpen").boolValue);
  86. this.serializedObject.FindProperty("SceneSettingsListFoldoutOpen").boolValue = isOpen;
  87. if (isOpen == false)
  88. {
  89. containerHeight = 0;
  90. }
  91. Rect containerRect = PhotonGUI.ContainerBody(containerHeight);
  92. if (isOpen == true)
  93. {
  94. for (int i = 0; i < listProperty.arraySize; ++i)
  95. {
  96. Rect elementRect = new Rect(containerRect.xMin, containerRect.yMin + containerElementHeight * i,
  97. containerRect.width, containerElementHeight);
  98. {
  99. Rect texturePosition = new Rect(elementRect.xMin + 6,
  100. elementRect.yMin + elementRect.height / 2f - 1, 9, 5);
  101. ReorderableListResources.DrawTexture(texturePosition, ReorderableListResources.texGrabHandle);
  102. Rect propertyPosition = new Rect(elementRect.xMin + 20, elementRect.yMin + 3,
  103. elementRect.width - 45, 16);
  104. _sceneSettings_i = listProperty.GetArrayElementAtIndex(i);
  105. sceneNameProperty = _sceneSettings_i.FindPropertyRelative("sceneName");
  106. sceneAssetProperty = _sceneSettings_i.FindPropertyRelative("sceneAsset");
  107. minViewIdProperty = _sceneSettings_i.FindPropertyRelative("minViewId");
  108. string _sceneName = sceneNameProperty.stringValue;
  109. SceneAsset _sceneAsset = m_Target.MinViewIdPerScene[i].sceneAsset;
  110. // check if we need to find the scene asset based on the scene name. This is for backward compatibility or when the scene asset was deleted
  111. if (_firstTime)
  112. {
  113. if (_sceneAsset == null && !string.IsNullOrEmpty(_sceneName))
  114. {
  115. string[] guids = AssetDatabase.FindAssets(_sceneName + " t:SceneAsset");
  116. foreach (string guid in guids)
  117. {
  118. string path = AssetDatabase.GUIDToAssetPath(guid);
  119. if (Path.GetFileNameWithoutExtension(path) == _sceneName)
  120. {
  121. sceneAssetProperty.objectReferenceValue =
  122. AssetDatabase.LoadAssetAtPath<SceneAsset>(
  123. AssetDatabase.GUIDToAssetPath(guid));
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. bool _missingSceneAsset = _sceneAsset == null && !string.IsNullOrEmpty(_sceneName);
  130. // if we don't have a scene asset for the serialized scene named, we show an error.
  131. if (_missingSceneAsset ||
  132. (sceneNameProperty!=null && _duplicateScenesDefinition!=null && _duplicateScenesDefinition.Contains(sceneNameProperty.stringValue))
  133. )
  134. {
  135. GUI.color = Color.red;
  136. }
  137. EditorGUI.BeginChangeCheck();
  138. string _label = _missingSceneAsset
  139. ? "Scene Asset: Missing '" + _sceneName + "'"
  140. : "Scene Asset";
  141. EditorGUI.PropertyField(propertyPosition,sceneAssetProperty, new GUIContent(_label));
  142. if (EditorGUI.EndChangeCheck())
  143. {
  144. _sceneAsset = sceneAssetProperty.objectReferenceValue as SceneAsset;
  145. if (_sceneAsset == null && !string.IsNullOrEmpty(sceneNameProperty.stringValue))
  146. {
  147. sceneNameProperty.stringValue = null;
  148. }
  149. else if (sceneNameProperty.stringValue != _sceneAsset.name)
  150. {
  151. sceneNameProperty.stringValue = _sceneAsset.name;
  152. }
  153. }
  154. // EditorGUI.PropertyField(propertyPosition, sceneNameProperty,
  155. // new GUIContent("Scene Name"));
  156. GUI.color = Color.white;
  157. if ( minViewIdProperty.intValue<1 || minViewIdProperty.intValue> PhotonNetwork.MAX_VIEW_IDS)
  158. {
  159. GUI.color = Color.red;
  160. }
  161. Rect secondPropertyPosition = new Rect(elementRect.xMin + 20, elementRect.yMin + containerElementHeight/2,
  162. elementRect.width - 45, 16);
  163. EditorGUI.PropertyField(secondPropertyPosition, _sceneSettings_i.FindPropertyRelative("minViewId"),
  164. new GUIContent("Minimum View ID"));
  165. GUI.color = Color.white;
  166. //Debug.Log( listProperty.GetArrayElementAtIndex( i ).objectReferenceValue.GetType() );
  167. //Rect statsPosition = new Rect( propertyPosition.xMax + 7, propertyPosition.yMin, statsIcon.width, statsIcon.height );
  168. //ReorderableListResources.DrawTexture( statsPosition, statsIcon );
  169. Rect removeButtonRect = new Rect(
  170. elementRect.xMax - PhotonGUI.DefaultRemoveButtonStyle.fixedWidth,
  171. elementRect.yMin + 2,
  172. PhotonGUI.DefaultRemoveButtonStyle.fixedWidth,
  173. PhotonGUI.DefaultRemoveButtonStyle.fixedHeight);
  174. if (GUI.Button(removeButtonRect, new GUIContent(ReorderableListResources.texRemoveButton),
  175. PhotonGUI.DefaultRemoveButtonStyle))
  176. {
  177. listProperty.DeleteArrayElementAtIndex(i);
  178. Undo.RecordObject(this.m_Target, "Removed SceneSettings Entry");
  179. }
  180. if (i < listProperty.arraySize - 1)
  181. {
  182. texturePosition = new Rect(elementRect.xMin + 2, elementRect.yMax, elementRect.width - 4,
  183. 1);
  184. PhotonGUI.DrawSplitter(texturePosition);
  185. }
  186. }
  187. }
  188. }
  189. if (PhotonGUI.AddButton())
  190. {
  191. this.listProperty.InsertArrayElementAtIndex(Mathf.Max(0, listProperty.arraySize - 1));
  192. _sceneSettings_i = this.listProperty.GetArrayElementAtIndex(listProperty.arraySize - 1);
  193. sceneNameProperty = _sceneSettings_i.FindPropertyRelative("sceneName");
  194. sceneAssetProperty = _sceneSettings_i.FindPropertyRelative("sceneAsset");
  195. minViewIdProperty = _sceneSettings_i.FindPropertyRelative("minViewId");
  196. sceneAssetProperty.objectReferenceValue = null;
  197. sceneNameProperty.stringValue = "";
  198. minViewIdProperty.intValue = 1;
  199. Undo.RecordObject(this.m_Target, "Added SceneSettings Entry");
  200. }
  201. this.serializedObject.ApplyModifiedProperties();
  202. }
  203. }
  204. }