Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

NavLocation.GetHashCode

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 int GetHashCode();

Returns

int The hash code representing the node identifier and position of this location.

Description

Returns the hash code for use in collections.

The returned value combines the node identifier and the position of this NavLocation. The hash code is suitable for storing locations in hash-based collections such as Dictionary or HashSet. Two NavLocation values that compare equal through NavLocation.Equals also produce the same hash code.

using System.Collections.Generic;
using UnityEngine;
using Unity.AI.Navigation.LowLevel;

public class NavLocationHashExample : MonoBehaviour
{
    readonly HashSet<NavLocation> m_Visited = new HashSet<NavLocation>();

    void Update()
    {
        using NavWorld world = NavWorld.GetDefaultWorld();
        NavLocation current = world.MapLocation(transform.position, Vector3.one, 0);

        // The hash combines both the node and the exact position, so two locations on the same
        // polygon usually hash differently, though hash collisions are still possible. Key on
        // NavLocation only when the precise point matters; to track which polygons have been
        // visited, key on NavNode instead.
        if (world.IsValid(current) && m_Visited.Add(current))
            Debug.Log($"Visited new location, hash {current.GetHashCode()}");
    }
}