ClientWebsocketSettingsDrawer.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace Mirror.SimpleWeb.Editor
  4. {
  5. #if UNITY_EDITOR
  6. [CustomPropertyDrawer(typeof(ClientWebsocketSettings))]
  7. public class ClientWebsocketSettingsDrawer : PropertyDrawer
  8. {
  9. readonly string websocketPortOptionName = nameof(ClientWebsocketSettings.ClientPortOption);
  10. readonly string customPortName = nameof(ClientWebsocketSettings.CustomClientPort);
  11. readonly GUIContent portOptionLabel = new GUIContent("Client Port Option",
  12. "Specify what port the client websocket connection uses (default same as server port)");
  13. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  14. {
  15. property.isExpanded = true;
  16. return SumPropertyHeights(property, websocketPortOptionName, customPortName);
  17. }
  18. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  19. {
  20. DrawPortSettings(position, property);
  21. }
  22. void DrawPortSettings(Rect position, SerializedProperty property)
  23. {
  24. SerializedProperty portOptionProp = property.FindPropertyRelative(websocketPortOptionName);
  25. SerializedProperty portProp = property.FindPropertyRelative(customPortName);
  26. float portOptionHeight = EditorGUI.GetPropertyHeight(portOptionProp);
  27. float portHeight = EditorGUI.GetPropertyHeight(portProp);
  28. float spacing = EditorGUIUtility.standardVerticalSpacing;
  29. bool wasEnabled = GUI.enabled;
  30. position.height = portOptionHeight;
  31. EditorGUI.PropertyField(position, portOptionProp, portOptionLabel);
  32. position.y += spacing + portOptionHeight;
  33. position.height = portHeight;
  34. WebsocketPortOption portOption = (WebsocketPortOption)portOptionProp.enumValueIndex;
  35. if (portOption == WebsocketPortOption.MatchWebpageProtocol || portOption == WebsocketPortOption.DefaultSameAsServer)
  36. {
  37. int port = 0;
  38. if (property.serializedObject.targetObject is SimpleWebTransport swt)
  39. if (portOption == WebsocketPortOption.MatchWebpageProtocol)
  40. port = swt.clientUseWss ? 443 : 80;
  41. else
  42. port = swt.port;
  43. GUI.enabled = false;
  44. EditorGUI.IntField(position, new GUIContent("Client Port"), port);
  45. GUI.enabled = wasEnabled;
  46. }
  47. else
  48. EditorGUI.PropertyField(position, portProp);
  49. position.y += spacing + portHeight;
  50. }
  51. float SumPropertyHeights(SerializedProperty property, params string[] propertyNames)
  52. {
  53. float totalHeight = 0;
  54. foreach (var name in propertyNames)
  55. totalHeight += EditorGUI.GetPropertyHeight(property.FindPropertyRelative(name)) + EditorGUIUtility.standardVerticalSpacing;
  56. return totalHeight;
  57. }
  58. }
  59. #endif
  60. }