Skip to main content
Version: 🚧 Work in Progress

Gizmos

Gizmos is a powerful tool system that allows you to render simple shapes and lines in the scene and game views. Jungle uses Unity's builtin gizmos system for handling the rendering. A wrapper system is used to listen for gizmo render events and invoke them dynamically in Jungle Nodes.

In short, the gizmo wrapper is a self-managed system designed to make rendering gizmos a breeze.

Implementing Gizmos

Quick video guide on how to implement gizmos into your Jungle Nodes.


Implementation​

There are three methods for rendering gizmos during different conditions:

  • OnDrawGizmos: Base method for rendering gizmos.
    • Always called before OnDrawGizmosSelected and OnDrawGizmosRunning.
  • OnDrawGizmosSelected: Method called when the node is selected in the Jungle Editor.
    • Not called while the node is running even if the node is selected.
  • OnDrawGizmosRunning: Method called when the node is running.
Editor Only

Node gizmos methods are only called in the editor. These methods should be surrounded with script tags to ensure they are not compiled to the build. This is not a requirement but good practice.

#if UNITY_EDITOR
public override void OnDrawGizmos()
{
// Your gizmos code here...
}
#endif

Example​

using Jungle;
using UnityEngine;

public class GizmosSphereNode : IdentityNode
{
[SerializeField]
private Vector3 position;

[SerializeField]
private float radius = 2;

[SerializeField]
private Color color = Color.red;

protected override void OnStart() { }

protected override void OnUpdate() { }

public override void OnDrawGizmos()
{
Color dimmedColor = color;
dimmedColor.a = 0.5f;

Gizmos.color = dimmedColor;
Gizmos.DrawSphere(position, radius);
}

public override void OnDrawGizmosSelected()
{
Gizmos.color = color;
Gizmos.DrawSphere(position, radius);
}

public override void OnDrawGizmosRunning()
{
Color pingPongColor = color;
pingPongColor.a = Mathf.PingPong(Time.time, 1);

Gizmos.color = pingPongColor;
Gizmos.DrawSphere(position, radius);
}
}

Best Practices​

Gizmos methods are invoked frequently so ensure your code is efficient. If you believe Jungle's gizmos system is causing too significant of a performance impact, it can be disabled in the Jungle preferences.


Unity provides a way to draw gizmos in the scene view. Gizmos are visual shapes and lines that can be used for debugging and visualizing information in the scene view. Usually, this feature is only available in the OnDrawGizmos and OnDrawGizmosSelected methods within a MonoBehaviour class.

Luckily, Jungle provides a way to draw gizmos in the scene view all from within your node scripts.

NOTE

You can view gizmos in the game view by enabling the Gizmos toggle in the game view toolbar.

Arrow pointing to where to enable gizmos in game view

How to Draw Gizmos​

Drawing gizmos is as easy as overriding the DrawGizmos or DrawGizmosSelected methods in your node script. Within either of these methods, you safely draw gizmos or handles in the scene view.

using Jungle;

public class GizmosNode : IdentityNode
{
...

public override void DrawGizmos()
{
// Draws gizmos in the scene view when:
// - The node is open in the editor
// - The tree this node is in is running
}

public override void DrawGizmosSelected()
{
// Draws gizmos in the scene view when:
// - The node is selected in the editor
}
}

Example​

Here's an example of a node that draws a red sphere in the scene view:

using Jungle;
using UnityEngine;

public class GizmosNode : GenericNode
{
[SerializeField]
private Vector3 position = Vector3.zero;

[SerializeField]
private float radius = 1f;

protected override void OnStart() { }

protected override void OnUpdate() { }

public override void DrawGizmos()
{
// Draws a red sphere at the origin
Gizmos.color = Color.red;
Gizmos.DrawSphere(position, radius);
}
}

The example above will draw a red sphere at the specified position with the specified radius in the scene view whenever the tree is open in the editor or when the tree is running.

Arrows pointing to gizmos draw by the nodes

Two red sphere are drawn to the scene view because there are two instances of the example above.