A* Pathfinding Project  4.0.9
The A* Pathfinding Project for Unity 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Events Macros Groups Pages
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.
 
bool Draw (Hasher hasher)
 Schedules the meshes for the specified hash to be drawn.
 
void DrawExisting ()
 Schedules all meshes that were drawn the last frame (last time FinalizeDraw was called) to be drawn again.
 
void FinalizeDraw ()
 Call after all Draw commands for the frame have been done to draw everything.
 
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.
 

Public Attributes

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

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

void ClearCache ( )

Destroys all cached meshes.

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

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.
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.

void FinalizeDraw ( )

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

GraphGizmoHelper GetGizmoHelper ( AstarPath  active,
Hasher  hasher 
)
Mesh GetMesh ( )
private
GraphGizmoHelper GetSingleFrameGizmoHelper ( AstarPath  active)
bool HasCachedMesh ( Hasher  hasher)

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

void PoolMesh ( Mesh  mesh)
private
void RemoveUnusedMeshes ( List< MeshWithHash meshList)
private

Member Data Documentation

Stack<Mesh> cachedMeshes = new Stack<Mesh>()
private
HashSet<ulong> existingHashes = new HashSet<ulong>()
private
Material lineMaterial

Material to use for the navmesh outline in the editor.

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

Material to use for the navmesh in the editor.

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

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