A* Pathfinding Project  4.1.19
The A* Pathfinding Project for Unity 3D
RetainedGizmos Class Reference

Helper for drawing Gizmos in a performant way. More...

Detailed Description

Helper for drawing Gizmos in a performant way.

This is a replacement for the Unity Gizmos class as that is not very performant when drawing very large amounts of geometry (for example a large grid graph). These gizmos can be persistent, so if the data does not change, the gizmos do not need to be updated.

How to use

  • Create a Hasher object and hash whatever data you will be using to draw the gizmos Could be for example the positions of the vertices or something. Just as long as if the gizmos should change, then the hash changes as well.
  • Check if a cached mesh exists for that hash
  • If not, then create a Builder object and call the drawing methods until you are done and then call Finalize with a reference to a gizmos class and the hash you calculated before.
  • Call gizmos.Draw with the hash.
  • When you are done with drawing gizmos for this frame, call gizmos.FinalizeDraw
var a = Vector3.zero;
var b = Vector3.one;
var color = Color.red;
var hasher = new RetainedGizmos.Hasher();
hasher.AddHash(a.GetHashCode());
hasher.AddHash(b.GetHashCode());
hasher.AddHash(color.GetHashCode());
if (!gizmos.Draw(hasher)) {
using (var helper = gizmos.GetGizmoHelper(active, hasher)) {
builder.DrawLine(a, b, color);
builder.Finalize(gizmos, hasher);
}
}

Classes

class  Builder
 Helper for drawing gizmos. More...
 
struct  Hasher
 Combines hashes into a single hash value. More...
 
struct  MeshWithHash
 

Public Member Functions

void ClearCache ()
 Destroys all cached meshes. More...
 
bool Draw (Hasher hasher)
 Schedules the meshes for the specified hash to be drawn. More...
 
void DrawExisting ()
 Schedules all meshes that were drawn the last frame (last time FinalizeDraw was called) to be drawn again. More...
 
void FinalizeDraw ()
 Call after all Draw commands for the frame have been done to draw everything. More...
 
GraphGizmoHelper GetGizmoHelper (AstarPath active, Hasher hasher)
 
GraphGizmoHelper GetSingleFrameGizmoHelper (AstarPath active)
 
bool HasCachedMesh (Hasher hasher)
 True if there already is a mesh with the specified hash. More...
 

Public Attributes

Material lineMaterial
 Material to use for the navmesh outline in the editor. More...
 
Material surfaceMaterial
 Material to use for the navmesh in the editor. More...
 

Private Member Functions

Mesh GetMesh ()
 
void PoolMesh (Mesh mesh)
 
void RemoveUnusedMeshes (List< MeshWithHash > meshList)
 

Private Attributes

Stack< Mesh > cachedMeshes = new Stack<Mesh>()
 
HashSet< ulong > existingHashes = new HashSet<ulong>()
 
List< MeshWithHashmeshes = new List<MeshWithHash>()
 
HashSet< ulong > usedHashes = new HashSet<ulong>()
 

Member Function Documentation

◆ ClearCache()

void ClearCache ( )

Destroys all cached meshes.

Used to make sure that no memory leaks happen in the Unity Editor.

◆ Draw()

bool Draw ( Hasher  hasher)

Schedules the meshes for the specified hash to be drawn.

Returns
False if there is no cached mesh for this hash, you may want to submit one in that case. The draw command will be issued regardless of the return value.

◆ DrawExisting()

void DrawExisting ( )

Schedules all meshes that were drawn the last frame (last time FinalizeDraw was called) to be drawn again.

Also draws any new meshes that have been added since FinalizeDraw was last called.

◆ FinalizeDraw()

void FinalizeDraw ( )

Call after all Draw commands for the frame have been done to draw everything.

◆ GetGizmoHelper()

GraphGizmoHelper GetGizmoHelper ( AstarPath  active,
Hasher  hasher 
)

◆ GetMesh()

Mesh GetMesh ( )
private

◆ GetSingleFrameGizmoHelper()

GraphGizmoHelper GetSingleFrameGizmoHelper ( AstarPath  active)

◆ HasCachedMesh()

bool HasCachedMesh ( Hasher  hasher)

True if there already is a mesh with the specified hash.

◆ PoolMesh()

void PoolMesh ( Mesh  mesh)
private

◆ RemoveUnusedMeshes()

void RemoveUnusedMeshes ( List< MeshWithHash meshList)
private

Member Data Documentation

◆ cachedMeshes

Stack<Mesh> cachedMeshes = new Stack<Mesh>()
private

◆ existingHashes

HashSet<ulong> existingHashes = new HashSet<ulong>()
private

◆ lineMaterial

Material lineMaterial

Material to use for the navmesh outline in the editor.

◆ meshes

List<MeshWithHash> meshes = new List<MeshWithHash>()
private

◆ surfaceMaterial

Material surfaceMaterial

Material to use for the navmesh in the editor.

◆ usedHashes

HashSet<ulong> usedHashes = new HashSet<ulong>()
private

The documentation for this class was generated from the following file: