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

PaletteChunk.cs 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections.ObjectModel;
  2. using System.IO;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Aseprite
  5. {
  6. /// <summary>
  7. /// Structure for an entry in the color palette.
  8. /// </summary>
  9. public struct PaletteEntry
  10. {
  11. internal PaletteEntry(string name, Color32 color)
  12. {
  13. this.name = name;
  14. this.color = color;
  15. }
  16. /// <summary>
  17. /// Name of the color.
  18. /// </summary>
  19. public string name { get; private set; }
  20. /// <summary>
  21. /// Color value.
  22. /// </summary>
  23. public Color32 color { get; private set; }
  24. }
  25. /// <summary>
  26. /// Parsed representation of an Aseprite Palette chunk.
  27. /// </summary>
  28. public class PaletteChunk : BaseChunk, IPaletteProvider
  29. {
  30. public override ChunkTypes chunkType => ChunkTypes.Palette;
  31. /// <summary>
  32. /// Number of entries in the palette.
  33. /// </summary>
  34. public uint noOfEntries { get; private set; }
  35. /// <summary>
  36. /// Index of the first color to change.
  37. /// </summary>
  38. public uint firstColorIndex { get; private set; }
  39. /// <summary>
  40. /// Index of the last color to change.
  41. /// </summary>
  42. public uint lastColorIndex { get; private set; }
  43. /// <summary>
  44. /// Array of palette entries.
  45. /// </summary>
  46. public ReadOnlyCollection<PaletteEntry> entries => System.Array.AsReadOnly(m_Entries);
  47. PaletteEntry[] m_Entries;
  48. internal PaletteChunk(uint chunkSize) : base(chunkSize) { }
  49. protected override void InternalRead(BinaryReader reader)
  50. {
  51. noOfEntries = reader.ReadUInt32();
  52. firstColorIndex = reader.ReadUInt32();
  53. lastColorIndex = reader.ReadUInt32();
  54. // Reserved bytes
  55. for (var i = 0; i < 8; ++i)
  56. reader.ReadByte();
  57. m_Entries = new PaletteEntry[noOfEntries];
  58. for (var i = 0; i < noOfEntries; ++i)
  59. {
  60. var entryFlag = reader.ReadUInt16();
  61. var red = reader.ReadByte();
  62. var green = reader.ReadByte();
  63. var blue = reader.ReadByte();
  64. var alpha = reader.ReadByte();
  65. var color = new Color32(red, green, blue, alpha);
  66. var name = "";
  67. var hasName = entryFlag == 1;
  68. if (hasName)
  69. name = AsepriteUtilities.ReadString(reader);
  70. m_Entries[i] = new PaletteEntry(name, color);
  71. }
  72. }
  73. }
  74. }