Esempio n. 1
0
    /// <summary>
    /// Clears up variables and other stuff, destroys graphs.
    /// Note that when destroying an AstarPath object, all static variables such as callbacks will be cleared.
    /// </summary>
    void OnDestroy()
    {
        // This class uses the [ExecuteInEditMode] attribute
        // So OnDestroy is called even when not playing
        // Don't do anything when not in play mode
        if (!Application.isPlaying)
        {
            return;
        }

        if (logPathResults == PathLog.Heavy)
        {
            Debug.Log("+++ AstarPath Component Destroyed - Cleaning Up Pathfinding Data +++");
        }

        if (active != this)
        {
            return;
        }

        // Block until the pathfinding threads have
        // completed their current path calculation
        PausePathfinding();

        navmeshUpdates.OnDisable();

        euclideanEmbedding.dirty = false;
        FlushWorkItems();

        // Don't accept any more path calls to this AstarPath instance.
        // This will cause all pathfinding threads to exit (if any exist)
        pathProcessor.queue.TerminateReceivers();

        if (logPathResults == PathLog.Heavy)
        {
            Debug.Log("Processing Possible Work Items");
        }

        // Stop the graph update thread (if it is running)
        graphUpdates.DisableMultithreading();

        // Try to join pathfinding threads
        pathProcessor.JoinThreads();

        if (logPathResults == PathLog.Heavy)
        {
            Debug.Log("Returning Paths");
        }


        // Return all paths
        pathReturnQueue.ReturnPaths(false);

        if (logPathResults == PathLog.Heavy)
        {
            Debug.Log("Destroying Graphs");
        }


        // Clean up graph data
        data.OnDestroy();

        if (logPathResults == PathLog.Heavy)
        {
            Debug.Log("Cleaning up variables");
        }

        // Clear variables up, static variables are good to clean up, otherwise the next scene might get weird data

        // Clear all callbacks
        OnAwakeSettings  = null;
        OnGraphPreScan   = null;
        OnGraphPostScan  = null;
        OnPathPreSearch  = null;
        OnPathPostSearch = null;
        OnPreScan        = null;
        OnPostScan       = null;
        OnLatePostScan   = null;
        On65KOverflow    = null;
        OnGraphsUpdated  = null;

        active = null;
    }