PhotonViewInspector.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="PhotonViewInspector.cs" company="Exit Games GmbH">
  3. // PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Custom inspector for the PhotonView component.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // ----------------------------------------------------------------------------
  10. using System;
  11. using UnityEditor;
  12. using UnityEngine;
  13. using Photon.Realtime;
  14. namespace Photon.Pun
  15. {
  16. [CustomEditor(typeof(PhotonView))]
  17. [CanEditMultipleObjects]
  18. internal class PhotonViewInspector : Editor
  19. {
  20. private PhotonView m_Target;
  21. private static GUIContent ownerTransferGuiContent = new GUIContent("Ownership Transfer", "Determines how ownership changes may be initiated.");
  22. private static GUIContent syncronizationGuiContent = new GUIContent("Synchronization", "Determines how sync updates are culled and sent.");
  23. private static GUIContent observableSearchGuiContent = new GUIContent("Observable Search", "When set to Auto, On Awake, Observables on this GameObject (and child GameObjects) will be found and populate the Observables List." +
  24. "\n\nNested PhotonViews (children with a PhotonView) and their children will not be included in the search.");
  25. public void OnEnable()
  26. {
  27. this.m_Target = (PhotonView)this.target;
  28. if (!Application.isPlaying)
  29. m_Target.FindObservables();
  30. }
  31. public override void OnInspectorGUI()
  32. {
  33. this.m_Target = (PhotonView)this.target;
  34. bool isProjectPrefab = PhotonEditorUtils.IsPrefab(this.m_Target.gameObject);
  35. bool multiSelected = Selection.gameObjects.Length > 1;
  36. if (this.m_Target.ObservedComponents == null)
  37. {
  38. this.m_Target.ObservedComponents = new System.Collections.Generic.List<Component>();
  39. }
  40. if (this.m_Target.ObservedComponents.Count == 0)
  41. {
  42. this.m_Target.ObservedComponents.Add(null);
  43. }
  44. GUILayout.Space(5);
  45. EditorGUILayout.BeginVertical((GUIStyle)"HelpBox");
  46. // View ID - Hide if we are multi-selected
  47. if (!multiSelected)
  48. {
  49. if (isProjectPrefab)
  50. {
  51. EditorGUILayout.LabelField("View ID", "<i>Set at runtime</i>", new GUIStyle("Label") { richText = true });
  52. }
  53. else if (EditorApplication.isPlaying)
  54. {
  55. EditorGUILayout.LabelField("View ID", this.m_Target.ViewID.ToString());
  56. }
  57. else
  58. {
  59. // this is an object in a scene, modified at edit-time. we can store this as sceneViewId
  60. int idValue = EditorGUILayout.IntField("View ID [1.." + (PhotonNetwork.MAX_VIEW_IDS - 1) + "]", this.m_Target.sceneViewId);
  61. if (this.m_Target.sceneViewId != idValue)
  62. {
  63. Undo.RecordObject(this.m_Target, "Change PhotonView viewID");
  64. this.m_Target.sceneViewId = idValue;
  65. }
  66. }
  67. }
  68. // Locally Controlled
  69. if (EditorApplication.isPlaying)
  70. {
  71. string masterClientHint = PhotonNetwork.IsMasterClient ? " (master)" : "";
  72. EditorGUILayout.LabelField("IsMine:", this.m_Target.IsMine.ToString() + masterClientHint);
  73. Room room = PhotonNetwork.CurrentRoom;
  74. int cretrId = this.m_Target.CreatorActorNr;
  75. Player cretr = (room != null) ? room.GetPlayer(cretrId) : null;
  76. Player owner = this.m_Target.Owner;
  77. Player ctrlr = this.m_Target.Controller;
  78. EditorGUILayout.LabelField("Controller:", (ctrlr != null ? ("[" + ctrlr.ActorNumber + "] '" + ctrlr.NickName + "' " + (ctrlr.IsMasterClient ? " (master)" : "")) : "[0] <null>"));
  79. EditorGUILayout.LabelField("Owner:", (owner != null ? ("[" + owner.ActorNumber + "] '" + owner.NickName + "' " + (owner.IsMasterClient ? " (master)" : "")) : "[0] <null>"));
  80. EditorGUILayout.LabelField("Creator:", (cretr != null ? ("[" +cretrId + "] '" + cretr.NickName + "' " + (cretr.IsMasterClient ? " (master)" : "")) : "[0] <null>"));
  81. }
  82. EditorGUILayout.EndVertical();
  83. EditorGUI.BeginDisabledGroup(Application.isPlaying);
  84. GUILayout.Space(5);
  85. // Ownership section
  86. EditorGUILayout.LabelField("Ownership", (GUIStyle)"BoldLabel");
  87. OwnershipOption own = (OwnershipOption)EditorGUILayout.EnumPopup(ownerTransferGuiContent, this.m_Target.OwnershipTransfer/*, GUILayout.MaxWidth(68), GUILayout.MinWidth(68)*/);
  88. if (own != this.m_Target.OwnershipTransfer)
  89. {
  90. // jf: fixed 5 and up prefab not accepting changes if you quit Unity straight after change.
  91. // not touching the define nor the rest of the code to avoid bringing more problem than solving.
  92. EditorUtility.SetDirty(this.m_Target);
  93. Undo.RecordObject(this.m_Target, "Change PhotonView Ownership Transfer");
  94. this.m_Target.OwnershipTransfer = own;
  95. }
  96. GUILayout.Space(5);
  97. // Observables section
  98. EditorGUILayout.LabelField("Observables", (GUIStyle)"BoldLabel");
  99. EditorGUILayout.PropertyField(this.serializedObject.FindProperty("Synchronization"), syncronizationGuiContent);
  100. if (this.m_Target.Synchronization == ViewSynchronization.Off)
  101. {
  102. // Show warning if there are any observables. The null check is because the list allows nulls.
  103. var observed = m_Target.ObservedComponents;
  104. if (observed.Count > 0)
  105. {
  106. for (int i = 0, cnt = observed.Count; i < cnt; ++i)
  107. if (observed[i] != null)
  108. {
  109. EditorGUILayout.HelpBox("Synchronization is set to Off. Select a Synchronization setting in order to sync the listed Observables.", MessageType.Warning);
  110. break;
  111. }
  112. }
  113. }
  114. PhotonView.ObservableSearch autoFindObservables = (PhotonView.ObservableSearch)EditorGUILayout.EnumPopup(observableSearchGuiContent, m_Target.observableSearch);
  115. if (m_Target.observableSearch != autoFindObservables)
  116. {
  117. Undo.RecordObject(this.m_Target, "Change Auto Find Observables Toggle");
  118. m_Target.observableSearch = autoFindObservables;
  119. }
  120. m_Target.FindObservables();
  121. if (!multiSelected)
  122. {
  123. bool disableList = Application.isPlaying || autoFindObservables != PhotonView.ObservableSearch.Manual;
  124. if (disableList)
  125. EditorGUI.BeginDisabledGroup(true);
  126. this.DrawObservedComponentsList(disableList);
  127. if (disableList)
  128. EditorGUI.EndDisabledGroup();
  129. }
  130. // Cleanup: save and fix look
  131. if (GUI.changed)
  132. {
  133. PhotonViewHandler.OnHierarchyChanged(); // TODO: check if needed
  134. }
  135. EditorGUI.EndDisabledGroup();
  136. }
  137. private int GetObservedComponentsCount()
  138. {
  139. int count = 0;
  140. for (int i = 0; i < this.m_Target.ObservedComponents.Count; ++i)
  141. {
  142. if (this.m_Target.ObservedComponents[i] != null)
  143. {
  144. count++;
  145. }
  146. }
  147. return count;
  148. }
  149. /// <summary>
  150. /// Find Observables, and then baking them into the serialized object.
  151. /// </summary>
  152. private void EditorFindObservables()
  153. {
  154. Undo.RecordObject(serializedObject.targetObject, "Find Observables");
  155. var property = serializedObject.FindProperty("ObservedComponents");
  156. // Just doing a Find updates the Observables list, but Unity fails to save that change.
  157. // Instead we do the find, and then iterate the found objects into the serialize property, then apply that.
  158. property.ClearArray();
  159. m_Target.FindObservables(true);
  160. for(int i = 0; i < m_Target.ObservedComponents.Count; ++i)
  161. {
  162. property.InsertArrayElementAtIndex(i);
  163. property.GetArrayElementAtIndex(i).objectReferenceValue = m_Target.ObservedComponents[i];
  164. }
  165. serializedObject.ApplyModifiedProperties();
  166. }
  167. private void DrawObservedComponentsList(bool disabled = false)
  168. {
  169. SerializedProperty listProperty = this.serializedObject.FindProperty("ObservedComponents");
  170. if (listProperty == null)
  171. {
  172. return;
  173. }
  174. float containerElementHeight = 22;
  175. float containerHeight = listProperty.arraySize * containerElementHeight;
  176. string foldoutLabel = "Observed Components (" + this.GetObservedComponentsCount() + ")";
  177. bool isOpen = PhotonGUI.ContainerHeaderFoldout(foldoutLabel, this.serializedObject.FindProperty("ObservedComponentsFoldoutOpen").boolValue, () => EditorFindObservables(), "Find");
  178. this.serializedObject.FindProperty("ObservedComponentsFoldoutOpen").boolValue = isOpen;
  179. if (isOpen == false)
  180. {
  181. containerHeight = 0;
  182. }
  183. //Texture2D statsIcon = AssetDatabase.LoadAssetAtPath( "Assets/Photon Unity Networking/Editor/PhotonNetwork/PhotonViewStats.png", typeof( Texture2D ) ) as Texture2D;
  184. Rect containerRect = PhotonGUI.ContainerBody(containerHeight);
  185. bool wasObservedComponentsEmpty = this.m_Target.ObservedComponents.FindAll(item => item != null).Count == 0;
  186. if (isOpen == true)
  187. {
  188. for (int i = 0; i < listProperty.arraySize; ++i)
  189. {
  190. Rect elementRect = new Rect(containerRect.xMin, containerRect.yMin + containerElementHeight * i, containerRect.width, containerElementHeight);
  191. {
  192. Rect texturePosition = new Rect(elementRect.xMin + 6, elementRect.yMin + elementRect.height / 2f - 1, 9, 5);
  193. ReorderableListResources.DrawTexture(texturePosition, ReorderableListResources.texGrabHandle);
  194. Rect propertyPosition = new Rect(elementRect.xMin + 20, elementRect.yMin + 3, elementRect.width - 45, 16);
  195. // keep track of old type to catch when a new type is observed
  196. Type _oldType = listProperty.GetArrayElementAtIndex(i).objectReferenceValue != null ? listProperty.GetArrayElementAtIndex(i).objectReferenceValue.GetType() : null;
  197. EditorGUI.PropertyField(propertyPosition, listProperty.GetArrayElementAtIndex(i), new GUIContent());
  198. // new type, could be different from old type
  199. Type _newType = listProperty.GetArrayElementAtIndex(i).objectReferenceValue != null ? listProperty.GetArrayElementAtIndex(i).objectReferenceValue.GetType() : null;
  200. // the user dropped a Transform, we must change it by adding a PhotonTransformView and observe that instead
  201. if (_oldType != _newType)
  202. {
  203. if (_newType == typeof(PhotonView))
  204. {
  205. listProperty.GetArrayElementAtIndex(i).objectReferenceValue = null;
  206. Debug.LogError("PhotonView Detected you dropped a PhotonView, this is not allowed. \n It's been removed from observed field.");
  207. }
  208. else if (_newType == typeof(Transform))
  209. {
  210. // try to get an existing PhotonTransformView ( we don't want any duplicates...)
  211. PhotonTransformView _ptv = this.m_Target.gameObject.GetComponent<PhotonTransformView>();
  212. if (_ptv == null)
  213. {
  214. // no ptv yet, we create one and enable position and rotation, no scaling, as it's too rarely needed to take bandwidth for nothing
  215. _ptv = Undo.AddComponent<PhotonTransformView>(this.m_Target.gameObject);
  216. }
  217. // switch observe from transform to _ptv
  218. listProperty.GetArrayElementAtIndex(i).objectReferenceValue = _ptv;
  219. Debug.Log("PhotonView has detected you dropped a Transform. Instead it's better to observe a PhotonTransformView for better control and performances");
  220. }
  221. else if (_newType == typeof(Rigidbody))
  222. {
  223. Rigidbody _rb = listProperty.GetArrayElementAtIndex(i).objectReferenceValue as Rigidbody;
  224. // try to get an existing PhotonRigidbodyView ( we don't want any duplicates...)
  225. PhotonRigidbodyView _prbv = _rb.gameObject.GetComponent<PhotonRigidbodyView>();
  226. if (_prbv == null)
  227. {
  228. // no _prbv yet, we create one
  229. _prbv = Undo.AddComponent<PhotonRigidbodyView>(_rb.gameObject);
  230. }
  231. // switch observe from transform to _prbv
  232. listProperty.GetArrayElementAtIndex(i).objectReferenceValue = _prbv;
  233. Debug.Log("PhotonView has detected you dropped a RigidBody. Instead it's better to observe a PhotonRigidbodyView for better control and performances");
  234. }
  235. else if (_newType == typeof(Rigidbody2D))
  236. {
  237. // try to get an existing PhotonRigidbody2DView ( we don't want any duplicates...)
  238. PhotonRigidbody2DView _prb2dv = this.m_Target.gameObject.GetComponent<PhotonRigidbody2DView>();
  239. if (_prb2dv == null)
  240. {
  241. // no _prb2dv yet, we create one
  242. _prb2dv = Undo.AddComponent<PhotonRigidbody2DView>(this.m_Target.gameObject);
  243. }
  244. // switch observe from transform to _prb2dv
  245. listProperty.GetArrayElementAtIndex(i).objectReferenceValue = _prb2dv;
  246. Debug.Log("PhotonView has detected you dropped a Rigidbody2D. Instead it's better to observe a PhotonRigidbody2DView for better control and performances");
  247. }
  248. else if (_newType == typeof(Animator))
  249. {
  250. // try to get an existing PhotonAnimatorView ( we don't want any duplicates...)
  251. PhotonAnimatorView _pav = this.m_Target.gameObject.GetComponent<PhotonAnimatorView>();
  252. if (_pav == null)
  253. {
  254. // no _pav yet, we create one
  255. _pav = Undo.AddComponent<PhotonAnimatorView>(this.m_Target.gameObject);
  256. }
  257. // switch observe from transform to _prb2dv
  258. listProperty.GetArrayElementAtIndex(i).objectReferenceValue = _pav;
  259. Debug.Log("PhotonView has detected you dropped a Animator, so we switched to PhotonAnimatorView so that you can serialized the Animator variables");
  260. }
  261. else if (!typeof(IPunObservable).IsAssignableFrom(_newType))
  262. {
  263. bool _ignore = false;
  264. #if PLAYMAKER
  265. _ignore = _newType == typeof(PlayMakerFSM);// Photon Integration for PlayMaker will swap at runtime to a proxy using iPunObservable.
  266. #endif
  267. if (_newType == null || _newType == typeof(Rigidbody) || _newType == typeof(Rigidbody2D))
  268. {
  269. _ignore = true;
  270. }
  271. if (!_ignore)
  272. {
  273. listProperty.GetArrayElementAtIndex(i).objectReferenceValue = null;
  274. Debug.LogError("PhotonView Detected you dropped a Component missing IPunObservable Interface,\n You dropped a <" + _newType + "> instead. It's been removed from observed field.");
  275. }
  276. }
  277. }
  278. //Debug.Log( listProperty.GetArrayElementAtIndex( i ).objectReferenceValue.GetType() );
  279. //Rect statsPosition = new Rect( propertyPosition.xMax + 7, propertyPosition.yMin, statsIcon.width, statsIcon.height );
  280. //ReorderableListResources.DrawTexture( statsPosition, statsIcon );
  281. Rect removeButtonRect = new Rect(elementRect.xMax - PhotonGUI.DefaultRemoveButtonStyle.fixedWidth,
  282. elementRect.yMin + 2,
  283. PhotonGUI.DefaultRemoveButtonStyle.fixedWidth,
  284. PhotonGUI.DefaultRemoveButtonStyle.fixedHeight);
  285. GUI.enabled = !disabled && listProperty.arraySize > 1;
  286. if (GUI.Button(removeButtonRect, new GUIContent(ReorderableListResources.texRemoveButton), PhotonGUI.DefaultRemoveButtonStyle))
  287. {
  288. listProperty.DeleteArrayElementAtIndex(i);
  289. }
  290. GUI.enabled = !disabled;
  291. if (i < listProperty.arraySize - 1)
  292. {
  293. texturePosition = new Rect(elementRect.xMin + 2, elementRect.yMax, elementRect.width - 4, 1);
  294. PhotonGUI.DrawSplitter(texturePosition);
  295. }
  296. }
  297. }
  298. }
  299. if (PhotonGUI.AddButton())
  300. {
  301. listProperty.InsertArrayElementAtIndex(Mathf.Max(0, listProperty.arraySize - 1));
  302. }
  303. this.serializedObject.ApplyModifiedProperties();
  304. bool isObservedComponentsEmpty = this.m_Target.ObservedComponents.FindAll(item => item != null).Count == 0;
  305. if (wasObservedComponentsEmpty == true && isObservedComponentsEmpty == false && this.m_Target.Synchronization == ViewSynchronization.Off)
  306. {
  307. Undo.RecordObject(this.m_Target, "Change PhotonView");
  308. this.m_Target.Synchronization = ViewSynchronization.UnreliableOnChange;
  309. this.serializedObject.Update();
  310. }
  311. if (wasObservedComponentsEmpty == false && isObservedComponentsEmpty == true)
  312. {
  313. Undo.RecordObject(this.m_Target, "Change PhotonView");
  314. this.m_Target.Synchronization = ViewSynchronization.Off;
  315. this.serializedObject.Update();
  316. }
  317. }
  318. }
  319. }