PhotonGUI.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="PhotonGUI.cs" company="Exit Games GmbH">
  3. // PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // GUI scripts for the Editor.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // ----------------------------------------------------------------------------
  10. using UnityEngine;
  11. using UnityEditor;
  12. namespace Photon.Pun
  13. {
  14. public class PhotonGUI
  15. {
  16. #region Styles
  17. static GUIStyle m_DefaultTitleStyle;
  18. public static GUIStyle DefaultTitleStyle
  19. {
  20. get
  21. {
  22. if (m_DefaultTitleStyle == null)
  23. {
  24. m_DefaultTitleStyle = new GUIStyle();
  25. m_DefaultTitleStyle.border = new RectOffset(2, 2, 2, 1);
  26. m_DefaultTitleStyle.margin = new RectOffset(5, 5, 5, 0);
  27. m_DefaultTitleStyle.padding = new RectOffset(5, 5, 0, 0);
  28. m_DefaultTitleStyle.alignment = TextAnchor.MiddleLeft;
  29. m_DefaultTitleStyle.normal.background = ReorderableListResources.texTitleBackground;
  30. m_DefaultTitleStyle.normal.textColor = EditorGUIUtility.isProSkin
  31. ? new Color(0.8f, 0.8f, 0.8f)
  32. : new Color(0.2f, 0.2f, 0.2f);
  33. }
  34. return m_DefaultTitleStyle;
  35. }
  36. }
  37. static GUIStyle m_DefaultContainerStyle;
  38. public static GUIStyle DefaultContainerStyle
  39. {
  40. get
  41. {
  42. if (m_DefaultContainerStyle == null)
  43. {
  44. m_DefaultContainerStyle = new GUIStyle();
  45. m_DefaultContainerStyle.border = new RectOffset(2, 2, 1, 2);
  46. m_DefaultContainerStyle.margin = new RectOffset(5, 5, 5, 5);
  47. m_DefaultContainerStyle.padding = new RectOffset(1, 1, 2, 2);
  48. m_DefaultContainerStyle.normal.background = ReorderableListResources.texContainerBackground;
  49. }
  50. return m_DefaultContainerStyle;
  51. }
  52. }
  53. static GUIStyle m_DefaultAddButtonStyle;
  54. public static GUIStyle DefaultAddButtonStyle
  55. {
  56. get
  57. {
  58. if (m_DefaultAddButtonStyle == null)
  59. {
  60. m_DefaultAddButtonStyle = new GUIStyle();
  61. m_DefaultAddButtonStyle.fixedWidth = 30;
  62. m_DefaultAddButtonStyle.fixedHeight = 16;
  63. m_DefaultAddButtonStyle.normal.background = ReorderableListResources.texAddButton;
  64. m_DefaultAddButtonStyle.active.background = ReorderableListResources.texAddButtonActive;
  65. }
  66. return m_DefaultAddButtonStyle;
  67. }
  68. }
  69. static GUIStyle m_DefaultRemoveButtonStyle;
  70. public static GUIStyle DefaultRemoveButtonStyle
  71. {
  72. get
  73. {
  74. if (m_DefaultRemoveButtonStyle == null)
  75. {
  76. m_DefaultRemoveButtonStyle = new GUIStyle();
  77. m_DefaultRemoveButtonStyle.fixedWidth = 30;
  78. m_DefaultRemoveButtonStyle.fixedHeight = 20;
  79. m_DefaultRemoveButtonStyle.active.background = ReorderableListResources.CreatePixelTexture("Dark Pixel (List GUI)", new Color32(18, 18, 18, 255));
  80. m_DefaultRemoveButtonStyle.imagePosition = ImagePosition.ImageOnly;
  81. m_DefaultRemoveButtonStyle.alignment = TextAnchor.MiddleCenter;
  82. }
  83. return m_DefaultRemoveButtonStyle;
  84. }
  85. }
  86. static GUIStyle m_DefaultContainerRowStyle;
  87. public static GUIStyle DefaultContainerRowStyle
  88. {
  89. get
  90. {
  91. if (m_DefaultContainerRowStyle == null)
  92. {
  93. m_DefaultContainerRowStyle = new GUIStyle();
  94. m_DefaultContainerRowStyle.border = new RectOffset(2, 2, 2, 2);
  95. m_DefaultContainerRowStyle.margin = new RectOffset(5, 5, 5, 5);
  96. m_DefaultContainerRowStyle.padding = new RectOffset(1, 1, 2, 2);
  97. m_DefaultContainerRowStyle.normal.background = ReorderableListResources.texContainerBackground;
  98. }
  99. return m_DefaultContainerRowStyle;
  100. }
  101. }
  102. static GUIStyle m_FoldoutBold;
  103. public static GUIStyle FoldoutBold
  104. {
  105. get
  106. {
  107. if (m_FoldoutBold == null)
  108. {
  109. m_FoldoutBold = new GUIStyle(EditorStyles.foldout);
  110. m_FoldoutBold.fontStyle = FontStyle.Bold;
  111. }
  112. return m_FoldoutBold;
  113. }
  114. }
  115. static GUIStyle m_RichLabel;
  116. public static GUIStyle RichLabel
  117. {
  118. get
  119. {
  120. if (m_RichLabel == null)
  121. {
  122. m_RichLabel = new GUIStyle(GUI.skin.label);
  123. m_RichLabel.richText = true;
  124. m_RichLabel.wordWrap = true;
  125. }
  126. return m_RichLabel;
  127. }
  128. }
  129. #endregion
  130. internal static string GetIconPath(string iconFileName)
  131. {
  132. string _thisIconPath = PhotonNetwork.FindAssetPath ("PhotonGUI");
  133. if (string.IsNullOrEmpty(_thisIconPath))
  134. {
  135. _thisIconPath = "Assets/Photon/PhotonUnityNetworking/Code/Editor/"+iconFileName;
  136. }
  137. else
  138. {
  139. _thisIconPath = _thisIconPath.Replace("PhotonGUI.cs", iconFileName);
  140. }
  141. return _thisIconPath;
  142. }
  143. static Texture2D m_HelpIcon;
  144. public static Texture2D HelpIcon
  145. {
  146. get
  147. {
  148. if (m_HelpIcon == null)
  149. {
  150. m_HelpIcon = AssetDatabase.LoadAssetAtPath(GetIconPath("help.png"), typeof(Texture2D)) as Texture2D;
  151. }
  152. return m_HelpIcon;
  153. }
  154. }
  155. static Texture2D m_CopyIcon;
  156. static Texture2D m_CopyIconPro;
  157. public static Texture2D CopyIcon
  158. {
  159. get
  160. {
  161. if (EditorGUIUtility.isProSkin)
  162. {
  163. if (m_CopyIconPro == null)
  164. {
  165. m_CopyIconPro = AssetDatabase.LoadAssetAtPath(GetIconPath("CopyIconPro.png"), typeof(Texture2D)) as Texture2D;
  166. }
  167. return m_CopyIconPro;
  168. }
  169. if (m_CopyIcon == null)
  170. {
  171. m_CopyIcon = AssetDatabase.LoadAssetAtPath(GetIconPath("CopyIcon.png"), typeof(Texture2D)) as Texture2D;
  172. }
  173. return m_CopyIcon;
  174. }
  175. }
  176. #region Interface
  177. public static void ContainerHeader(string headline)
  178. {
  179. DoContainerHeader(headline, 27, 0);
  180. }
  181. public static bool ContainerHeaderToggle(string headline, bool toggle)
  182. {
  183. return DoContainerHeaderToggle(headline, toggle);
  184. }
  185. public static bool ContainerHeaderFoldout(string headline, bool foldout, System.Action buttonAction = null, string buttonName = null)
  186. {
  187. return DoContainerHeaderFoldout(headline, foldout, buttonAction, buttonName);
  188. }
  189. public static Rect ContainerBody(float height)
  190. {
  191. return DoContainerBody(height);
  192. }
  193. public static bool AddButton()
  194. {
  195. Rect controlRect = EditorGUILayout.GetControlRect(false, DefaultAddButtonStyle.fixedHeight - 5);
  196. controlRect.yMin -= 5;
  197. controlRect.yMax -= 5;
  198. Rect addButtonRect = new Rect(controlRect.xMax - DefaultAddButtonStyle.fixedWidth,
  199. controlRect.yMin,
  200. DefaultAddButtonStyle.fixedWidth,
  201. DefaultAddButtonStyle.fixedHeight);
  202. return GUI.Button(addButtonRect, "", DefaultAddButtonStyle);
  203. }
  204. public static void DrawSplitter(Rect position)
  205. {
  206. ReorderableListResources.DrawTexture(position, ReorderableListResources.texItemSplitter);
  207. }
  208. public static void DrawGizmoOptions(
  209. Rect position,
  210. string label,
  211. SerializedProperty gizmoEnabledProperty,
  212. SerializedProperty gizmoColorProperty,
  213. SerializedProperty gizmoTypeProperty,
  214. SerializedProperty gizmoSizeProperty)
  215. {
  216. float height = EditorGUIUtility.singleLineHeight;
  217. float flexibleWidth = Mathf.Max(40, position.width - EditorGUIUtility.labelWidth - 20 - 75 - 5 - 40 - 5);
  218. Rect labelRect = new Rect(position.xMin, position.yMin, EditorGUIUtility.labelWidth, height);
  219. GUI.Label(labelRect, label);
  220. Rect enabledRect = new Rect(labelRect.xMax, labelRect.yMin, 20, height);
  221. EditorGUI.PropertyField(enabledRect, gizmoEnabledProperty, GUIContent.none);
  222. bool oldGUIEnabled = GUI.enabled;
  223. GUI.enabled = gizmoEnabledProperty.boolValue;
  224. Rect colorRect = new Rect(enabledRect.xMax + 5, labelRect.yMin, 70, height);
  225. EditorGUI.PropertyField(colorRect, gizmoColorProperty, GUIContent.none);
  226. Rect typeRect = new Rect(colorRect.xMax + 5, labelRect.yMin, flexibleWidth * 0.7f, height);
  227. EditorGUI.PropertyField(typeRect, gizmoTypeProperty, GUIContent.none);
  228. Rect sizeLabelRect = new Rect(typeRect.xMax + 10, labelRect.yMin, 30, height);
  229. GUI.Label(sizeLabelRect, "Size");
  230. Rect sizeRect = new Rect(sizeLabelRect.xMax + 5, labelRect.yMin, flexibleWidth * 0.3f, height);
  231. EditorGUI.PropertyField(sizeRect, gizmoSizeProperty, GUIContent.none);
  232. GUI.enabled = oldGUIEnabled;
  233. }
  234. #endregion
  235. #region Implementation
  236. static Rect DoContainerBody(float height)
  237. {
  238. Rect controlRect = EditorGUILayout.GetControlRect(false, height);
  239. controlRect.yMin -= 3;
  240. controlRect.yMax -= 2;
  241. int controlID = GUIUtility.GetControlID(FocusType.Passive, controlRect);
  242. if (Event.current.type == EventType.Repaint)
  243. {
  244. PhotonGUI.DefaultContainerStyle.Draw(controlRect, GUIContent.none, controlID);
  245. }
  246. return controlRect;
  247. }
  248. static bool DoContainerHeaderToggle(string headline, bool toggle)
  249. {
  250. Rect rect = DoContainerHeader(headline, 27, 15);
  251. Rect toggleRect = new Rect(rect.xMin + 5, rect.yMin + 5, EditorGUIUtility.labelWidth, rect.height);
  252. return EditorGUI.Toggle(toggleRect, toggle);
  253. }
  254. static bool DoContainerHeaderFoldout(string headline, bool foldout, System.Action buttonAction = null, string buttonLabel = null, float buttonWidth = 48)
  255. {
  256. bool showButton = buttonAction != null;
  257. Rect rect = DoContainerHeader("", 27, 0f);
  258. // Shorten foldout label if button is present, so it doesn't interfere with clicking.
  259. float foldoutWidth = rect.width - (showButton ? 15 + buttonWidth: 15);
  260. Rect foldoutRect = new Rect(rect.xMin + 15, rect.yMin + 5, foldoutWidth, 16);
  261. bool expanded = EditorGUI.Foldout(foldoutRect, foldout, headline, FoldoutBold);
  262. // If a button is defined show it, and invoke action on click.
  263. if (showButton && GUI.Button(new Rect(foldoutRect) { x = foldoutRect.xMax, height = 17, width = buttonWidth - 4 }, buttonLabel == null ? "" : buttonLabel))
  264. {
  265. buttonAction.Invoke();
  266. }
  267. return expanded;
  268. }
  269. static Rect DoContainerHeader(string headline, float height, float contentOffset)
  270. {
  271. GUILayout.Space(5);
  272. Rect controlRect = EditorGUILayout.GetControlRect(false, height);
  273. int controlID = GUIUtility.GetControlID(FocusType.Passive, controlRect);
  274. if (Event.current.type == EventType.Repaint)
  275. {
  276. PhotonGUI.DefaultTitleStyle.Draw(controlRect, GUIContent.none, controlID);
  277. Rect labelRect = new Rect(controlRect.xMin + 5 + contentOffset, controlRect.yMin + 5, controlRect.width, controlRect.height);
  278. GUI.Label(labelRect, headline, EditorStyles.boldLabel);
  279. }
  280. return controlRect;
  281. }
  282. #endregion
  283. }
  284. }