Created
July 17, 2022 05:26
-
-
Save gtk2k/1658aee02dda23d756770c6014de19d4 to your computer and use it in GitHub Desktop.
Runtime Editor で Trilib をつかってモデルをインポート
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using Battlehub.RTEditor; | |
using Battlehub.RTCommon; | |
using System.Threading.Tasks; | |
using System.IO; | |
using Battlehub.RTSL.Interface; | |
using System; | |
using UnityObject = UnityEngine.Object; | |
using System.Threading; | |
using TriLibCore; | |
namespace Battlehub.RTImporter | |
{ | |
public class TriLibImporterAsync : ProjectFileImporterAsync | |
{ | |
public override int Priority | |
{ | |
get { return int.MinValue; } | |
} | |
public override string FileExt | |
{ | |
get { return ".fbx"; } | |
} | |
public override string IconPath | |
{ | |
get { return "Importers/GLTF"; } | |
} | |
public override Type TargetType | |
{ | |
get { return typeof(GameObject); } | |
} | |
private AssetLoaderOptions CreateTrilibAssetLoaderOptions() | |
{ | |
AssetLoaderOptions assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(); | |
assetLoaderOptions.AddAssetUnloader = false; | |
assetLoaderOptions.ImportMaterials = true; | |
//assetLoaderOptions.AutomaticallyPlayLegacyAnimations = false; | |
assetLoaderOptions.ImportTextures = true; | |
assetLoaderOptions.ImportColors = true; | |
assetLoaderOptions.UseFileScale = true; | |
assetLoaderOptions.OptimizeMeshes = false; | |
assetLoaderOptions.SortHierarchyByName = false; | |
assetLoaderOptions.ReadAndWriteEnabled = true; | |
assetLoaderOptions.ForceGCCollectionWhileLoading = false; | |
assetLoaderOptions.ScaleFactor = 1f; | |
assetLoaderOptions.AlphaMaterialMode = TriLibCore.General.AlphaMaterialMode.Transparent; | |
return assetLoaderOptions; | |
} | |
private Task<GameObject> BuildFromTrilib(string filePath) | |
{ | |
TaskCompletionSource<GameObject> tcs = new TaskCompletionSource<GameObject>(); | |
AssetLoaderOptions assetLoaderOptions = CreateTrilibAssetLoaderOptions(); | |
assetLoaderOptions.Timeout = 1800; // 30 minutes | |
GameObject go = null; | |
AssetLoader.LoadModelFromFile(filePath, ctx => | |
{ | |
go = ctx.RootGameObject; | |
go.SetActive(false); | |
}, | |
ctx => | |
{ | |
tcs.SetResult(go); | |
}, | |
(ctx, progress) => { }, | |
err => | |
{ | |
tcs.SetException(err.GetInnerException()); | |
}, | |
null, assetLoaderOptions, null); | |
return tcs.Task; | |
} | |
public override async Task ImportAsync(string filePath, string targetPath, IProjectAsync project, CancellationToken cancelToken) | |
{ | |
GameObject go = null; | |
try | |
{ | |
go = await BuildFromTrilib(filePath); | |
IRuntimeEditor editor = IOC.Resolve<IRuntimeEditor>(); | |
ProjectItem folder = project.Utils.GetFolder(Path.GetDirectoryName(targetPath)); | |
Transform[] children = go.GetComponentsInChildren<Transform>(true); | |
for (int i = 0; i < children.Length; ++i) | |
{ | |
children[i].gameObject.AddComponent<ExposeToEditor>(); | |
if(i % 100 == 0) | |
{ | |
await Task.Yield(); | |
} | |
} | |
await editor.CreatePrefabAsync(folder, go.GetComponent<ExposeToEditor>(), true); | |
} | |
catch(Exception e) | |
{ | |
throw new FileImporterException(e.Message, e); | |
} | |
finally | |
{ | |
if (go != null) | |
{ | |
UnityObject.Destroy(go); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment