Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

NavWorld.Equals

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public bool Equals(NavWorld other);

Parameters

Parameter Description
other The NavWorld handle to compare this one with.

Returns

bool true if the two NavWorld values refer to the same underlying NavMesh world, and false otherwise.

Description

Determines whether two NavWorld handles refer to the same underlying NavMesh world.

The comparison checks whether both NavWorld values point to the same internal navigation system. A NavWorld obtained from a previous NavWorld.GetDefaultWorld call isn't equal to one obtained after a NavMesh.RemoveAllNavMeshData call, because the underlying world has been destroyed and re-created.

using UnityEngine;
using UnityEngine.AI;
using Unity.AI.Navigation.LowLevel;

public class NavWorldEqualityExample : MonoBehaviour
{
    NavWorld m_World;
    int m_CachedWorldHash;

    void OnEnable()
    {
        m_World = NavWorld.GetDefaultWorld();
        m_CachedWorldHash = m_World.GetHashCode();
        Debug.Log($"Cached the NavMesh world hash {m_CachedWorldHash}");
        CompareNavigationWorldToPrevious();

        // Destroying all the data makes every existing handle to the old world invalid.
        NavMesh.RemoveAllNavMeshData();
        Debug.Log($"Removed all NavMesh data.");

        //m_CachedWorldHash = m_World.GetHashCode();
        //Debug.Log($"Cached the NavMesh world, hash {m_CachedWorldHash}");
        CompareNavigationWorldToPrevious();
    }

    public void CompareNavigationWorldToPrevious()
    {
        NavWorld currentWorld = NavWorld.GetDefaultWorld();

        // Equals, == and != compare the underlying navigation system rather than the struct
        // copy, so two handles obtained from the same world always compare equal.
        if (currentWorld == m_World)
        {
            Debug.Log($"The navigation world is the same as before.");

            // The handles are interchangeable, so keep one and release the duplicate.
            currentWorld.Dispose();
            return;
        }

        Debug.Log($"The world was re-created: hash {m_CachedWorldHash} became {currentWorld.GetHashCode()}");

        // Release the stale handle before replacing it, otherwise its safety handle leaks.
        m_World.Dispose();
        m_World = currentWorld;
        m_CachedWorldHash = m_World.GetHashCode();
    }

    void OnDisable()
    {
        m_World.Dispose();
    }
}

Declaration

public bool Equals(Object obj);

Parameters

Parameter Description
obj An object to interpret as a NavWorld and to compare this one with.

Returns

bool true if the two NavWorld values refer to the same underlying NavMesh world, and false otherwise.

Description

Determines whether two NavWorld handles refer to the same underlying NavMesh world.

The comparison checks whether both NavWorld values point to the same internal navigation system. A NavWorld obtained from a previous NavWorld.GetDefaultWorld call isn't equal to one obtained after a NavMesh.RemoveAllNavMeshData call, because the underlying world has been destroyed and re-created.

using UnityEngine;
using UnityEngine.AI;
using Unity.AI.Navigation.LowLevel;

public class NavWorldEqualityExample : MonoBehaviour
{
    NavWorld m_World;
    int m_CachedWorldHash;

    void OnEnable()
    {
        m_World = NavWorld.GetDefaultWorld();
        m_CachedWorldHash = m_World.GetHashCode();
        Debug.Log($"Cached the NavMesh world hash {m_CachedWorldHash}");
        CompareNavigationWorldToPrevious();

        // Destroying all the data makes every existing handle to the old world invalid.
        NavMesh.RemoveAllNavMeshData();
        Debug.Log($"Removed all NavMesh data.");

        //m_CachedWorldHash = m_World.GetHashCode();
        //Debug.Log($"Cached the NavMesh world, hash {m_CachedWorldHash}");
        CompareNavigationWorldToPrevious();
    }

    public void CompareNavigationWorldToPrevious()
    {
        NavWorld currentWorld = NavWorld.GetDefaultWorld();

        // Equals, == and != compare the underlying navigation system rather than the struct
        // copy, so two handles obtained from the same world always compare equal.
        if (currentWorld == m_World)
        {
            Debug.Log($"The navigation world is the same as before.");

            // The handles are interchangeable, so keep one and release the duplicate.
            currentWorld.Dispose();
            return;
        }

        Debug.Log($"The world was re-created: hash {m_CachedWorldHash} became {currentWorld.GetHashCode()}");

        // Release the stale handle before replacing it, otherwise its safety handle leaks.
        m_World.Dispose();
        m_World = currentWorld;
        m_CachedWorldHash = m_World.GetHashCode();
    }

    void OnDisable()
    {
        m_World.Dispose();
    }
}