mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-11 21:38:56 -05:00
TransferOwnership
This commit is contained in:
@@ -17,15 +17,42 @@ namespace UniGLTF
|
||||
public static void Import(ScriptedImporter scriptedImporter, AssetImportContext context, Axises reverseAxis)
|
||||
{
|
||||
#if VRM_DEVELOP
|
||||
Debug.Log("OnImportAsset to " + context.assetPath);
|
||||
Debug.Log("OnImportAsset to " + scriptedImporter.assetPath);
|
||||
#endif
|
||||
|
||||
var loaded = Load(context.assetPath,
|
||||
scriptedImporter.GetExternalObjectMap(),
|
||||
reverseAxis
|
||||
);
|
||||
//
|
||||
// Parse(parse glb, parser gltf json)
|
||||
//
|
||||
var parser = new GltfParser();
|
||||
parser.ParsePath(scriptedImporter.assetPath);
|
||||
|
||||
AddSubAssets(context, loaded);
|
||||
//
|
||||
// Import(create unity objects)
|
||||
//
|
||||
var externalObjectMap = scriptedImporter.GetExternalObjectMap();
|
||||
|
||||
using (var loaded = new ImporterContext(parser, null,
|
||||
externalObjectMap.Where(x => x.Value != null).Select(x => (x.Value.name, x.Value)).Concat(
|
||||
EnumerateExists(externalObjectMap, parser, UnityPath.FromUnityPath(scriptedImporter.assetPath).Parent))))
|
||||
{
|
||||
loaded.InvertAxis = reverseAxis;
|
||||
loaded.Load();
|
||||
loaded.ShowMeshes();
|
||||
|
||||
loaded.TransferOwnership(o =>
|
||||
{
|
||||
#if VRM_DEVELOP
|
||||
Debug.Log($"[{o.GetType().Name}] {o.name} will not destroy");
|
||||
#endif
|
||||
|
||||
context.AddObjectToAsset(o.name, o);
|
||||
if (o is GameObject)
|
||||
{
|
||||
// Root GameObject is main object
|
||||
context.SetMainObject(loaded.Root);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static IEnumerable<(string, UnityEngine.Object)> EnumerateExists(Dictionary<AssetImporter.SourceAssetIdentifier, UnityEngine.Object> exclude,
|
||||
@@ -58,83 +85,5 @@ namespace UniGLTF
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse して、UnityObject 化する。
|
||||
///
|
||||
/// TODO: すべての UnityObject を ImporterContext が所有する。(Disposeで削除できる)
|
||||
/// </summary>
|
||||
/// <param name="assetPath">glbのパス</param>
|
||||
/// <param name="externalObjectMap">ScriptedImporter外部に作成済みのAssetへの参照</param>
|
||||
/// <param name="reverseAxis">gltf から unityへの座標変換オプション</param>
|
||||
/// <returns></returns>
|
||||
static ImporterContext Load(string assetPath, Dictionary<AssetImporter.SourceAssetIdentifier, UnityEngine.Object> externalObjectMap, Axises reverseAxis)
|
||||
{
|
||||
//
|
||||
// Parse(parse glb, parser gltf json)
|
||||
//
|
||||
var parser = new GltfParser();
|
||||
parser.ParsePath(assetPath);
|
||||
|
||||
//
|
||||
// Import(create unity objects)
|
||||
//
|
||||
var context = new ImporterContext(parser, null,
|
||||
externalObjectMap.Where(x => x.Value != null).Select(x => (x.Value.name, x.Value)).Concat(
|
||||
EnumerateExists(externalObjectMap, parser, UnityPath.FromUnityPath(assetPath).Parent)));
|
||||
context.InvertAxis = reverseAxis;
|
||||
context.Load();
|
||||
context.ShowMeshes();
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UnityObjectをSubAsset化する。
|
||||
///
|
||||
/// TODO: SubAsset化されると、ImporterContext から所有権を除去する
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="loaded"></param>
|
||||
static void AddSubAssets(AssetImportContext context, ImporterContext loaded)
|
||||
{
|
||||
// Texture
|
||||
foreach (var info in loaded.TextureFactory.Textures)
|
||||
{
|
||||
if (info.IsSubAsset)
|
||||
{
|
||||
var texture = info.Texture;
|
||||
context.AddObjectToAsset(texture.name, texture);
|
||||
}
|
||||
}
|
||||
|
||||
// Material
|
||||
foreach (var info in loaded.MaterialFactory.Materials)
|
||||
{
|
||||
if (info.IsSubAsset)
|
||||
{
|
||||
var material = info.Asset;
|
||||
context.AddObjectToAsset(material.name, material);
|
||||
}
|
||||
}
|
||||
|
||||
// Mesh
|
||||
foreach (var mesh in loaded.Meshes.Select(x => x.Mesh))
|
||||
{
|
||||
// all mesh is subasset
|
||||
context.AddObjectToAsset(mesh.name, mesh);
|
||||
}
|
||||
|
||||
// Animation
|
||||
foreach (var clip in loaded.AnimationClips)
|
||||
{
|
||||
// all animation is subasset
|
||||
context.AddObjectToAsset(clip.name, clip);
|
||||
}
|
||||
|
||||
// Root GameObject is main object
|
||||
context.AddObjectToAsset(loaded.Root.name, loaded.Root);
|
||||
context.SetMainObject(loaded.Root);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,14 +235,17 @@ namespace UniGLTF
|
||||
destroy(x);
|
||||
}
|
||||
AnimationClips.Clear();
|
||||
|
||||
foreach (var x in Meshes)
|
||||
{
|
||||
destroy(x.Mesh);
|
||||
}
|
||||
Meshes.Clear();
|
||||
m_materialFactory.Dispose();
|
||||
m_textureFactory.Dispose();
|
||||
destroy(Root);
|
||||
|
||||
// // m_materialFactory.Dispose();
|
||||
// m_textureFactory.Dispose();
|
||||
|
||||
// destroy(Root);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -256,13 +259,21 @@ namespace UniGLTF
|
||||
add(mesh.Mesh);
|
||||
}
|
||||
Meshes.Clear();
|
||||
MaterialFactory.TransferOwnership(add);
|
||||
|
||||
TextureFactory.TransferOwnership(add);
|
||||
MaterialFactory.TransferOwnership(add);
|
||||
|
||||
foreach (var animation in AnimationClips)
|
||||
{
|
||||
add(animation);
|
||||
}
|
||||
AnimationClips.Clear();
|
||||
|
||||
if (Root != null)
|
||||
{
|
||||
add(Root);
|
||||
Root = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -75,19 +75,12 @@ namespace UniGLTF
|
||||
|
||||
List<MaterialLoadInfo> m_materials = new List<MaterialLoadInfo>();
|
||||
public IReadOnlyList<MaterialLoadInfo> Materials => m_materials;
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var x in ObjectsForSubAsset())
|
||||
{
|
||||
UnityEngine.Object.DestroyImmediate(x, true);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<UnityEngine.Object> ObjectsForSubAsset()
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var x in m_materials)
|
||||
{
|
||||
yield return x.Asset;
|
||||
UnityEngine.Object.DestroyImmediate(x.Asset, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,7 +200,10 @@ namespace UniGLTF
|
||||
{
|
||||
foreach (var x in m_materials)
|
||||
{
|
||||
add(x.Asset);
|
||||
if (!x.UseExternal)
|
||||
{
|
||||
add(x.Asset);
|
||||
}
|
||||
}
|
||||
m_materials.Clear();
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ namespace UniGLTF
|
||||
var keys = new List<string>();
|
||||
foreach (var x in m_textureCache)
|
||||
{
|
||||
if (x.Value.IsUsed)
|
||||
if (x.Value.IsUsed && !x.Value.IsExternal)
|
||||
{
|
||||
keys.Add(x.Key);
|
||||
add(x.Value.Texture);
|
||||
|
||||
Reference in New Issue
Block a user