HTGuiTools.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. public class HTGuiTools{
  5. private static Texture2D gradientTexture;
  6. #region Widget
  7. public static bool BeginFoldOut(string text,bool foldOut, bool endSpace=true){
  8. text = "<b><size=11>" + text + "</size></b>";
  9. if (foldOut){
  10. text = "\u25BC " + text;
  11. }
  12. else{
  13. text = "\u25BA " + text;
  14. }
  15. if ( !GUILayout.Toggle(true,text,"dragtab")){
  16. foldOut=!foldOut;
  17. }
  18. if (!foldOut && endSpace)GUILayout.Space(5f);
  19. return foldOut;
  20. }
  21. public static void BeginGroup(int padding=0){
  22. GUILayout.BeginHorizontal();
  23. GUILayout.Space(padding);
  24. EditorGUILayout.BeginHorizontal("As TextArea", GUILayout.MinHeight(10f));
  25. GUILayout.BeginVertical();
  26. GUILayout.Space(2f);
  27. }
  28. public static void EndGroup(bool endSpace = true){
  29. GUILayout.Space(3f);
  30. GUILayout.EndVertical();
  31. EditorGUILayout.EndHorizontal();
  32. GUILayout.Space(3f);
  33. GUILayout.EndHorizontal();
  34. if (endSpace){
  35. GUILayout.Space(10f);
  36. }
  37. }
  38. public static bool Toggle(string text, bool value,bool leftToggle=false){
  39. //if (value) GUI.backgroundColor = Color.green; else GUI.backgroundColor = Color.red;
  40. if (leftToggle){
  41. value = EditorGUILayout.ToggleLeft(text,value);
  42. }
  43. else{
  44. value = EditorGUILayout.Toggle(text,value);
  45. }
  46. //GUI.backgroundColor = Color.white;
  47. return value;
  48. }
  49. public static bool Toggle (string text, bool value,int width,bool leftToggle=false){
  50. if (value) GUI.backgroundColor = Color.green; else GUI.backgroundColor = Color.red;
  51. if (leftToggle){
  52. value = EditorGUILayout.ToggleLeft(text,value,GUILayout.Width( width));
  53. }
  54. else{
  55. value = EditorGUILayout.Toggle(text,value,GUILayout.Width( width));
  56. }
  57. GUI.backgroundColor = Color.white;
  58. return value;
  59. }
  60. static public bool Button(string label,Color color,int width, bool leftAligment=false, int height=0){
  61. GUI.backgroundColor = color;
  62. GUIStyle buttonStyle = new GUIStyle("Button");
  63. if (leftAligment)
  64. buttonStyle.alignment = TextAnchor.MiddleLeft;
  65. if (height==0){
  66. if (GUILayout.Button( label,buttonStyle,GUILayout.Width(width))){
  67. GUI.backgroundColor = Color.white;
  68. return true;
  69. }
  70. }
  71. else{
  72. if (GUILayout.Button( label,buttonStyle,GUILayout.Width(width),GUILayout.Height(height))){
  73. GUI.backgroundColor = Color.white;
  74. return true;
  75. }
  76. }
  77. GUI.backgroundColor = Color.white;
  78. return false;
  79. }
  80. #endregion
  81. #region 2d effect
  82. public static void DrawSeparatorLine(int padding=0){
  83. EditorGUILayout.Space();
  84. DrawLine(Color.gray, padding);
  85. EditorGUILayout.Space();
  86. }
  87. private static void DrawLine(Color color,int padding=0){
  88. GUILayout.Space(10);
  89. Rect lastRect = GUILayoutUtility.GetLastRect();
  90. GUI.color = color;
  91. GUI.DrawTexture(new Rect(padding, lastRect.yMax -lastRect.height/2f, Screen.width, 1f), EditorGUIUtility.whiteTexture);
  92. GUI.color = Color.white;
  93. }
  94. #endregion
  95. #region Texture
  96. private static Rect DrawGradient(int padding, int width, int height=35){
  97. GUILayout.Space(height);
  98. Rect lastRect = GUILayoutUtility.GetLastRect();
  99. lastRect.yMin = lastRect.yMin + 7;
  100. lastRect.yMax = lastRect.yMax - 7;
  101. lastRect.width = Screen.width;
  102. GUI.DrawTexture(new Rect(padding,lastRect.yMin+1,width, lastRect.yMax- lastRect.yMin), GetGradientTexture());
  103. return lastRect;
  104. }
  105. private static Texture2D GetGradientTexture(){
  106. if (gradientTexture==null){
  107. gradientTexture = CreateGradientTexture();
  108. }
  109. return gradientTexture;
  110. }
  111. private static Texture2D CreateGradientTexture(){
  112. int height =18;
  113. Texture2D myTexture = new Texture2D(1, height);
  114. myTexture.hideFlags = HideFlags.HideInInspector;
  115. myTexture.hideFlags = HideFlags.DontSave;
  116. myTexture.filterMode = FilterMode.Bilinear;
  117. Color startColor= new Color(0.4f,0.4f,0.4f);
  118. Color endColor= new Color(0.6f,0.6f,0.6f);
  119. float stepR = (endColor.r - startColor.r)/18f;
  120. float stepG = (endColor.g - startColor.g)/18f;
  121. float stepB = (endColor.b - startColor.b)/18f;
  122. Color pixColor = startColor;
  123. for (int i = 1; i < height-1; i++)
  124. {
  125. pixColor = new Color(pixColor.r + stepR,pixColor.g + stepG , pixColor.b + stepB);
  126. myTexture.SetPixel(0, i, pixColor);
  127. }
  128. myTexture.SetPixel(0, 0, new Color(0,0,0));
  129. myTexture.SetPixel(0, 17, new Color(1,1,1));
  130. myTexture.Apply();
  131. return myTexture;
  132. }
  133. #endregion
  134. }