AssetPostProcessors Pivot 자동 변경 로직 추가

This commit is contained in:
NTG_Lenovo 2025-07-04 18:21:11 +09:00
parent e4f50e6da1
commit 3f9fd4227a
2 changed files with 90 additions and 8 deletions

View File

@ -1,6 +1,7 @@
#if UNITY_EDITOR #if UNITY_EDITOR
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
using UnityEngine.Networking;
namespace DDD namespace DDD
{ {
@ -16,15 +17,34 @@ private void OnPreprocessTexture()
// { // {
// AssetPostprocessorModel.OnPreprocessTexture(importer); // AssetPostprocessorModel.OnPreprocessTexture(importer);
// } // }
Debug.Log(upperPath);
if (upperPath.Contains("ASSETS/_DATAS/RAW/SPRITES/")) if (upperPath.Contains("ASSETS/_DATAS/RAW/SPRITES/"))
{ {
AssetPostprocessorSprite.OnPreprocessTexture(importer); AssetPostprocessorSprite.OnPreprocessTexture(importer);
} }
} }
public static void OnPostprocessAllAssets(string[] importedAssets, string[] deleteAssets, string[] movedAssets, string[] movedFromAssetPaths) public static void OnPostprocessAllAssets(string[] importedAssets, string[] deleteAssets, string[] movedAssets,
string[] movedFromAssetPaths)
{ {
for (int i = 0; i < movedAssets.Length; i++)
{
string fromPath = movedFromAssetPaths[i];
string toPath = movedAssets[i];
// 특정 폴더일 때만 작동
if (toPath.StartsWith("Assets/_Datas/Raw/Sprites/"))
{
if (AssetDatabase.LoadAssetAtPath<Sprite>(toPath) == null)
{
Debug.Log($"에셋 이동 감지: {fromPath} → {toPath}");
// 여기서 임포트 강제 재실행
AssetDatabase.ImportAsset(toPath, ImportAssetOptions.ForceUpdate);
}
}
}
foreach (var path in deleteAssets) foreach (var path in deleteAssets)
{ {
PostRemove(path); PostRemove(path);
@ -50,7 +70,7 @@ public static void OnPostprocessAllAssets(string[] importedAssets, string[] dele
AssetPostprocessorSprite.BuildTarget(); AssetPostprocessorSprite.BuildTarget();
AssetPostprocessorSprite.BuildTarget(); AssetPostprocessorSprite.BuildTarget();
} }
private static void PostRemove(string path, string movePath = "") private static void PostRemove(string path, string movePath = "")
{ {
try try

View File

@ -22,7 +22,7 @@ public static void OnPreprocessTexture(TextureImporter importer)
importer.spritePixelsPerUnit = width <= height ? width : height; importer.spritePixelsPerUnit = width <= height ? width : height;
importer.sRGBTexture = true; importer.sRGBTexture = true;
importer.isReadable = false; importer.isReadable = true;
importer.mipmapEnabled = false; importer.mipmapEnabled = false;
importer.streamingMipmaps = false; importer.streamingMipmaps = false;
importer.wrapMode = TextureWrapMode.Clamp; importer.wrapMode = TextureWrapMode.Clamp;
@ -36,6 +36,66 @@ public static void OnPreprocessTexture(TextureImporter importer)
textureSettings.spriteMeshType = SpriteMeshType.FullRect; textureSettings.spriteMeshType = SpriteMeshType.FullRect;
textureSettings.spriteExtrude = 2; textureSettings.spriteExtrude = 2;
importer.SetTextureSettings(textureSettings); importer.SetTextureSettings(textureSettings);
string path = importer.assetPath;
EditorApplication.delayCall += () => { TryApplyPivotAfterImport(path); };
}
private static void TryApplyPivotAfterImport(string path)
{
if (string.IsNullOrEmpty(path)) return;
// ✅ 무한 루프 방지 플래그 확인
string sessionKey = $"__SPRITE_PIVOT_SET__{path}";
if (SessionState.GetBool(sessionKey, false))
{
SessionState.EraseBool(sessionKey);
return; // 이미 한 번 처리한 경우 재진입 금지
}
var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
if (texture == null || !texture.isReadable) return;
int height = texture.height;
int width = texture.width;
int bottomY = height;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (texture.GetPixel(x, y).a > 0.01f)
{
bottomY = y;
goto FOUND_ALPHA;
}
}
}
FOUND_ALPHA:
if (bottomY == height)
{
Debug.LogWarning($"[SpritePivot] 모든 픽셀이 투명하여 pivot 설정 생략: {path}");
return;
}
float pivotY = (float)bottomY / height;
var importer = AssetImporter.GetAtPath(path) as TextureImporter;
if (importer == null) return;
var settings = new TextureImporterSettings();
importer.ReadTextureSettings(settings);
settings.spriteAlignment = (int)SpriteAlignment.Custom;
settings.spritePivot = new Vector2(0.5f, pivotY);
importer.SetTextureSettings(settings);
Debug.Log($"[SpritePivot] {path} → pivotY = {pivotY:F2}");
// ✅ 재임포트 플래그 설정 후 실행 (한 번만)
SessionState.SetBool(sessionKey, true);
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
} }
public static void OnRemove(string path, string movePath) public static void OnRemove(string path, string movePath)
@ -186,11 +246,11 @@ public static void CreatePrefab(string path, string destPath)
prefab.name = sprite.name; prefab.name = sprite.name;
GameObject visualLook = new GameObject("VisualLook"); GameObject visualLook = new GameObject("VisualLook");
visualLook.transform.SetParent(prefab.transform); visualLook.transform.SetParent(prefab.transform);
visualLook.transform.localPosition = Vector3.zero; visualLook.transform.localPosition = Vector3.zero;
visualLook.transform.localRotation = Quaternion.Euler(new Vector3(40f, 0f, 0f)); visualLook.transform.localRotation = Quaternion.Euler(new Vector3(40f, 0f, 0f));
SpriteRenderer spriteRenderer = visualLook.AddComponent<SpriteRenderer>(); SpriteRenderer spriteRenderer = visualLook.AddComponent<SpriteRenderer>();
spriteRenderer.sprite = sprite; spriteRenderer.sprite = sprite;
spriteRenderer.sortingOrder = 5; spriteRenderer.sortingOrder = 5;
@ -280,10 +340,12 @@ public static void BuildTarget()
{ {
foreach (var path in TargetPaths) foreach (var path in TargetPaths)
{ {
CreateAtlas(Utils.FolderPath(path), Utils.FolderPath(path).Replace("/Raw/", "/Addressables/") + ".spriteatlasv2"); CreateAtlas(Utils.FolderPath(path),
Utils.FolderPath(path).Replace("/Raw/", "/Addressables/") + ".spriteatlasv2");
CreatePrefab(path, (path.Replace("/Raw/Sprites/", "/Addressables/") + ".prefab").Replace(".png", "")); CreatePrefab(path, (path.Replace("/Raw/Sprites/", "/Addressables/") + ".prefab").Replace(".png", ""));
} }
TargetPaths.Clear(); TargetPaths.Clear();
} }
} }