AssetPostProcessors Pivot 자동 변경 로직 추가
This commit is contained in:
parent
e4f50e6da1
commit
3f9fd4227a
@ -1,6 +1,7 @@
|
||||
#if UNITY_EDITOR
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace DDD
|
||||
{
|
||||
@ -16,15 +17,34 @@ private void OnPreprocessTexture()
|
||||
// {
|
||||
// AssetPostprocessorModel.OnPreprocessTexture(importer);
|
||||
// }
|
||||
|
||||
Debug.Log(upperPath);
|
||||
if (upperPath.Contains("ASSETS/_DATAS/RAW/SPRITES/"))
|
||||
{
|
||||
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)
|
||||
{
|
||||
PostRemove(path);
|
||||
|
@ -22,7 +22,7 @@ public static void OnPreprocessTexture(TextureImporter importer)
|
||||
importer.spritePixelsPerUnit = width <= height ? width : height;
|
||||
|
||||
importer.sRGBTexture = true;
|
||||
importer.isReadable = false;
|
||||
importer.isReadable = true;
|
||||
importer.mipmapEnabled = false;
|
||||
importer.streamingMipmaps = false;
|
||||
importer.wrapMode = TextureWrapMode.Clamp;
|
||||
@ -36,6 +36,66 @@ public static void OnPreprocessTexture(TextureImporter importer)
|
||||
textureSettings.spriteMeshType = SpriteMeshType.FullRect;
|
||||
textureSettings.spriteExtrude = 2;
|
||||
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)
|
||||
@ -280,10 +340,12 @@ public static void BuildTarget()
|
||||
{
|
||||
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", ""));
|
||||
}
|
||||
|
||||
|
||||
TargetPaths.Clear();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user