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

ImportMergedLayers.cs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using Unity.Collections;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. using UnityEngine;
  6. using UnityEngine.Assertions;
  7. namespace UnityEditor.U2D.Aseprite
  8. {
  9. internal static class ImportMergedLayers
  10. {
  11. public static void Import(string assetName, ref List<Layer> layers, out List<NativeArray<Color32>> cellBuffers, out List<int> cellWidth, out List<int> cellHeight)
  12. {
  13. var cellsPerFrame = GetAllCellsPerFrame(in layers);
  14. var mergedCells = MergeCells(in cellsPerFrame, assetName);
  15. cellBuffers = new List<NativeArray<Color32>>();
  16. cellWidth = new List<int>();
  17. cellHeight = new List<int>();
  18. for (var i = 0; i < mergedCells.Count; ++i)
  19. {
  20. var width = mergedCells[i].cellRect.width;
  21. var height = mergedCells[i].cellRect.height;
  22. if (width == 0 || height == 0)
  23. continue;
  24. cellBuffers.Add(mergedCells[i].image);
  25. cellWidth.Add(width);
  26. cellHeight.Add(height);
  27. }
  28. UpdateLayerList(mergedCells, assetName, ref layers);
  29. }
  30. static Dictionary<int, List<Cell>> GetAllCellsPerFrame(in List<Layer> layers)
  31. {
  32. var cellsPerFrame = new Dictionary<int, List<Cell>>();
  33. for (var i = 0; i < layers.Count; ++i)
  34. {
  35. var cells = layers[i].cells;
  36. for (var m = 0; m < cells.Count; ++m)
  37. {
  38. var cell = cells[m];
  39. var width = cell.cellRect.width;
  40. var height = cell.cellRect.height;
  41. if (width == 0 || height == 0)
  42. continue;
  43. if (cellsPerFrame.ContainsKey(cell.frameIndex))
  44. cellsPerFrame[cell.frameIndex].Add(cell);
  45. else
  46. cellsPerFrame.Add(cell.frameIndex, new List<Cell>() { cell });
  47. }
  48. var linkedCells = layers[i].linkedCells;
  49. for (var m = 0; m < linkedCells.Count; ++m)
  50. {
  51. var frameIndex = linkedCells[m].frameIndex;
  52. var linkedToFrame = linkedCells[m].linkedToFrame;
  53. var cellIndex = cells.FindIndex(x => x.frameIndex == linkedToFrame);
  54. Assert.AreNotEqual(-1, cellIndex, $"Linked Cell: {frameIndex} is linked to cell: {linkedToFrame}, which cannot be found.");
  55. var cell = cells[cellIndex];
  56. var width = cell.cellRect.width;
  57. var height = cell.cellRect.height;
  58. if (width == 0 || height == 0)
  59. continue;
  60. if (cellsPerFrame.ContainsKey(frameIndex))
  61. cellsPerFrame[frameIndex].Add(cell);
  62. else
  63. cellsPerFrame.Add(frameIndex, new List<Cell>() { cell });
  64. }
  65. }
  66. return cellsPerFrame;
  67. }
  68. static List<Cell> MergeCells(in Dictionary<int, List<Cell>> cellsPerFrame, string assetName)
  69. {
  70. var mergedCells = new List<Cell>(cellsPerFrame.Count);
  71. foreach (var (frameIndex, cells) in cellsPerFrame)
  72. {
  73. unsafe
  74. {
  75. var count = cells.Count;
  76. var textures = new NativeArray<IntPtr>(count, Allocator.Persistent);
  77. var cellRects = new NativeArray<RectInt>(count, Allocator.Persistent);
  78. var cellBlendModes = new NativeArray<BlendModes>(count, Allocator.Persistent);
  79. for (var i = 0; i < cells.Count; ++i)
  80. {
  81. textures[i] = (IntPtr)cells[i].image.GetUnsafePtr();
  82. cellRects[i] = cells[i].cellRect;
  83. cellBlendModes[i] = cells[i].blendMode;
  84. }
  85. TextureTasks.MergeTextures(in textures, in cellRects, in cellBlendModes, out var output);
  86. var mergedCell = new Cell()
  87. {
  88. cellRect = output.rect,
  89. image = output.image,
  90. frameIndex = frameIndex,
  91. name = ImportUtilities.GetCellName(assetName, frameIndex, cellsPerFrame.Count),
  92. spriteId = GUID.Generate()
  93. };
  94. mergedCells.Add(mergedCell);
  95. textures.Dispose();
  96. cellRects.Dispose();
  97. cellBlendModes.Dispose();
  98. }
  99. }
  100. return mergedCells;
  101. }
  102. static void UpdateLayerList(in List<Cell> cells, string assetName, ref List<Layer> layers)
  103. {
  104. layers.Clear();
  105. var flattenLayer = new Layer()
  106. {
  107. cells = cells,
  108. index = 0,
  109. name = assetName
  110. };
  111. flattenLayer.guid = Layer.GenerateGuid(flattenLayer);
  112. layers.Add(flattenLayer);
  113. }
  114. }
  115. }