Files
Studio/Assets/Editor/WI/TwinObjectPreprocessingHelper.cs
2025-03-24 20:03:17 +09:00

274 lines
9.7 KiB
C#

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using XRLib;
using XRLib.Util;
using System.Threading.Tasks;
#if UNITY_EDITOR
using UnityEditor.AddressableAssets.Settings;
using UnityEditor.AddressableAssets;
#endif
namespace XED
{
public class TwinObjectPreprocessingHelper
{
#if UNITY_EDITOR
static List<TwinContainer> twinContainerList = new();
//[MenuItem("Tools/TwinObjectsSetting")]
//public static void TwinObjectsSetting()
//{
// AutomateTwinObjectSetup();
//}
//존재하지 않은 폴더의 경우 해당 폴더를 생성해주는 메서드
static void CreateFolder(string folderPath)
{
if (Directory.Exists(folderPath))
return;
Directory.CreateDirectory(folderPath);
}
//TODO : 경로를 하드코딩 하는 방식 이외의 다른 방식 활용, Prefab 의 이름 파싱을 이용한 방식 활용(PRF_Robot01_Robot)
static void AutomateTwinObjectSetup()
{
var mainPath = "Assets/Models/";
var beforePath = string.Concat(mainPath, "TwinObject_BeforeProcessing");
var afterPath = string.Concat(mainPath, "TwinObject_AfterProcessing/");
string etcPath = string.Concat(mainPath, "$etc");
CreateFolder(etcPath);
CreateAddressableAssets(beforePath, etcPath, afterPath);
TwinContainerAddressableSetting(twinContainerList);
}
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);
//전처리 완료 폴더를 생성합니다.
CreateFolder(folderPath);
//TwinContainer 를 생성합니다.
var twinContainer = CreateTwinContainer(folderPath, twinObject);
//TwinObject 미리보기 이미지를 생성합니다.
var texture = CreateTwinObjectPortrait(folderPath, twinObject);
//TwinObject 의 모델링 위치, 콜라이더를 초기화 후 파일 위치를 변경합니다.
SetTwinObject(loadAsset);
var moveFilePath = MoveFiles(fileName, filePath, fullPath, etcPath);
AssetDatabase.Refresh();
//TwinContainer 에 데이터를 할당합니다.
TwinContainerSetData(twinContainer, texture, moveFilePath);
twinContainerList.Add(twinContainer);
}
}
static TwinContainer CreateTwinContainer(string folderPath, TwinObject twinObject)
{
var twinContainer = ScriptableObject.CreateInstance<TwinContainer>();
var path = string.Concat(folderPath, "/", twinObject.name, ".asset");
AssetDatabase.CreateAsset(twinContainer, path);
return twinContainer;
}
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;
}
//작업이 완료된 모델링을 전처리 완료 폴더로 이동시키는 메서드
static string MoveFiles(string fileName, string filePath, string fullPath, string etcPath)
{
if (File.Exists(fullPath) && (filePath != fullPath))
{
int filenum = 0;
string EtcFileName = string.Concat(etcPath, "/", fileName);
while (File.Exists(EtcFileName))
{
filenum++;
EtcFileName = string.Concat(etcPath, "/", filenum.ToString(), "_", fileName);
}
fullPath = EtcFileName;
}
File.Move(filePath, fullPath);
return fullPath;
}
static void TwinContainerSetData(TwinContainer twinContainer, Texture2D texture, string filePath)
{
var moveAsset = AssetDatabase.LoadAssetAtPath<GameObject>(filePath);
var moveTwinObject = moveAsset.GetComponent<TwinObject>();
twinContainer.twinObject = moveTwinObject;
twinContainer.twinObjectPortrait = texture;
}
#region TwinObject Modeling Collider
//TwinObject 의 Modeling 의 위치와 Collider 를 설정하는 메서드
static void SetTwinObject(GameObject twinObject)
{
var bounds = CalculateBounds(twinObject.transform);
SetModelPosition(twinObject.transform, bounds);
SetBoxCollider(twinObject.transform, bounds);
}
//TwinObject 의 Bounds 를 계산하는 메서드
static Bounds CalculateBounds(Transform twinObject)
{
var childRenderers = twinObject.GetComponentsInChildren<Renderer>();
var combinedBounds = childRenderers[0].bounds;
foreach (var childRenderer in childRenderers)
{
combinedBounds.Encapsulate(childRenderer.bounds);
}
return combinedBounds;
}
//TwinObject 의 콜라이더를 크기에 맞게 설정하는 메서드
static void SetBoxCollider(Transform twinObject, Bounds bounds)
{
var boxCollider = twinObject.GetOrAddComponent<BoxCollider>();
boxCollider.size = bounds.size;
boxCollider.center = Vector3.zero;
}
//TwinObject 의 모델링 객체의 위치를 설정하는 메서드
static void SetModelPosition(Transform twinObject, Bounds bounds)
{
var twinObjectChild = twinObject.GetChild(0);
twinObjectChild.position -= bounds.center;
}
#endregion
#region TwinContainer Addressable Asset
private static void TwinContainerAddressableSetting(List<TwinContainer> twinContainers)
{
foreach (var twinContainer in twinContainers)
{
AddressableSetting(twinContainer);
}
}
//TwinContainer 의 Addressable(Group, Label)을 설정하는 메서드
private static void AddressableSetting(TwinContainer twinContainer)
{
AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
var path = AssetDatabase.GetAssetPath(twinContainer);
var guid = AssetDatabase.AssetPathToGUID(path);
var entry = settings.FindAssetEntry(guid);
var label = twinContainer.twinObject.assetLabel.ToString();
var group = CreateOrFindGroup(settings, label);
if (entry == null)
{
entry = settings.CreateOrMoveEntry(guid, group);
}
else
{
entry.address = path;
}
settings.MoveEntry(entry, group);
entry.SetLabel(label, true);
}
//Addressable Group 을 찾고, 찾지 못했으면 해당 Group 을 생성하고 반환하는 메서드
static AddressableAssetGroup CreateOrFindGroup(AddressableAssetSettings settings, string groupName)
{
var group = settings.FindGroup(groupName);
if (group == null)
group = settings.CreateGroup(groupName, false, false, true, null);
return group;
}
#endregion
#endif
}
}