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

GridPaletteAddPopup.cs 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using UnityEngine;
  2. namespace UnityEditor.Tilemaps
  3. {
  4. internal class GridPaletteAddPopup : EditorWindow
  5. {
  6. static class Styles
  7. {
  8. public static readonly GUIContent nameLabel = EditorGUIUtility.TrTextContent("Name");
  9. public static readonly GUIContent ok = EditorGUIUtility.TrTextContent("Create");
  10. public static readonly GUIContent cancel = EditorGUIUtility.TrTextContent("Cancel");
  11. public static readonly GUIContent header = EditorGUIUtility.TrTextContent("Create New Palette");
  12. public static readonly GUIContent gridLabel = EditorGUIUtility.TrTextContent("Grid");
  13. public static readonly GUIContent sizeLabel = EditorGUIUtility.TrTextContent("Cell Size");
  14. public static readonly GUIContent hexagonLabel = EditorGUIUtility.TrTextContent("Hexagon Type");
  15. public static readonly GUIContent[] hexagonSwizzleTypeLabel =
  16. {
  17. EditorGUIUtility.TrTextContent("Point Top"),
  18. EditorGUIUtility.TrTextContent("Flat Top"),
  19. };
  20. public static readonly GridLayout.CellSwizzle[] hexagonSwizzleTypeValue =
  21. {
  22. GridLayout.CellSwizzle.XYZ,
  23. GridLayout.CellSwizzle.YXZ,
  24. };
  25. public static readonly GUIContent transparencySortModeLabel =
  26. EditorGUIUtility.TrTextContent("Sort Mode");
  27. public static readonly GUIContent transparencySortAxisLabel =
  28. EditorGUIUtility.TrTextContent("Sort Axis");
  29. }
  30. private static long s_LastClosedTime;
  31. private string m_Name = "New Palette";
  32. private static GridPaletteAddPopup s_Instance;
  33. private GridLayout.CellLayout m_Layout;
  34. private int m_HexagonLayout;
  35. private GridPalette.CellSizing m_CellSizing;
  36. private Vector3 m_CellSize;
  37. private TransparencySortMode m_TransparencySortMode;
  38. private Vector3 m_TransparencySortAxis = new Vector3(0f, 0f, 1f);
  39. void Init(Rect buttonRect)
  40. {
  41. m_CellSize = new Vector3(1, 1, 0);
  42. buttonRect = GUIUtility.GUIToScreenRect(buttonRect);
  43. ShowAsDropDown(buttonRect, new Vector2(312, 185));
  44. }
  45. internal void OnGUI()
  46. {
  47. GUI.Label(new Rect(0, 0, position.width, position.height), GUIContent.none, "grey_border");
  48. GUILayout.Space(3);
  49. GUILayout.Label(Styles.header, EditorStyles.boldLabel);
  50. GUILayout.Space(4);
  51. GUILayout.BeginHorizontal();
  52. GUILayout.Label(Styles.nameLabel, GUILayout.Width(90f));
  53. m_Name = EditorGUILayout.TextField(m_Name);
  54. GUILayout.EndHorizontal();
  55. GUILayout.BeginHorizontal();
  56. GUILayout.Label(Styles.gridLabel, GUILayout.Width(90f));
  57. EditorGUI.BeginChangeCheck();
  58. var newLayout = (GridLayout.CellLayout)EditorGUILayout.EnumPopup(m_Layout);
  59. if (EditorGUI.EndChangeCheck())
  60. {
  61. // Set useful user settings for certain layouts
  62. switch (newLayout)
  63. {
  64. case GridLayout.CellLayout.Rectangle:
  65. case GridLayout.CellLayout.Hexagon:
  66. {
  67. m_CellSizing = GridPalette.CellSizing.Manual;
  68. m_CellSize = new Vector3(0.8659766f, 1, 0);
  69. break;
  70. }
  71. case GridLayout.CellLayout.Isometric:
  72. {
  73. m_CellSizing = GridPalette.CellSizing.Manual;
  74. m_CellSize = new Vector3(1, 0.5f, 1);
  75. break;
  76. }
  77. case GridLayout.CellLayout.IsometricZAsY:
  78. {
  79. m_CellSizing = GridPalette.CellSizing.Manual;
  80. m_CellSize = new Vector3(1, 0.5f, 1);
  81. m_TransparencySortMode = TransparencySortMode.CustomAxis;
  82. m_TransparencySortAxis = new Vector3(0f, 1f, -0.26f);
  83. break;
  84. }
  85. }
  86. m_Layout = newLayout;
  87. }
  88. GUILayout.EndHorizontal();
  89. if (m_Layout == GridLayout.CellLayout.Hexagon)
  90. {
  91. GUILayout.BeginHorizontal();
  92. float oldLabelWidth = EditorGUIUtility.labelWidth;
  93. EditorGUIUtility.labelWidth = 94;
  94. m_HexagonLayout = EditorGUILayout.Popup(Styles.hexagonLabel, m_HexagonLayout, Styles.hexagonSwizzleTypeLabel);
  95. EditorGUIUtility.labelWidth = oldLabelWidth;
  96. GUILayout.EndHorizontal();
  97. }
  98. GUILayout.BeginHorizontal();
  99. GUILayout.Label(Styles.sizeLabel, GUILayout.Width(90f));
  100. m_CellSizing = (GridPalette.CellSizing)EditorGUILayout.EnumPopup(m_CellSizing);
  101. GUILayout.EndHorizontal();
  102. using (new EditorGUI.DisabledScope(m_CellSizing == GridPalette.CellSizing.Automatic))
  103. {
  104. GUILayout.BeginHorizontal();
  105. GUILayout.Label(GUIContent.none, GUILayout.Width(90f));
  106. m_CellSize = EditorGUILayout.Vector3Field(GUIContent.none, m_CellSize);
  107. GUILayout.EndHorizontal();
  108. }
  109. GUILayout.FlexibleSpace();
  110. GUILayout.BeginHorizontal();
  111. GUILayout.Label(Styles.transparencySortModeLabel, GUILayout.Width(90f));
  112. m_TransparencySortMode = (TransparencySortMode)EditorGUILayout.EnumPopup(m_TransparencySortMode);
  113. GUILayout.EndHorizontal();
  114. using (new EditorGUI.DisabledScope(m_TransparencySortMode != TransparencySortMode.CustomAxis))
  115. {
  116. GUILayout.BeginHorizontal();
  117. GUILayout.Label(Styles.transparencySortAxisLabel, GUILayout.Width(90f));
  118. m_TransparencySortAxis = EditorGUILayout.Vector3Field("", m_TransparencySortAxis);
  119. GUILayout.EndHorizontal();
  120. }
  121. GUILayout.FlexibleSpace();
  122. // Cancel, Ok
  123. GUILayout.BeginHorizontal();
  124. GUILayout.Space(10);
  125. if (GUILayout.Button(Styles.cancel))
  126. {
  127. Close();
  128. }
  129. using (new EditorGUI.DisabledScope(!Utils.Paths.IsValidAssetPath(m_Name)))
  130. {
  131. if (GUILayout.Button(Styles.ok))
  132. {
  133. s_LastClosedTime = System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond;
  134. // case 1077362: Close window to prevent overlap with OS folder window when saving new palette asset
  135. Close();
  136. var swizzle = GridLayout.CellSwizzle.XYZ;
  137. if (m_Layout == GridLayout.CellLayout.Hexagon)
  138. swizzle = Styles.hexagonSwizzleTypeValue[m_HexagonLayout];
  139. GameObject go = GridPaletteUtility.CreateNewPaletteAtCurrentFolder(m_Name, m_Layout, m_CellSizing, m_CellSize
  140. , swizzle, m_TransparencySortMode, m_TransparencySortAxis);
  141. if (go != null)
  142. {
  143. GridPaintingState.palette = go;
  144. }
  145. GUIUtility.ExitGUI();
  146. }
  147. }
  148. GUILayout.Space(10);
  149. GUILayout.EndHorizontal();
  150. }
  151. internal static bool ShowAtPosition(Rect buttonRect)
  152. {
  153. // We could not use realtimeSinceStartUp since it is set to 0 when entering/exitting playmode, we assume an increasing time when comparing time.
  154. long nowMilliSeconds = System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond;
  155. bool justClosed = nowMilliSeconds < s_LastClosedTime + 50;
  156. if (!justClosed)
  157. {
  158. Event.current.Use();
  159. if (s_Instance == null)
  160. s_Instance = ScriptableObject.CreateInstance<GridPaletteAddPopup>();
  161. s_Instance.Init(buttonRect);
  162. return true;
  163. }
  164. return false;
  165. }
  166. }
  167. }
  168. // namespace