From 1157dc0931dd863a6add8e95059ed6961a84a48c Mon Sep 17 00:00:00 2001 From: NTG_Lenovo Date: Fri, 1 Aug 2025 17:30:36 +0900 Subject: [PATCH] =?UTF-8?q?prop=20material,=20prefab=20=EC=9E=90=EB=8F=99?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AssetPostProcessors.cs | 13 +- .../AssetPostprocessorEnvironment.cs | 188 ++++++++++++++++++ Assets/_DDD/_Scripts/Utilities/Constants.cs | 5 + 3 files changed, 202 insertions(+), 4 deletions(-) create mode 100644 Assets/_DDD/_Scripts/AssetPostprocessors/AssetPostprocessorEnvironment.cs diff --git a/Assets/_DDD/_Scripts/AssetPostprocessors/AssetPostProcessors.cs b/Assets/_DDD/_Scripts/AssetPostprocessors/AssetPostProcessors.cs index 19c7b32e5..50708eb24 100644 --- a/Assets/_DDD/_Scripts/AssetPostprocessors/AssetPostProcessors.cs +++ b/Assets/_DDD/_Scripts/AssetPostprocessors/AssetPostProcessors.cs @@ -1,7 +1,6 @@ #if UNITY_EDITOR using UnityEngine; using UnityEditor; -using UnityEngine.Networking; namespace DDD { @@ -12,8 +11,8 @@ private void OnPreprocessTexture() var importer = assetImporter as TextureImporter; var upperPath = importer.assetPath.ToUpper(); - - if (upperPath.Contains(PathConstants.RawUiPathUpper)) + + if (upperPath.Contains(PathConstants.RawSpritesPathUpper)) { if (upperPath.Contains(PathConstants.RawOnlyAtlasPathUpper)) { @@ -28,6 +27,10 @@ private void OnPreprocessTexture() AssetPostprocessorSprite.OnPreprocessTextureForUi(importer); } } + else if (upperPath.Contains(PathConstants.RawEnvironmentsPathUpper)) + { + AssetPostprocessorEnvironment.OnPreprocessTexture(importer); + } } public static void OnPostprocessAllAssets(string[] importedAssets, string[] deleteAssets, string[] movedAssets, @@ -75,7 +78,7 @@ public static void OnPostprocessAllAssets(string[] importedAssets, string[] dele } AssetPostprocessorSprite.BuildTarget(); - AssetPostprocessorSprite.BuildTarget(); + AssetPostprocessorEnvironment.BuildTarget(); } private static void PostRemove(string path, string movePath = "") @@ -83,6 +86,7 @@ private static void PostRemove(string path, string movePath = "") try { AssetPostprocessorSprite.OnRemove(path, movePath); + AssetPostprocessorEnvironment.OnRemove(path); } catch (System.Exception e) { @@ -95,6 +99,7 @@ private static void PostAdd(string path) try { AssetPostprocessorSprite.OnAdd(path); + AssetPostprocessorEnvironment.OnAdd(path); } catch (System.Exception e) { diff --git a/Assets/_DDD/_Scripts/AssetPostprocessors/AssetPostprocessorEnvironment.cs b/Assets/_DDD/_Scripts/AssetPostprocessors/AssetPostprocessorEnvironment.cs new file mode 100644 index 000000000..dd3ff6bbb --- /dev/null +++ b/Assets/_DDD/_Scripts/AssetPostprocessors/AssetPostprocessorEnvironment.cs @@ -0,0 +1,188 @@ +#if UNITY_EDITOR +using System.Collections.Generic; +using System.IO; +using UnityEditor; +using UnityEditor.AddressableAssets; +using UnityEditor.AddressableAssets.Settings; +using UnityEngine; + +namespace DDD +{ + public static class AssetPostprocessorEnvironment + { + private static readonly HashSet SpriteTargetPaths = new(); + private static readonly HashSet MeshTargetPaths = new(); + + private static readonly int BaseMap = Shader.PropertyToID("_BaseMap"); + private static readonly int MetallicGlossMap = Shader.PropertyToID("_MetallicGlossMap"); + private static readonly int BumpMap = Shader.PropertyToID("_BumpMap"); + private static readonly int EmissionMap = Shader.PropertyToID("_EmissionMap"); + + private const string BaseMeshPrefabPath = "Assets/_DDD/_Raw/Environments/Env_Mesh_Prop.prefab"; + private const string BaseSpritePrefabPath = "Assets/_DDD/_Raw/Environments/Env_Sprite_Background.prefab"; + private const string ShaderName = "Universal Render Pipeline/LitEnvironment"; + private const string Prop = "Prop_"; + private const string BaseColorUpper = "_BASECOLOR"; + private const string MohsUpper = "_MOHS"; + private const string NormalUpper = "_NORMAL"; + private const string EmissionUpper = "_EMISSION"; + + public static void OnPreprocessTexture(TextureImporter importer) + { + var path = importer.assetPath; + string fileNameUpper = Utils.FileName(path).ToUpper(); + + if (fileNameUpper.Contains(NormalUpper)) + { + importer.textureType = TextureImporterType.NormalMap; + } + else + { + importer.textureType = TextureImporterType.Default; + importer.sRGBTexture = true; + } + + importer.mipmapEnabled = true; + } + + public static void OnAdd(string path) + { + string upperPath = path.ToUpper(); + + if (upperPath.Contains(PathConstants.RawEnvSpritesPathUpper) && + upperPath.Contains(ExtenstionConstants.PngExtensionUpper)) + { + if (!SpriteTargetPaths.Contains(path)) + SpriteTargetPaths.Add(path); + } + else if (upperPath.Contains(PathConstants.RawEnvMeshesPathUpper) && + upperPath.Contains(ExtenstionConstants.PngExtensionUpper)) + { + if (!MeshTargetPaths.Contains(path)) + MeshTargetPaths.Add(path); + } + } + + public static void OnRemove(string path, string movePath = "") + { + string upperPath = path.ToUpper(); + + if (upperPath.Contains(PathConstants.RawEnvSpritesPathUpper) && + upperPath.Contains(ExtenstionConstants.PngExtensionUpper)) + { + if (!SpriteTargetPaths.Contains(path)) + SpriteTargetPaths.Add(path); + } + else if (upperPath.Contains(PathConstants.RawEnvMeshesPathUpper) && + upperPath.Contains(ExtenstionConstants.PngExtensionUpper)) + { + if (!MeshTargetPaths.Contains(path)) + MeshTargetPaths.Add(path); + } + } + + public static void BuildMaterialAndPrefab(string path, bool isMesh) + { + var di = new DirectoryInfo(path); + if (!di.Exists) return; + + string folderName = di.Name; + string rawRoot = PathConstants.RawFolderPath; // "/_Raw" + string addrRoot = PathConstants.AddressablesFolderPath; // "/_Addressables" + + string destDir = path.Replace(rawRoot, addrRoot); + string materialPath = $"{destDir}/{folderName}{ExtenstionConstants.MaterialExtenstionLower}"; + string prefabPath = $"{destDir}/{Prop}{folderName}{ExtenstionConstants.PrefabExtenstionLower}"; + + Utils.MakeFolderFromFilePath(materialPath); + + // 머티리얼 생성 또는 로드 + Material mat = AssetDatabase.LoadAssetAtPath(materialPath); + if (mat == null) + { + mat = new Material(Shader.Find(ShaderName)); + AssetDatabase.CreateAsset(mat, materialPath); + } + + // PNG 텍스처 매핑 + var files = Directory.GetFiles(path, $"*{ExtenstionConstants.PngExtensionLower}", SearchOption.TopDirectoryOnly); + foreach (var file in files) + { + string texName = Path.GetFileNameWithoutExtension(file).ToUpper(); + var tex = AssetDatabase.LoadAssetAtPath(file); + if (tex == null) continue; + + if (texName.Contains(BaseColorUpper)) + { + mat.SetTexture(BaseMap, tex); + } + else if (texName.Contains(MohsUpper)) + { + mat.SetTexture(MetallicGlossMap, tex); + } + else if (texName.Contains(NormalUpper)) + { + mat.SetTexture(BumpMap, tex); + } + else if (texName.Contains(EmissionUpper)) + { + mat.SetTexture(EmissionMap, tex); + } + } + + AssetDatabase.ImportAsset(materialPath, ImportAssetOptions.ForceUpdate); + AssetDatabase.SaveAssets(); + + CreateOrUpdatePrefabVariant(folderName, mat, prefabPath, isMesh); + } + + private static void CreateOrUpdatePrefabVariant(string folderName, Material mat, string prefabPath, bool isMesh) + { + if (AssetDatabase.LoadAssetAtPath(prefabPath) != null) return; + + string basePrefabPath = isMesh ? BaseMeshPrefabPath : BaseSpritePrefabPath; + + var basePrefab = AssetDatabase.LoadAssetAtPath(basePrefabPath); + if (basePrefab == null) + { + Debug.LogWarning($"Base prefab not found: {basePrefabPath}"); + return; + } + + GameObject instancePrefab = AssetDatabase.LoadAssetAtPath(prefabPath); + if (instancePrefab == null) + { + instancePrefab = (GameObject)PrefabUtility.InstantiatePrefab(basePrefab); + instancePrefab.name = $"{Prop}{folderName}"; + } + + var renderer = instancePrefab.GetComponentInChildren(); + if (renderer != null) renderer.sharedMaterial = mat; + + Utils.MakeFolderFromFilePath(prefabPath); + PrefabUtility.SaveAsPrefabAssetAndConnect(instancePrefab, prefabPath, InteractionMode.AutomatedAction); + Object.DestroyImmediate(instancePrefab); + + AssetDatabase.ImportAsset(prefabPath, ImportAssetOptions.ForceUpdate); + AssetDatabase.SaveAssets(); + AssetDatabase.Refresh(); + } + + public static void BuildTarget() + { + foreach (var path in SpriteTargetPaths) + { + BuildMaterialAndPrefab(Utils.FolderPath(path), isMesh: false); + } + + foreach (var path in MeshTargetPaths) + { + BuildMaterialAndPrefab(Utils.FolderPath(path), isMesh: true); + } + + SpriteTargetPaths.Clear(); + MeshTargetPaths.Clear(); + } + } +} +#endif \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/Utilities/Constants.cs b/Assets/_DDD/_Scripts/Utilities/Constants.cs index 8de426db0..82732c5ec 100644 --- a/Assets/_DDD/_Scripts/Utilities/Constants.cs +++ b/Assets/_DDD/_Scripts/Utilities/Constants.cs @@ -42,6 +42,9 @@ public static class PathConstants public const string RawOnlyAtlasPathUpper = "ASSETS/_DDD/_RAW/SPRITES/ONLYATLAS/"; public const string RawFolderPath = "/_Raw"; public const string AddressablesFolderPath = "/_Addressables"; + public const string RawEnvironmentsPathUpper = "ASSETS/_DDD/_RAW/ENVIRONMENTS/"; + public const string RawEnvSpritesPathUpper = "ASSETS/_DDD/_RAW/ENVIRONMENTS/SPRITES/"; + public const string RawEnvMeshesPathUpper = "ASSETS/_DDD/_RAW/ENVIRONMENTS/MESHES/"; } public static class ExtenstionConstants @@ -49,6 +52,8 @@ public static class ExtenstionConstants public const string PngExtensionUpper = ".PNG"; public const string PngExtensionLower = ".png"; public const string SpriteAtlasExtenstionLower = ".spriteatlasv2"; + public const string MaterialExtenstionLower = ".mat"; + public const string PrefabExtenstionLower = ".prefab"; } public static class SpriteConstants