GUIHelper.cs 2.7 KB

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