暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PrefabGeneration.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.AssetImporters;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. namespace UnityEditor.U2D.Aseprite
  7. {
  8. internal static class PrefabGeneration
  9. {
  10. public static void Generate(
  11. AssetImportContext ctx,
  12. TextureGenerationOutput output,
  13. List<Layer> layers,
  14. Dictionary<int, GameObject> layerIdToGameObject,
  15. Vector2Int canvasSize,
  16. AsepriteImporterSettings importSettings,
  17. ref UnityEngine.Object mainAsset,
  18. out GameObject rootGameObject)
  19. {
  20. rootGameObject = new GameObject("Root");
  21. #if ENABLE_URP
  22. if (importSettings.addShadowCasters && layers.Count > 1)
  23. rootGameObject.AddComponent<UnityEngine.Rendering.Universal.CompositeShadowCaster2D>();
  24. #endif
  25. if (importSettings.addSortingGroup && layers.Count > 1)
  26. rootGameObject.AddComponent<SortingGroup>();
  27. if (layers.Count == 1)
  28. {
  29. layerIdToGameObject.Add(layers[0].index, rootGameObject);
  30. }
  31. else
  32. CreateLayerHierarchy(layers, layerIdToGameObject, rootGameObject);
  33. for (var i = layers.Count - 1; i >= 0; --i)
  34. {
  35. var layer = layers[i];
  36. SetupLayerGameObject(layer, layerIdToGameObject, output.sprites, importSettings, canvasSize);
  37. if (layer.parentIndex == -1)
  38. continue;
  39. var parentGo = layerIdToGameObject[layer.parentIndex];
  40. layerIdToGameObject[layer.index].transform.parent = parentGo.transform;
  41. }
  42. // We need the GameObjects in order to generate Animation Clips.
  43. // But we will only save down the GameObjects if it is requested.
  44. if (importSettings.generateModelPrefab)
  45. {
  46. ctx.AddObjectToAsset(rootGameObject.name, rootGameObject);
  47. mainAsset = rootGameObject;
  48. }
  49. else
  50. rootGameObject.hideFlags = HideFlags.HideAndDontSave;
  51. }
  52. static void CreateLayerHierarchy(List<Layer> layers, Dictionary<int, GameObject> layerIdToGameObject, GameObject root)
  53. {
  54. for (var i = layers.Count - 1; i >= 0; --i)
  55. {
  56. var layer = layers[i];
  57. var go = new GameObject(layer.name);
  58. go.transform.parent = root.transform;
  59. go.transform.localRotation = Quaternion.identity;
  60. layerIdToGameObject.Add(layer.index, go);
  61. }
  62. }
  63. static void SetupLayerGameObject(
  64. Layer layer,
  65. Dictionary<int, GameObject> layerIdToGameObject,
  66. Sprite[] sprites,
  67. AsepriteImporterSettings importSettings,
  68. Vector2Int canvasSize)
  69. {
  70. if (layer.cells.Count == 0)
  71. return;
  72. var firstCell = layer.cells[0];
  73. var gameObject = layerIdToGameObject[layer.index];
  74. var sprite = Array.Find(sprites, x => x.GetSpriteID() == firstCell.spriteId);
  75. var sr = gameObject.AddComponent<SpriteRenderer>();
  76. sr.sprite = sprite;
  77. sr.sortingOrder = layer.index + firstCell.additiveSortOrder;
  78. #if ENABLE_URP
  79. if (importSettings.addShadowCasters)
  80. gameObject.AddComponent<UnityEngine.Rendering.Universal.ShadowCaster2D>();
  81. #endif
  82. if (importSettings.defaultPivotSpace == PivotSpaces.Canvas)
  83. gameObject.transform.localPosition = Vector3.zero;
  84. else
  85. {
  86. var cellRect = firstCell.cellRect;
  87. var position = new Vector3(cellRect.x, cellRect.y, 0f);
  88. var pivot = sprite.pivot;
  89. position.x += pivot.x;
  90. position.y += pivot.y;
  91. var globalPivot = ImportUtilities.PivotAlignmentToVector(importSettings.defaultPivotAlignment);
  92. position.x -= (canvasSize.x * globalPivot.x);
  93. position.y -= (canvasSize.y * globalPivot.y);
  94. position.x /= sprite.pixelsPerUnit;
  95. position.y /= sprite.pixelsPerUnit;
  96. gameObject.transform.localPosition = position;
  97. }
  98. }
  99. }
  100. }