暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SpriteEditorUtility.cs 6.4KB

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