// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using UnityEngine; namespace Microsoft.MixedReality.Toolkit.SpatialAwareness { /// /// Object encapsulating the components of a spatial awareness mesh object. /// public class SpatialAwarenessMeshObject : BaseSpatialAwarenessObject { /// /// When a mesh is created we will need to create a game object with a minimum /// set of components to contain the mesh. These are the required component types. /// private static readonly Type[] RequiredMeshComponents = { typeof(MeshFilter), typeof(MeshRenderer), typeof(MeshCollider) }; /// /// The collider for the mesh object. /// public MeshCollider Collider { get; set; } /// /// Constructor. /// private SpatialAwarenessMeshObject() : base() { } /// /// Creates a . /// /// /// SpatialMeshObject containing the fields that describe the mesh. /// public static SpatialAwarenessMeshObject Create( Mesh mesh, int layer, string name, int meshId, GameObject meshParent = null) { SpatialAwarenessMeshObject newMesh = new SpatialAwarenessMeshObject { Id = meshId, GameObject = new GameObject(name, RequiredMeshComponents) { layer = layer } }; /// Preserve local transform when attaching to parent. newMesh.GameObject.transform.SetParent(meshParent != null ? meshParent.transform : null, false); newMesh.Filter = newMesh.GameObject.GetComponent(); newMesh.Filter.sharedMesh = mesh; newMesh.Renderer = newMesh.GameObject.GetComponent(); // Reset the surface mesh collider to fit the updated mesh. // Unity tribal knowledge indicates that to change the mesh assigned to a // mesh collider, the mesh must first be set to null. Presumably there // is a side effect in the setter when setting the shared mesh to null. newMesh.Collider = newMesh.GameObject.GetComponent(); newMesh.Collider.sharedMesh = null; newMesh.Collider.sharedMesh = newMesh.Filter.sharedMesh; return newMesh; } /// /// Clean up the resources associated with the surface. /// /// The whose resources will be cleaned up. public static void Cleanup(SpatialAwarenessMeshObject meshObject, bool destroyGameObject = true, bool destroyMeshes = true) { if (meshObject.GameObject == null) { return; } if (destroyGameObject) { UnityEngine.Object.Destroy(meshObject.GameObject); meshObject.GameObject = null; return; } if (destroyMeshes) { Mesh filterMesh = meshObject.Filter.sharedMesh; Mesh colliderMesh = meshObject.Collider.sharedMesh; if (filterMesh != null) { UnityEngine.Object.Destroy(filterMesh); meshObject.Filter.sharedMesh = null; } if ((colliderMesh != null) && (colliderMesh != filterMesh)) { UnityEngine.Object.Destroy(colliderMesh); meshObject.Collider.sharedMesh = null; } } } } }