SpriteSelectionWidget.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. namespace UnityEditor.Experimental.U2D.Animation
  5. {
  6. internal class SpriteSelectorWidget
  7. {
  8. class Styles
  9. {
  10. public GUIStyle gridListStyle;
  11. public Styles()
  12. {
  13. gridListStyle = new GUIStyle("GridList");
  14. gridListStyle.alignment = GUI.skin.button.alignment;
  15. }
  16. }
  17. Sprite[] m_SpriteList = null;
  18. Texture2D[] m_SpritePreviews = null;
  19. List<int> m_SpritePreviewNeedFetching = new List<int>();
  20. Vector2 m_ScrollPos;
  21. Styles m_Style;
  22. const int kTargetPreviewSize = 64;
  23. public SpriteSelectorWidget()
  24. {}
  25. public void UpdateContents(Sprite[] sprites)
  26. {
  27. m_SpriteList = sprites;
  28. m_SpritePreviews = new Texture2D[sprites.Length];
  29. for (int i = 0; i < m_SpritePreviews.Length; ++i)
  30. m_SpritePreviewNeedFetching.Add(i);
  31. UpdateSpritePreviews();
  32. }
  33. public int ShowGUI(int selectedIndex)
  34. {
  35. if (m_Style == null)
  36. m_Style = new Styles();
  37. UpdateSpritePreviews();
  38. if (m_SpriteList == null || m_SpriteList.Length == 0)
  39. return selectedIndex;
  40. selectedIndex = (selectedIndex > m_SpriteList.Length) ? 0 : selectedIndex;
  41. using (var topRect = new EditorGUILayout.HorizontalScope())
  42. {
  43. //GUILayout.Label(Styles.spriteList, EditorStyles.label, new [] {GUILayout.Width(EditorGUIUtility.labelWidth - 5)});
  44. using (var selectionGridRect = new EditorGUILayout.HorizontalScope("box", new[] {GUILayout.ExpandWidth(true)}))
  45. {
  46. {
  47. float columnF;
  48. int columnCount, rowCount;
  49. GetRowColumnCount(EditorGUIUtility.currentViewWidth, kTargetPreviewSize, m_SpriteList.Length, out columnCount, out rowCount, out columnF);
  50. if (columnCount > 0 && rowCount > 0)
  51. {
  52. float contentSize = (columnF * kTargetPreviewSize) / columnCount;
  53. if (rowCount >= 2)
  54. m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos, GUIStyle.none, GUI.skin.verticalScrollbar, new[] { GUILayout.Height(rowCount > 1 ? contentSize * 2 : contentSize) });
  55. m_Style.gridListStyle.fixedWidth = contentSize;
  56. m_Style.gridListStyle.fixedHeight = contentSize;
  57. selectedIndex = ContentSelectionGrid(selectedIndex, m_SpriteList, m_Style.gridListStyle, columnCount - 1);
  58. if (rowCount >= 2)
  59. EditorGUILayout.EndScrollView();
  60. }
  61. }
  62. }
  63. }
  64. return selectedIndex;
  65. }
  66. static void GetRowColumnCount(float drawWidth, int size, int contentCount, out int column, out int row, out float columnf)
  67. {
  68. columnf = (drawWidth) / size;
  69. column = (int)columnf;
  70. if (column == 0)
  71. row = 0;
  72. else
  73. row = (int)Mathf.Ceil((contentCount + column - 1) / column);
  74. }
  75. int ContentSelectionGrid(int selected, Sprite[] contents, GUIStyle style, int columnCount)
  76. {
  77. if (contents != null && contents.Length != 0)
  78. {
  79. selected = GUILayout.SelectionGrid(selected, m_SpritePreviews, columnCount, style);
  80. }
  81. return selected;
  82. }
  83. public bool NeedUpdatePreview()
  84. {
  85. return m_SpritePreviewNeedFetching.Count > 0;
  86. }
  87. void UpdateSpritePreviews()
  88. {
  89. for (int i = 0; i < m_SpritePreviewNeedFetching.Count; ++i)
  90. {
  91. var index = m_SpritePreviewNeedFetching[i];
  92. if(m_SpriteList[index] == null)
  93. m_SpritePreviews[index] = EditorGUIUtility.Load("icons/console.erroricon.png") as Texture2D;
  94. else
  95. m_SpritePreviews[index] = AssetPreview.GetAssetPreview(m_SpriteList[index]);
  96. if (m_SpritePreviews[index] != null)
  97. {
  98. m_SpritePreviewNeedFetching.RemoveAt(i);
  99. --i;
  100. }
  101. }
  102. }
  103. }
  104. }