Files
Studio/Assets/Scripts/XED/AssetTool/TwinObjectPreprocessingHelper.cs
geondo55 6c83d36452 백업
2025-03-11 09:44:32 +09:00

121 lines
4.2 KiB
C#

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<TwinContainer> 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<GameObject>(filePath);
var twinObject = loadAsset.GetComponent<TwinObject>();
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<Texture2D>(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;
}
}
}