Hammer Gizmos let us interact with the editor session, displaying and modifying entities' properties directly from our game code.
It's actually pretty agnostic of Hammer, so we could use them in any editor.
Here's some code that draws a sphere from a radius, with a drag control for it.
public class MyEntity : Entity
{
[Property]
public float Radius { get; set; }
public static void DrawGizmos( EditorContext ctx )
{
// This is a serialized version of the Entity currently being drawn
SerializedObject target = ctx.TargetObject;
// We can get key values from it
var radius = target.GetProperty( "radius" ).As.Float;
// Draw a sphere the size of the radius
Gizmo.Draw.Color = Gizmo.Colors.Green;
Gizmo.Draw.SolidSphere( Vector3.Zero, radius, 8, 8 );
// Gizmo that can control the radius
if( Gizmo.Control.Arrow( "radius-arrow", Camera.Rotation.Up,
out var distance, axisOffset: radius + 5,
length: 5, head: "cone", girth: 15.0f ) )
radius += distance;
// And change them
target.GetProperty( "radius" ).SetValue( radius );
}
}
The editor is running the game code through reflection without an instance, there's some obvious advantages to doing it this way:
- It's really simple to add a method to any entity
- Don't need #ifdef UNITY_EDITOR everywhere
- Can ship a single dll
But also some disadvantages:
- The entity doesn't actually exist, can be confusing and limiting at first
- No normal access to editor API
Overall I think it works pretty good.. here's some basic examples of what you can do with your own entities.
User Comments