mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-04-25 15:47:26 -05:00
UnityResourceDestroyer.cs
This commit is contained in:
parent
76176ac2ac
commit
553cd53719
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
27
Assets/UniGLTF/Runtime/UnityResourceDestroyer.cs
Normal file
27
Assets/UniGLTF/Runtime/UnityResourceDestroyer.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/UniGLTF/Runtime/UnityResourceDestroyer.cs.meta
Normal file
11
Assets/UniGLTF/Runtime/UnityResourceDestroyer.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ca87b3d02ce67634a9ebcfe8cca70ece
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user