MeshCleaner.cs 751 B

1234567891011121314151617181920212223
  1. using UnityEngine;
  2. namespace Google.Maps.Examples.Shared {
  3. /// <summary>
  4. /// This component is used to cleanup meshes (if any) when the associated <see cref="GameObject"/>
  5. /// is destroyed.
  6. /// </summary>
  7. /// <remarks>
  8. /// This is done to prevent a memory leak that happens with decorations (such as parapets) as we
  9. /// are dynamically loading/unloading map regions.
  10. /// </remarks>
  11. public class MeshCleaner : MonoBehaviour {
  12. private void OnDestroy() {
  13. // Does this GameObject have a mesh filter?
  14. // We destroy the current mesh (by accessing sharedMesh)
  15. MeshFilter mf = this.gameObject.GetComponent<MeshFilter>();
  16. if (mf != null && mf.sharedMesh) {
  17. Destroy(mf.sharedMesh);
  18. }
  19. }
  20. }
  21. }