PSDImportPostProcessor.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEditor.U2D;
  4. using UnityEditor.U2D.Sprites;
  5. using UnityEngine;
  6. namespace UnityEditor.U2D.PSD
  7. {
  8. internal class PSDImportPostProcessor : AssetPostprocessor
  9. {
  10. private static string s_CurrentApplyAssetPath = null;
  11. void OnPostprocessSprites(Texture2D texture, Sprite[] sprites)
  12. {
  13. var dataProviderFactories = new SpriteDataProviderFactories();
  14. dataProviderFactories.Init();
  15. PSDImporter psd = AssetImporter.GetAtPath(assetPath) as PSDImporter;
  16. if (psd == null)
  17. return;
  18. ISpriteEditorDataProvider importer = dataProviderFactories.GetSpriteEditorDataProviderFromObject(psd);
  19. if (importer != null)
  20. {
  21. importer.InitSpriteEditorDataProvider();
  22. var physicsOutlineDataProvider = importer.GetDataProvider<ISpritePhysicsOutlineDataProvider>();
  23. var textureDataProvider = importer.GetDataProvider<ITextureDataProvider>();
  24. int actualWidth = 0, actualHeight = 0;
  25. textureDataProvider.GetTextureActualWidthAndHeight(out actualWidth, out actualHeight);
  26. float definitionScaleW = (float)texture.width / actualWidth;
  27. float definitionScaleH = (float)texture.height / actualHeight;
  28. float definitionScale = Mathf.Min(definitionScaleW, definitionScaleH);
  29. foreach (var sprite in sprites)
  30. {
  31. var guid = sprite.GetSpriteID();
  32. var outline = physicsOutlineDataProvider.GetOutlines(guid);
  33. var outlineOffset = sprite.rect.size / 2;
  34. if (outline != null && outline.Count > 0)
  35. {
  36. // Ensure that outlines are all valid.
  37. int validOutlineCount = 0;
  38. for (int i = 0; i < outline.Count; ++i)
  39. validOutlineCount = validOutlineCount + ( (outline[i].Length > 2) ? 1 : 0 );
  40. int index = 0;
  41. var convertedOutline = new Vector2[validOutlineCount][];
  42. for (int i = 0; i < outline.Count; ++i)
  43. {
  44. if (outline[i].Length > 2)
  45. {
  46. convertedOutline[index] = new Vector2[outline[i].Length];
  47. for (int j = 0; j < outline[i].Length; ++j)
  48. {
  49. convertedOutline[index][j] = outline[i][j] * definitionScale + outlineOffset;
  50. }
  51. index++;
  52. }
  53. }
  54. sprite.OverridePhysicsShape(convertedOutline);
  55. }
  56. }
  57. }
  58. }
  59. public static string currentApplyAssetPath
  60. {
  61. set { s_CurrentApplyAssetPath = value; }
  62. }
  63. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPath)
  64. {
  65. if (!string.IsNullOrEmpty(s_CurrentApplyAssetPath))
  66. {
  67. foreach (var asset in importedAssets)
  68. {
  69. if (asset == s_CurrentApplyAssetPath)
  70. {
  71. var obj = AssetDatabase.LoadMainAssetAtPath(asset);
  72. Selection.activeObject = obj;
  73. Unsupported.SceneTrackerFlushDirty();
  74. s_CurrentApplyAssetPath = null;
  75. break;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }