/// <summary>
 /// Stop the MLMeshinddg API before live code changes are compiled.
 /// </summary>
 void StopAPIBeforeAssemblyReload()
 {
     if (MLMeshing.IsStarted)
     {
         MLMeshing.Stop();
     }
 }
 /// <summary>
 /// Stop the MLMeshing API.
 /// </summary>
 void OnDestroy()
 {
     #if PLATFORM_LUMIN
     if (MLMeshing.IsStarted)
     {
         MLMeshing.Stop();
     }
     #if UNITY_EDITOR
     UnityEditor.AssemblyReloadEvents.beforeAssemblyReload -= StopAPIBeforeAssemblyReload;
     #endif
     #endif
 }
        /// <summary>
        /// Verify required members and start the MLMeshing API.
        /// </summary>
        void Start()
        {
            if (MeshParent == null)
            {
                Debug.LogError("Error: MLMeshingBehavior.MeshParent is not set, disabling script.");
                enabled = false;
                return;
            }

            if (MeshPrefab == null)
            {
                Debug.LogError("Error: MLMeshingBehavior.MeshPrefab is not set, disabling script.");
                enabled = false;
                return;
            }

            if ((MeshType == MeshingType.PointCloud && PointCloudMeshMaterial == null) || (MeshType == MeshingType.Triangles && TriangleMeshMaterial == null))
            {
                Debug.LogErrorFormat("Error: MLMeshingBehavior failed because material member required for mesh type {0} is null, disbling script.", MeshType.ToString());
                enabled = false;
                return;
            }

            #if PLATFORM_LUMIN
            MLMeshing.MeshSettings initSettings;

            if (UseDefaultSettings)
            {
                initSettings = MLMeshing.MeshSettings.Create();
                SetSettings(ref initSettings);
            }
            else
            {
                initSettings = GetSettings();
            }

            MLResult result = MLMeshing.Start(initSettings);
            if (!result.IsOk)
            {
                Debug.LogErrorFormat("Error: MLMeshingBehavior failed starting MLMeshing, disabling script. Reason: {0}", result);
                enabled = false;
                return;
            }

            #if UNITY_EDITOR
            UnityEditor.AssemblyReloadEvents.beforeAssemblyReload += StopAPIBeforeAssemblyReload;
            #endif
            #endif
        }
        /// <summary>
        /// Processes latest Mesh Info request results.
        /// </summary>
        private void ProcessMeshInfoRequests()
        {
            #if PLATFORM_LUMIN
            MLResult result = MLMeshing.RequestMeshInfo(in meshExtents);
            if (!result.IsOk)
            {
                Debug.LogErrorFormat("Error: MLMeshingBehavior failed to create a new mesh info request, disabling script. Reason: {0}", result);
                enabled = false;
                return;
            }

            MLMeshing.GetMeshInfoResults(out Dictionary <MagicLeapNativeBindings.MLCoordinateFrameUID, Tuple <MLMeshing.BlockInfo, ulong> > results);
            if (results.Count > 0)
            {
                foreach (Tuple <MLMeshing.BlockInfo, ulong> pair in results.Values)
                {
                    switch (pair.Item1.State)
                    {
                    case MLMeshing.MeshState.New:
                    case MLMeshing.MeshState.Updated:
                    case MLMeshing.MeshState.Unchanged:
                        AddToQueueIfNecessary(pair.Item1);
                        break;

                    case MLMeshing.MeshState.Deleted:
                        OnMeshRemoved?.Invoke(pair.Item1.ID);

                        meshesToGenerateIDs.Remove(pair.Item1.ID);

                        if (meshObjMap.TryGetValue(pair.Item1.ID, out MeshObject meshObj))
                        {
                            Destroy(meshObj.Obj);
                            meshObjMap.Remove(pair.Item1.ID);
                        }

                        break;

                    default:
                        break;
                    }
                }
            }
            #endif
        }