No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SpriteEditorUtility.cs 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Assertions;
  4. using UnityEvent = UnityEngine.Event;
  5. namespace UnityEditor.U2D.Sprites
  6. {
  7. static internal class SpriteEditorUtility
  8. {
  9. public static Vector2 GetPivotValue(SpriteAlignment alignment, Vector2 customOffset)
  10. {
  11. switch (alignment)
  12. {
  13. case SpriteAlignment.BottomLeft:
  14. return new Vector2(0f, 0f);
  15. case SpriteAlignment.BottomCenter:
  16. return new Vector2(0.5f, 0f);
  17. case SpriteAlignment.BottomRight:
  18. return new Vector2(1f, 0f);
  19. case SpriteAlignment.LeftCenter:
  20. return new Vector2(0f, 0.5f);
  21. case SpriteAlignment.Center:
  22. return new Vector2(0.5f, 0.5f);
  23. case SpriteAlignment.RightCenter:
  24. return new Vector2(1f, 0.5f);
  25. case SpriteAlignment.TopLeft:
  26. return new Vector2(0f, 1f);
  27. case SpriteAlignment.TopCenter:
  28. return new Vector2(0.5f, 1f);
  29. case SpriteAlignment.TopRight:
  30. return new Vector2(1f, 1f);
  31. case SpriteAlignment.Custom:
  32. return customOffset;
  33. }
  34. return Vector2.zero;
  35. }
  36. public static Rect RoundedRect(Rect rect)
  37. {
  38. return new Rect(
  39. Mathf.RoundToInt(rect.xMin),
  40. Mathf.RoundToInt(rect.yMin),
  41. Mathf.RoundToInt(rect.width),
  42. Mathf.RoundToInt(rect.height)
  43. );
  44. }
  45. public static Rect RoundToInt(Rect r)
  46. {
  47. r.xMin = Mathf.RoundToInt(r.xMin);
  48. r.yMin = Mathf.RoundToInt(r.yMin);
  49. r.xMax = Mathf.RoundToInt(r.xMax);
  50. r.yMax = Mathf.RoundToInt(r.yMax);
  51. return r;
  52. }
  53. public static Rect ClampedRect(Rect rect, Rect clamp, bool maintainSize)
  54. {
  55. Rect r = new Rect(rect);
  56. if (maintainSize)
  57. {
  58. Vector2 center = rect.center;
  59. if (center.x + Mathf.Abs(rect.width) * .5f > clamp.xMax)
  60. center.x = clamp.xMax - rect.width * .5f;
  61. if (center.x - Mathf.Abs(rect.width) * .5f < clamp.xMin)
  62. center.x = clamp.xMin + rect.width * .5f;
  63. if (center.y + Mathf.Abs(rect.height) * .5f > clamp.yMax)
  64. center.y = clamp.yMax - rect.height * .5f;
  65. if (center.y - Mathf.Abs(rect.height) * .5f < clamp.yMin)
  66. center.y = clamp.yMin + rect.height * .5f;
  67. r.center = center;
  68. }
  69. else
  70. {
  71. if (r.width > 0f)
  72. {
  73. r.xMin = Mathf.Max(rect.xMin, clamp.xMin);
  74. r.xMax = Mathf.Min(rect.xMax, clamp.xMax);
  75. }
  76. else
  77. {
  78. r.xMin = Mathf.Min(rect.xMin, clamp.xMax);
  79. r.xMax = Mathf.Max(rect.xMax, clamp.xMin);
  80. }
  81. if (r.height > 0f)
  82. {
  83. r.yMin = Mathf.Max(rect.yMin, clamp.yMin);
  84. r.yMax = Mathf.Min(rect.yMax, clamp.yMax);
  85. }
  86. else
  87. {
  88. r.yMin = Mathf.Min(rect.yMin, clamp.yMax);
  89. r.yMax = Mathf.Max(rect.yMax, clamp.yMin);
  90. }
  91. }
  92. r.width = Mathf.Abs(r.width);
  93. r.height = Mathf.Abs(r.height);
  94. return r;
  95. }
  96. public static void DrawBox(Rect position)
  97. {
  98. var points0 = new Vector3(position.xMin, position.yMin, 0f);
  99. var points1 = new Vector3(position.xMax, position.yMin, 0f);
  100. var points2 = new Vector3(position.xMax, position.yMax, 0f);
  101. var points3 = new Vector3(position.xMin, position.yMax, 0f);
  102. DrawLine(points0, points1);
  103. DrawLine(points1, points2);
  104. DrawLine(points2, points3);
  105. DrawLine(points3, points0);
  106. }
  107. public static void DrawLine(Vector3 p1, Vector3 p2)
  108. {
  109. GL.Vertex(p1);
  110. GL.Vertex(p2);
  111. }
  112. public static void BeginLines(Color color)
  113. {
  114. Assert.IsTrue(UnityEvent.current.type == EventType.Repaint);
  115. HandleUtility.ApplyWireMaterial();
  116. GL.PushMatrix();
  117. GL.MultMatrix(Handles.matrix);
  118. GL.Begin(GL.LINES);
  119. GL.Color(color);
  120. }
  121. public static void EndLines()
  122. {
  123. Assert.IsTrue(UnityEvent.current.type == EventType.Repaint);
  124. GL.End();
  125. GL.PopMatrix();
  126. }
  127. public static void FourIntFields(Vector2 rectSize, GUIContent label, GUIContent labelX, GUIContent labelY, GUIContent labelZ, GUIContent labelW, ref int x, ref int y, ref int z, ref int w)
  128. {
  129. Rect rect = GUILayoutUtility.GetRect(rectSize.x, rectSize.y);
  130. Rect labelRect = rect;
  131. labelRect.width = EditorGUIUtility.labelWidth;
  132. labelRect.height = EditorGUI.kSingleLineHeight;
  133. GUI.Label(labelRect, label);
  134. Rect fieldRect = rect;
  135. fieldRect.width -= EditorGUIUtility.labelWidth;
  136. fieldRect.height = EditorGUI.kSingleLineHeight;
  137. fieldRect.x += EditorGUIUtility.labelWidth;
  138. fieldRect.width /= 2;
  139. fieldRect.width -= EditorGUI.kSpacingSubLabel;
  140. float oldLabelWidth = EditorGUIUtility.labelWidth;
  141. EditorGUIUtility.labelWidth = EditorGUI.kMiniLabelW;
  142. GUI.SetNextControlName("FourIntFields_x");
  143. x = EditorGUI.IntField(fieldRect, labelX, x);
  144. fieldRect.x += fieldRect.width + EditorGUI.kSpacing;
  145. GUI.SetNextControlName("FourIntFields_y");
  146. y = EditorGUI.IntField(fieldRect, labelY, y);
  147. fieldRect.y += EditorGUI.kSingleLineHeight + EditorGUI.kVerticalSpacingMultiField;
  148. fieldRect.x -= fieldRect.width + EditorGUI.kSpacing;
  149. GUI.SetNextControlName("FourIntFields_z");
  150. z = EditorGUI.IntField(fieldRect, labelZ, z);
  151. fieldRect.x += fieldRect.width + EditorGUI.kSpacing;
  152. GUI.SetNextControlName("FourIntFields_w");
  153. w = EditorGUI.IntField(fieldRect, labelW, w);
  154. EditorGUIUtility.labelWidth = oldLabelWidth;
  155. }
  156. public static void DetermineGridCellSizeWithCellCount(int width, int height, Vector2 offset, Vector2 padding, Vector2 cellCount, out Vector2 cellSize)
  157. {
  158. cellSize.x = (width - offset.x - (padding.x * Math.Max(0, cellCount.x - 1))) / cellCount.x;
  159. cellSize.y = (height - offset.y - (padding.y * Math.Max(0, cellCount.y - 1))) / cellCount.y;
  160. cellSize.x = Mathf.Floor(cellSize.x);
  161. cellSize.y = Mathf.Floor(cellSize.y);
  162. cellSize.x = Mathf.Clamp(cellSize.x, 1, width);
  163. cellSize.y = Mathf.Clamp(cellSize.y, 1, height);
  164. }
  165. }
  166. }