Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

NavQueryBuffer.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 A hash code derived from the buffer's internal allocation identifier.

Description

Computes a hash code for identifying this NavQueryBuffer in hash-based collections.

The returned value is suitable for storing the NavQueryBuffer in hash-based collections such as Dictionary or HashSet. Two NavQueryBuffer values that compare equal through NavQueryBuffer.Equals also produce the same hash code.

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

public class NavQueryBufferHashExample : MonoBehaviour
{
    readonly Dictionary<NavQueryBuffer, string> m_Labels = new Dictionary<NavQueryBuffer, string>();
    NavWorld m_World;
    NavQueryBuffer m_Buffer;

    void OnEnable()
    {
        m_World = NavWorld.GetDefaultWorld();
        m_Buffer = new NavQueryBuffer(m_World, Allocator.Persistent, 1024);
        m_Labels[m_Buffer] = "main";
        Debug.Log($"Registered buffer with hash {m_Buffer.GetHashCode()}");
    }

    void OnDisable()
    {
        // Drop the key before disposing, otherwise the dictionary keeps a stale buffer
        // whose hash no longer matches the disposed allocation.
        m_Labels.Remove(m_Buffer);

        m_Buffer.Dispose();
        m_World.Dispose();
    }
}