GUIHelper.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using UnityEngine;
  3. namespace BestHTTP.Examples
  4. {
  5. public static class GUIHelper
  6. {
  7. private static GUIStyle centerAlignedLabel;
  8. private static GUIStyle rightAlignedLabel;
  9. public static Rect ClientArea;
  10. private static void Setup()
  11. {
  12. // These has to be called from OnGUI
  13. if (centerAlignedLabel == null)
  14. {
  15. centerAlignedLabel = new GUIStyle(GUI.skin.label);
  16. centerAlignedLabel.alignment = TextAnchor.MiddleCenter;
  17. rightAlignedLabel = new GUIStyle(GUI.skin.label);
  18. rightAlignedLabel.alignment = TextAnchor.MiddleRight;
  19. }
  20. }
  21. public static void DrawArea(Rect area, bool drawHeader, Action action)
  22. {
  23. Setup();
  24. // Draw background
  25. GUI.Box(area, string.Empty);
  26. GUILayout.BeginArea(area);
  27. if (drawHeader)
  28. {
  29. GUIHelper.DrawCenteredText(SampleSelector.SelectedSample.DisplayName);
  30. GUILayout.Space(5);
  31. }
  32. if (action != null)
  33. action();
  34. GUILayout.EndArea();
  35. }
  36. public static void DrawCenteredText(string msg)
  37. {
  38. Setup();
  39. GUILayout.BeginHorizontal();
  40. GUILayout.FlexibleSpace();
  41. GUILayout.Label(msg, centerAlignedLabel);
  42. GUILayout.FlexibleSpace();
  43. GUILayout.EndHorizontal();
  44. }
  45. public static void DrawRow(string key, string value)
  46. {
  47. Setup();
  48. GUILayout.BeginHorizontal();
  49. GUILayout.Label(key);
  50. GUILayout.FlexibleSpace();
  51. GUILayout.Label(value, rightAlignedLabel);
  52. GUILayout.FlexibleSpace();
  53. GUILayout.EndHorizontal();
  54. }
  55. }
  56. public class GUIMessageList
  57. {
  58. System.Collections.Generic.List<string> messages = new System.Collections.Generic.List<string>();
  59. Vector2 scrollPos;
  60. public void Draw()
  61. {
  62. Draw(Screen.width, 0);
  63. }
  64. public void Draw(float minWidth, float minHeight)
  65. {
  66. scrollPos = GUILayout.BeginScrollView(scrollPos, false, false, GUILayout.MinHeight(minHeight));
  67. for (int i = 0; i < messages.Count; ++i)
  68. GUILayout.Label(messages[i], GUILayout.MinWidth(minWidth));
  69. GUILayout.EndScrollView();
  70. }
  71. public void Add(string msg)
  72. {
  73. messages.Add(msg);
  74. scrollPos = new Vector2(scrollPos.x, float.MaxValue);
  75. }
  76. public void Clear()
  77. {
  78. messages.Clear();
  79. }
  80. }
  81. }