Skip to content

Commit

Permalink
Merge pull request #1109 from Santarh/fixDestroyNulLAccess
Browse files Browse the repository at this point in the history
Add properties & Fix the bug in `RuntimeGltfInstance`
  • Loading branch information
ousttrue authored Jul 19, 2021
2 parents bbd738d + 2f970a2 commit 3579893
Showing 1 changed file with 108 additions and 12 deletions.
120 changes: 108 additions & 12 deletions Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,162 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using VRMShaders;

namespace UniGLTF
{
/// <summary>
/// ImporterContext の Load 結果の GltfModel
///
///
/// Runtime でモデルを Destory したときに関連リソース(Texture, Material...などの UnityEngine.Object)を自動的に Destroy する。
/// </summary>
public class RuntimeGltfInstance : MonoBehaviour, IResponsibilityForDestroyObjects
{
/// <summary>
/// this is UniGLTF root gameObject
/// </summary>
public GameObject Root => this.gameObject;
public GameObject Root => (this != null) ? this.gameObject : null;

List<(SubAssetKey, UnityEngine.Object)> m_resources = new List<(SubAssetKey, UnityEngine.Object)>();
/// <summary>
/// Transforms with gltf node index.
/// </summary>
public IReadOnlyList<Transform> Nodes => _nodes;

/// <summary>
/// Runtime resources.
/// ex. Material, Texture, AnimationClip, Mesh.
/// </summary>
public IReadOnlyList<(SubAssetKey, UnityEngine.Object)> RuntimeResources => _resources;

/// <summary>
/// Materials.
/// </summary>
public IReadOnlyList<Material> Materials => _materials;

/// <summary>
/// Textures.
/// </summary>
public IReadOnlyList<Texture> Textures => _textures;

/// <summary>
/// Animation Clips.
/// </summary>
public IReadOnlyList<AnimationClip> AnimationClips => _animationClips;

/// <summary>
/// Meshes.
/// </summary>
public IReadOnlyList<Mesh> Meshes => _meshes;

/// <summary>
/// Renderers.
/// ex. MeshRenderer, SkinnedMeshRenderer.
/// </summary>
public IReadOnlyList<Renderer> Renderers => _renderers;

/// <summary>
/// Mesh Renderers.
/// </summary>
public IReadOnlyList<MeshRenderer> MeshRenderers => _meshRenderers;

/// <summary>
/// Skinned Mesh Renderers.
/// </summary>
public IReadOnlyList<SkinnedMeshRenderer> SkinnedMeshRenderers => _skinnedMeshRenderers;

public IReadOnlyList<(SubAssetKey, UnityEngine.Object)> RuntimeResources => m_resources;
private readonly List<Transform> _nodes = new List<Transform>();
private readonly List<(SubAssetKey, UnityEngine.Object)> _resources = new List<(SubAssetKey, UnityEngine.Object)>();
private readonly List<Material> _materials = new List<Material>();
private readonly List<Texture> _textures = new List<Texture>();
private readonly List<AnimationClip> _animationClips = new List<AnimationClip>();
private readonly List<Mesh> _meshes = new List<Mesh>();
private readonly List<Renderer> _renderers = new List<Renderer>();
private readonly List<MeshRenderer> _meshRenderers = new List<MeshRenderer>();
private readonly List<SkinnedMeshRenderer> _skinnedMeshRenderers = new List<SkinnedMeshRenderer>();

public static RuntimeGltfInstance AttachTo(GameObject go, ImporterContext context)
{
var loaded = go.AddComponent<RuntimeGltfInstance>();

foreach (var node in context.Nodes)
{
// Maintain index order.
loaded._nodes.Add(node);
}

context.TransferOwnership((k, o) =>
{
loaded.m_resources.Add((k, o));
if (o == null) return;

loaded._resources.Add((k, o));


switch (o)
{
case Material material:
loaded._materials.Add(material);
break;
case Texture texture:
loaded._textures.Add(texture);
break;
case AnimationClip animationClip:
loaded._animationClips.Add(animationClip);
break;
case Mesh mesh:
loaded._meshes.Add(mesh);
break;
}
});

foreach (var renderer in go.GetComponentsInChildren<Renderer>())
{
loaded._renderers.Add(renderer);

switch (renderer)
{
case MeshRenderer meshRenderer:
loaded._meshRenderers.Add(meshRenderer);
break;
case SkinnedMeshRenderer skinnedMeshRenderer:
loaded._skinnedMeshRenderers.Add(skinnedMeshRenderer);
break;
}
}

return loaded;
}

public void ShowMeshes()
{
foreach (var r in GetComponentsInChildren<Renderer>())
foreach (var r in Renderers)
{
r.enabled = true;
}
}

public void EnableUpdateWhenOffscreen()
{
foreach (var smr in GetComponentsInChildren<SkinnedMeshRenderer>())
foreach (var skinnedMeshRenderer in SkinnedMeshRenderers)
{
smr.updateWhenOffscreen = true;
skinnedMeshRenderer.updateWhenOffscreen = true;
}
}

void OnDestroy()
{
Debug.Log("UnityResourceDestroyer.OnDestroy");
foreach (var (key, x) in m_resources)
foreach (var (_, obj) in _resources)
{
UnityObjectDestoyer.DestroyRuntimeOrEditor(x);
UnityObjectDestoyer.DestroyRuntimeOrEditor(obj);
}
}

public void TransferOwnership(TakeResponsibilityForDestroyObjectFunc take)
{
foreach (var (key, x) in m_resources.ToArray())
foreach (var (key, x) in _resources.ToArray())
{
take(key, x);
m_resources.Remove((key, x));
_resources.Remove((key, x));
}
}

Expand Down

1 comment on commit 3579893

@markjohnacaling
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Please sign in to comment.