UnityResourceDestroyer.cs

This commit is contained in:
ousttrue 2021-02-26 18:10:21 +09:00
parent 76176ac2ac
commit 553cd53719
4 changed files with 86 additions and 52 deletions

View File

@ -429,57 +429,6 @@ namespace UniGLTF
#endregion
#endif
/// <summary>
/// This function is used for clean up after create assets.
/// </summary>
/// <param name="destroySubAssets">Ambiguous arguments</param>
[Obsolete("Use Dispose for runtime loader resource management")]
public void Destroy(bool destroySubAssets)
{
if (Root != null) GameObject.DestroyImmediate(Root);
if (destroySubAssets)
{
#if UNITY_EDITOR
foreach (var o in ObjectsForSubAsset())
{
UnityEngine.Object.DestroyImmediate(o, true);
}
#endif
}
}
public void Dispose()
{
DestroyRootAndResources();
}
/// <summary>
/// Destroy resources that created ImporterContext for runtime load.
/// </summary>
public void DestroyRootAndResources()
{
if (!Application.isPlaying)
{
Debug.LogWarningFormat("Dispose called in editor mode. This function is for runtime");
}
// Remove hierarchy
if (Root != null) GameObject.Destroy(Root);
// Remove resources. materials, textures meshes etc...
foreach (var x in Meshes)
{
UnityEngine.Object.DestroyImmediate(x.Mesh, true);
}
foreach (var x in AnimationClips)
{
UnityEngine.Object.DestroyImmediate(x, true);
}
MaterialFactory.Dispose();
TextureFactory.Dispose();
}
#if UNITY_EDITOR
/// <summary>
/// Destroy the GameObject that became the basis of Prefab
/// </summary>
@ -502,6 +451,50 @@ namespace UniGLTF
UnityEngine.Object.DestroyImmediate(o, true);
}
}
#endif
/// <summary>
/// Importに使った一時オブジェクトを破棄する
///
/// 変換のあるテクスチャで、変換前のもの
/// normal, occlusion, metallicRoughness
/// </summary>
public void Dispose()
{
// m_textureFactory.Dispose();
}
/// <summary>
/// Root ヒエラルキーで使っているリソース
/// </summary>
/// <returns></returns>
public virtual IEnumerable<UnityEngine.Object> ResourcesInRootHierarchy()
{
foreach (var mesh in Meshes)
{
yield return mesh.Mesh;
}
foreach (var material in m_materialFactory.Materials)
{
yield return material.Asset;
}
foreach (var texture in m_textureFactory.Textures)
{
yield return texture.Texture;
}
foreach (var animation in AnimationClips)
{
yield return animation;
}
}
public UnityResourceDestroyer DisposeOnGameObjectDestroyed()
{
var destroyer = Root.AddComponent<UnityResourceDestroyer>();
foreach (var x in ResourcesInRootHierarchy())
{
destroyer.Resources.Add(x);
}
return destroyer;
}
}
}

View File

@ -0,0 +1,27 @@
using System.Collections.Generic;
using UnityEngine;
namespace UniGLTF
{
/// <summary>
/// Mesh, Material, Texture などを抱えておいて確実に破棄できるようにする
/// </summary>
public class UnityResourceDestroyer : MonoBehaviour
{
List<UnityEngine.Object> m_resources = new List<Object>();
public IList<UnityEngine.Object> Resources => m_resources;
void OnDestroy()
{
Debug.Log("UnityResourceDestroyer.OnDestroy");
foreach (var x in Resources)
{
#if VRM_DEVELOP
Debug.Log($"Destroy: {x}");
#endif
Destroy(x);
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ca87b3d02ce67634a9ebcfe8cca70ece
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -318,6 +318,7 @@ namespace VRM.Samples
await m_texts.UpdateMetaAsync(context);
await context.LoadAsync();
context.DisposeOnGameObjectDestroyed();
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();
context.ShowMeshes();
@ -333,6 +334,7 @@ namespace VRM.Samples
var context = new UniGLTF.ImporterContext(parser);
context.Load();
context.DisposeOnGameObjectDestroyed();
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();
context.ShowMeshes();
@ -348,6 +350,7 @@ namespace VRM.Samples
var context = new UniGLTF.ImporterContext(parser);
context.Load();
context.DisposeOnGameObjectDestroyed();
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();
context.ShowMeshes();