Files
Studio/Assets/Scripts/Studio/Managers/FBXFileManager.cs
2025-06-11 14:26:20 +09:00

210 lines
7.4 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Events;
using System.Collections;
using TriLibCore.SFB;
using Studio.Util;
using System.Threading.Tasks;
using System;
using System.Linq;
using Studio.Repositories;
using Studio.AssetTool;
using Ookii.Dialogs;
using Studio.Command;
using Studio.Manage;
using Studio.AssetLibraryTree;
using Studio.Core;
using XRLib;
using UnityEngine.Profiling;
namespace Studio.AssetTool
{
public class FBXFileManager : Manager
{
public CustomAssetDataRepository dataRepo;
public string identifier;
public string password;
public UnityEvent<string> onRemoveFbxFile;
public SaveData saveData;
public SharedMaterial sharedMaterial;
private bool isSaveTaskComplete = true;
public bool isLoadTaskComplete = true;
public Transform fbxAnchor;
public Action onLoadComplete;
private HashSet<string> filePaths = new();
public Dictionary<string, List<string>> fileDatas = new();
public FBXFileManager()
{
dataRepo = new CustomAssetDataRepository();
saveData = new SaveData();
sharedMaterial = new SharedMaterial();
identifier = string.IsNullOrEmpty(identifier) ? "defaultAssetData" : identifier;
fbxAnchor = new GameObject("LocalAssetDataAnchor").transform;
}
public override void Init()
{
}
public void ClearFilePaths()
{
dataRepo.assetDatas.Clear();
filePaths.Clear();
}
public void LoadResource(GameObject[] assets, FileSizeListWrapper fileSizeListWrapper)
{
foreach (var asset in assets)
{
var assetName = asset.name;
if (dataRepo.TryGetDataFromName(assetName, out var p))
{
continue;
}
GameObject newObject = new GameObject(assetName);
newObject.transform.parent = fbxAnchor;
CustomAssetData assetData = newObject.AddComponent<CustomAssetData>();
assetData.assetName = assetName;
assetData.folderName = "Base Library";
var fileSzie = fileSizeListWrapper.files.Find(item => item.name == assetName);
assetData.assetSize = fileSzie.size;
assetData.isLoading = true;
assetData.isLoadComplete = true;
var obj= UnityEngine.GameObject.Instantiate(asset);
obj.name = assetName;
obj.gameObject.SetActive(false);
assetData.loadedObject = obj;
assetData.OnLoadComplete();
RuntimePreviewGenerator.OrthographicMode = true;
Texture2D thumbnail = RuntimePreviewGenerator.GenerateModelPreview(assetData.loadedObject.transform, 320, 320);
thumbnail = TextureUtil.MakeReadableTexture(thumbnail);
assetData.thumbnail = thumbnail;
OnLoadComplete(assetData);
}
OnDirectoryLoadComplete();
}
public void LoadLocalFBXDirectorys(List<string> folderPaths)
{
fileDatas.Clear();
foreach (var path in folderPaths)
{
LoadLocalFBXDirectory(path);
}
CoroutineRunner.instance.StartCoroutine(LoadAssetFiles(filePaths.ToList(), OnDirectoryLoadComplete));
}
public void LoadLocalFBXDirectory(string folderPath)
{
if (!Directory.Exists(folderPath))
{
return;
}
var files = Directory.GetFiles(folderPath, "*.fbx", SearchOption.AllDirectories);
List<string> fileNames = new List<string>();
foreach (var file in files)
{
var fileName = Path.GetFileNameWithoutExtension(file);
fileNames.Add(fileName);
if (filePaths.Contains(NormalizePath(file)))
{
continue;
}
filePaths.Add(NormalizePath(file));
}
}
string NormalizePath(string path)
{
return path.Replace("\\", "/"); // 또는 ToUpperInvariant(), OS에 따라
}
private void OnDirectoryLoadComplete()
{
foreach(var asset in dataRepo.assetDatas)
{
if (!fileDatas.ContainsKey(asset.folderName))
{
fileDatas[asset.folderName] = new List<string>();
}
fileDatas[asset.folderName].Add(asset.assetName);
}
onLoadComplete?.Invoke();
}
private IEnumerator LoadAssetFiles(List<string> filePaths, Action onComplete)
{
foreach (var file in filePaths)
{
var assetName = Path.GetFileNameWithoutExtension(file);
var directoryPath = Path.GetDirectoryName(file);
var folderName = Path.GetFileName(directoryPath);
if (dataRepo.TryGetDataFromName(assetName, out var p))
{
continue;
}
GameObject newObject = new GameObject(assetName);
newObject.transform.parent = fbxAnchor;
CustomAssetData assetData = newObject.AddComponent<CustomAssetData>();
assetData.assetName = assetName;
assetData.folderName = folderName;
assetData.LoadLocalFBX(file);
yield return new WaitUntil(() => (assetData.isLoadComplete && assetData.progress == 1) || assetData.isLoadError);
assetData.OnLoadComplete();
RuntimePreviewGenerator.OrthographicMode = true;
Texture2D thumbnail = RuntimePreviewGenerator.GenerateModelPreview(assetData.loadedObject.transform, 320, 320);
assetData.assetSize = new FileInfo(assetData.localFBXPath).Length;
thumbnail = TextureUtil.MakeReadableTexture(thumbnail);
assetData.thumbnail = thumbnail;
OnLoadComplete(assetData);
}
onComplete?.Invoke(); // 한 폴더 끝나면 알림
}
public void SaveToLocalData(List<CustomAssetData> assetDatas, string path = "")
{
if (isSaveTaskComplete == false)
{
return;
}
//StartCoroutine(CoroutineSaveToLocalData(assetDatas, path));
}
public CustomAssetData GetCustomAssetData(string name)
{
return dataRepo.FindFromName(name);
//return listAssets.FirstOrDefault(x => x.name == name);
}
void OnLoadComplete(CustomAssetData assetData)
{
string folderName = assetData.folderName;
dataRepo.Add(assetData);
//listAssets.Add(assetData);
var connector = ManagerHub.instance.Get<CustomAssetConnector>();
AssetLibraryItem urlSectionItem = connector.assetScrollView.FindItem(folderName, AssetLibraryItemType.folder) ??
connector.assetScrollView.AddItem(folderName, null, AssetLibraryItemType.folder);
AssetLibraryItem item = connector.assetScrollView.AddItem(assetData.assetName, assetData.gameObject, AssetLibraryItemType.file, urlSectionItem);
}
}
}