SpritePolygonModeModule.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace UnityEditor.U2D.Sprites
  4. {
  5. [RequireSpriteDataProvider(typeof(ISpriteOutlineDataProvider), typeof(ITextureDataProvider))]
  6. internal partial class SpritePolygonModeModule : SpriteFrameModuleBase
  7. {
  8. List<List<Vector2[]>> m_Outline;
  9. public SpritePolygonModeModule(ISpriteEditor sw, IEventSystem es, IUndoSystem us, IAssetDatabase ad) :
  10. base("Sprite Polygon Mode Editor", sw, es, us, ad)
  11. {}
  12. // ISpriteEditorModule implemenation
  13. public override void OnModuleActivate()
  14. {
  15. base.OnModuleActivate();
  16. AddMainUI(spriteEditor.GetMainVisualContainer());
  17. m_Outline = new List<List<Vector2[]>>();
  18. for (int i = 0; i < m_RectsCache.spriteRects.Count; ++i)
  19. {
  20. var rect = m_RectsCache.spriteRects[i];
  21. m_Outline.Add(spriteEditor.GetDataProvider<ISpriteOutlineDataProvider>().GetOutlines(rect.spriteID));
  22. }
  23. showChangeShapeWindow = polygonSprite;
  24. if (polygonSprite)
  25. DeterminePolygonSides();
  26. }
  27. public override bool CanBeActivated()
  28. {
  29. return SpriteFrameModule.GetSpriteImportMode(spriteEditor.GetDataProvider<ISpriteEditorDataProvider>()) == SpriteImportMode.Polygon;
  30. }
  31. private bool polygonSprite
  32. {
  33. get { return spriteImportMode == SpriteImportMode.Polygon; }
  34. }
  35. private void DeterminePolygonSides()
  36. {
  37. if (polygonSprite && m_RectsCache.spriteRects.Count == 1 && m_Outline.Count == 1 && m_Outline[0].Count == 1)
  38. {
  39. polygonSides = m_Outline[0][0].Length;
  40. }
  41. else
  42. // If for reasons we cannot determine the sides of the polygon, fall back to 0 (Square)
  43. polygonSides = 0;
  44. ViewUpdateSideCountField();
  45. }
  46. public int GetPolygonSideCount()
  47. {
  48. DeterminePolygonSides();
  49. return polygonSides;
  50. }
  51. public int polygonSides
  52. {
  53. get;
  54. set;
  55. }
  56. public List<Vector2[]> GetSpriteOutlineAt(int i)
  57. {
  58. return m_Outline[i];
  59. }
  60. public void GeneratePolygonOutline()
  61. {
  62. for (int i = 0; i < m_RectsCache.spriteRects.Count; i++)
  63. {
  64. SpriteRect currentRect = m_RectsCache.spriteRects[i];
  65. var result = UnityEditor.Sprites.SpriteUtility.GeneratePolygonOutlineVerticesOfSize(polygonSides, (int)currentRect.rect.width, (int)currentRect.rect.height);
  66. m_Outline.Clear();
  67. var newOutlineList = new List<Vector2[]>();
  68. newOutlineList.Add(result);
  69. m_Outline.Add(newOutlineList);
  70. spriteEditor.SetDataModified();
  71. }
  72. Repaint();
  73. }
  74. public override bool ApplyRevert(bool apply)
  75. {
  76. var outlineProvider = spriteEditor.GetDataProvider<ISpriteOutlineDataProvider>();
  77. if (apply)
  78. {
  79. for (int i = 0; i < m_RectsCache.spriteRects.Count && i < m_Outline.Count; ++i)
  80. outlineProvider.SetOutlines(m_RectsCache.spriteRects[i].spriteID, m_Outline[i]);
  81. }
  82. else
  83. {
  84. m_Outline.Clear();
  85. for (int i = 0; i < m_RectsCache.spriteRects.Count; ++i)
  86. m_Outline.Add(outlineProvider.GetOutlines(m_RectsCache.spriteRects[i].spriteID));
  87. DeterminePolygonSides();
  88. ViewUpdateSideCountField();
  89. }
  90. return base.ApplyRevert(apply);
  91. }
  92. }
  93. }