using UnityEngine; using UnityEditor; using System.IO; using System.Collections.Generic; using System.Threading.Tasks; #if UNITY_EDITOR using UnityEditor.AddressableAssets.Settings; using UnityEditor.AddressableAssets; #endif namespace XED { public class TwinObjectPreprocessingHelper { static List twinContainerList = new(); [MenuItem("Tools/TwinObjectsSetting")] public static void TwinObjectsSetting() { AutomateTwinObjectSetup(); } //TODO : °æ·Î¸¦ ÇϵåÄÚµù ÇÏ´Â ¹æ½Ä ÀÌ¿ÜÀÇ ´Ù¸¥ ¹æ½Ä Ȱ¿ë, Prefab ÀÇ À̸§ ÆÄ½ÌÀ» ÀÌ¿ëÇÑ ¹æ½Ä Ȱ¿ë(PRF_Robot01_Robot) static void AutomateTwinObjectSetup() { var mainPath = "C:/Users/UVC_JYM/Desktop/Test"; var beforePath = string.Concat(mainPath, "TwinObject_BeforeProcessing"); var afterPath = string.Concat(mainPath, "TwinObject_AfterProcessing/"); string etcPath = string.Concat(mainPath, "$etc"); CreateAddressableAssets(beforePath, etcPath, afterPath); } static void CreateAddressableAssets(string beforePath, string etcPath, string afterPath) { //beforPath Æú´õ ¾ÈÀÇ Å¸ÀÔÀÌ ÇÁ¸®ÆÕÀÎ AssetÀÇ guid¸¦ ¸ðµÎ ã½À´Ï´Ù. string[] guids = AssetDatabase.FindAssets("t:Prefab", new string[] { beforePath }); foreach (var guid in guids) { //guid ¿¡ ÇØ´çÇÏ´Â Asset ÀÇ °æ·Î¸¦ ã½À´Ï´Ù. string filePath = AssetDatabase.GUIDToAssetPath(guid); if (filePath.StartsWith(etcPath)) { continue; } //°æ·Î¿¡ ÇØ´çÇÏ´Â Asset À» ã½À´Ï´Ù. var loadAsset = AssetDatabase.LoadAssetAtPath(filePath); var twinObject = loadAsset.GetComponent(); string fileName = Path.GetFileName(filePath); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath); string folderPath = string.Concat(afterPath, fileNameWithoutExtension); string fullPath = string.Concat(folderPath, "/", fileName); //TwinObject ¹Ì¸®º¸±â À̹ÌÁö¸¦ »ý¼ºÇÕ´Ï´Ù. var texture = CreateTwinObjectPortrait(folderPath, twinObject); } } static Texture2D CreateTwinObjectPortrait(string folderPath, TwinObject twinObject) { Texture2D thumbnailTexture = null; //¹Ì¸®º¸±â ÅØ½ºÃ³ »ý¼º while (thumbnailTexture == null) { thumbnailTexture = AssetPreview.GetAssetPreview(twinObject.gameObject); System.Threading.Thread.Sleep(5); } var transparentTexture = BackgroundTransparencyProcess(thumbnailTexture); //ÅØ½ºÃ³ ÀúÀå transparentTexture.name = string.Concat("Texture_", twinObject.name); byte[] bytes = transparentTexture.EncodeToPNG(); string filePath = string.Concat(folderPath, "/", transparentTexture.name, ".png"); File.WriteAllBytes(filePath, bytes); AssetDatabase.Refresh(); //ÅØ½ºÃ³ ºÒ·¯¿À±â var texture = AssetDatabase.LoadAssetAtPath(filePath); return texture; } static Texture2D BackgroundTransparencyProcess(Texture2D texture) { var transparentTexture = new Texture2D(texture.width, texture.height); transparentTexture.SetPixels(texture.GetPixels()); for (int h = 0; h < texture.height; h++) { for (int w = 0; w < texture.width; w++) { transparentTexture.SetPixel(w, h, Color.clear); if (texture.GetPixel(w, h) != texture.GetPixel(w + 1, h)) break; } for (int w = texture.width; w > 0; w--) { transparentTexture.SetPixel(w, h, Color.clear); if (texture.GetPixel(w, h) != texture.GetPixel(w - 1, h)) break; } } transparentTexture.Apply(); return transparentTexture; } } }