ETCGuiTools.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. public class ETCGuiTools{
  5. private static Texture2D gradientTexture;
  6. private static Texture2D checkerTexture;
  7. #region Widget
  8. public static bool BeginFoldOut(string text,bool foldOut, bool endSpace=true){
  9. text = "<b><size=11>" + text + "</size></b>";
  10. if (foldOut){
  11. text = "\u25BC " + text;
  12. }
  13. else{
  14. text = "\u25BA " + text;
  15. }
  16. if ( !GUILayout.Toggle(true,text,"dragtab")){
  17. foldOut=!foldOut;
  18. }
  19. if (!foldOut && endSpace)GUILayout.Space(5f);
  20. return foldOut;
  21. }
  22. public static void BeginGroup(int padding=0){
  23. GUILayout.BeginHorizontal();
  24. GUILayout.Space(padding);
  25. EditorGUILayout.BeginHorizontal("As TextArea", GUILayout.MinHeight(10f));
  26. GUILayout.BeginVertical();
  27. GUILayout.Space(2f);
  28. }
  29. public static void EndGroup(bool endSpace = true){
  30. GUILayout.Space(3f);
  31. GUILayout.EndVertical();
  32. EditorGUILayout.EndHorizontal();
  33. GUILayout.Space(3f);
  34. GUILayout.EndHorizontal();
  35. if (endSpace){
  36. GUILayout.Space(10f);
  37. }
  38. }
  39. public static bool Toggle(string text, bool value,bool leftToggle=false, int width=-1, bool bold=false){
  40. //if (value) GUI.backgroundColor = Color.green; else GUI.backgroundColor = Color.red;
  41. GUIStyle boldStyle = new GUIStyle( "toggle");
  42. boldStyle.fontStyle = FontStyle.Bold;
  43. if (leftToggle){
  44. if (width==-1)
  45. value = EditorGUILayout.ToggleLeft(text,value);
  46. else
  47. value = GUILayout.Toggle(value,text,boldStyle,GUILayout.Width(width));
  48. }
  49. else{
  50. if (width==-1)
  51. value = EditorGUILayout.Toggle(text,value);
  52. else
  53. value = GUILayout.Toggle(value,text,boldStyle,GUILayout.Width(width));
  54. }
  55. //GUI.backgroundColor = Color.white;
  56. return value;
  57. }
  58. public static bool BeginToogleGroup(string text, bool value,int indent=0){
  59. if (value) GUI.backgroundColor = Color.green; else GUI.backgroundColor = Color.red;
  60. value = EditorGUILayout.BeginToggleGroup( text,value);
  61. GUI.backgroundColor = Color.white;
  62. if (value)
  63. ETCGuiTools.BeginGroup(indent);
  64. return value;
  65. }
  66. public static void EndToggleGroup( bool value,bool space=false){
  67. if (value)
  68. ETCGuiTools.EndGroup(space);
  69. EditorGUILayout.EndToggleGroup();
  70. }
  71. static public bool Button(string label,Color color,int width, bool leftAligment=false, int height=0){
  72. GUI.backgroundColor = color;
  73. GUIStyle buttonStyle = new GUIStyle("Button");
  74. if (leftAligment)
  75. buttonStyle.alignment = TextAnchor.MiddleLeft;
  76. if (height==0){
  77. if (GUILayout.Button( label,buttonStyle,GUILayout.Width(width))){
  78. GUI.backgroundColor = Color.white;
  79. return true;
  80. }
  81. }
  82. else{
  83. if (GUILayout.Button( label,buttonStyle,GUILayout.Width(width),GUILayout.Height(height))){
  84. GUI.backgroundColor = Color.white;
  85. return true;
  86. }
  87. }
  88. GUI.backgroundColor = Color.white;
  89. return false;
  90. }
  91. #endregion
  92. #region 2d effect
  93. public static void DrawSeparatorLine(int padding=0){
  94. EditorGUILayout.Space();
  95. DrawLine(Color.gray, padding);
  96. EditorGUILayout.Space();
  97. }
  98. private static void DrawLine(Color color,int padding=0){
  99. GUILayout.Space(10);
  100. Rect lastRect = GUILayoutUtility.GetLastRect();
  101. GUI.color = color;
  102. GUI.DrawTexture(new Rect(padding, lastRect.yMax -lastRect.height/2f, Screen.width, 1f), EditorGUIUtility.whiteTexture);
  103. GUI.color = Color.white;
  104. }
  105. #endregion
  106. #region Texture
  107. public static void DrawTextureRectPreview( Rect rect, Rect textureRect, Texture2D tex, Color color){
  108. GUI.color = color;
  109. GUI.DrawTexture( rect, EditorGUIUtility.whiteTexture);
  110. GUI.color = Color.white;
  111. rect = new Rect(rect.x+2,rect.y+2,rect.width-4,rect.height-4);
  112. DrawTileTexture( rect, ETCGuiTools.GetCheckerTexture());
  113. if (tex!=null){
  114. GUI.DrawTextureWithTexCoords( rect, tex,textureRect );
  115. }
  116. }
  117. public static void DrawTileTexture (Rect rect, Texture tex)
  118. {
  119. GUI.BeginGroup(rect);
  120. {
  121. int width = Mathf.RoundToInt(rect.width);
  122. int height = Mathf.RoundToInt(rect.height);
  123. for (int y = 0; y < height; y += tex.height)
  124. {
  125. for (int x = 0; x < width; x += tex.width)
  126. {
  127. GUI.DrawTexture(new Rect(x, y, tex.width, tex.height), tex);
  128. }
  129. }
  130. }
  131. GUI.EndGroup();
  132. }
  133. private static Rect DrawGradient(int padding, int width, int height=35){
  134. GUILayout.Space(height);
  135. Rect lastRect = GUILayoutUtility.GetLastRect();
  136. lastRect.yMin = lastRect.yMin + 7;
  137. lastRect.yMax = lastRect.yMax - 7;
  138. lastRect.width = Screen.width;
  139. GUI.DrawTexture(new Rect(padding,lastRect.yMin+1,width, lastRect.yMax- lastRect.yMin), GetGradientTexture());
  140. return lastRect;
  141. }
  142. private static Texture2D GetGradientTexture(){
  143. if (gradientTexture==null){
  144. gradientTexture = CreateGradientTexture();
  145. }
  146. return gradientTexture;
  147. }
  148. private static Texture2D CreateGradientTexture(){
  149. int height =18;
  150. Texture2D myTexture = new Texture2D(1, height);
  151. myTexture.hideFlags = HideFlags.HideInInspector;
  152. myTexture.hideFlags = HideFlags.DontSave;
  153. myTexture.filterMode = FilterMode.Bilinear;
  154. Color startColor= new Color(0.4f,0.4f,0.4f);
  155. Color endColor= new Color(0.6f,0.6f,0.6f);
  156. float stepR = (endColor.r - startColor.r)/18f;
  157. float stepG = (endColor.g - startColor.g)/18f;
  158. float stepB = (endColor.b - startColor.b)/18f;
  159. Color pixColor = startColor;
  160. for (int i = 1; i < height-1; i++)
  161. {
  162. pixColor = new Color(pixColor.r + stepR,pixColor.g + stepG , pixColor.b + stepB);
  163. myTexture.SetPixel(0, i, pixColor);
  164. }
  165. myTexture.SetPixel(0, 0, new Color(0,0,0));
  166. myTexture.SetPixel(0, 17, new Color(1,1,1));
  167. myTexture.Apply();
  168. return myTexture;
  169. }
  170. public static Texture2D GetCheckerTexture(){
  171. if (ETCGuiTools.checkerTexture==null){
  172. ETCGuiTools.checkerTexture = CreateCheckerTexture();
  173. }
  174. return checkerTexture;
  175. }
  176. private static Texture2D CreateCheckerTexture(){
  177. Texture2D myTexture = new Texture2D(16, 16);
  178. myTexture.hideFlags = HideFlags.DontSave;
  179. Color color1 = new Color(0.5f,0.5f,0.5f);
  180. for( int x=0;x<8;x++){
  181. for( int y=0;y<8;y++){
  182. myTexture.SetPixel(x, y, color1);
  183. myTexture.SetPixel(x+8, y+8, color1);
  184. }
  185. }
  186. color1 = new Color(0.3f,0.3f,0.3f);
  187. for( int x=0;x<8;x++){
  188. for( int y=0;y<8;y++){
  189. myTexture.SetPixel(x+8, y, color1);
  190. myTexture.SetPixel(x, y+8, color1);
  191. }
  192. }
  193. myTexture.Apply();
  194. return myTexture;
  195. }
  196. #endregion
  197. }