S&box Wiki

World UI

World UI


WorldPanels let you render and interact with UI within the world. They are actually being drawn in the world, in the right transparent draw order. So they draw behind things and in front of things like you'd expect. Great for name tags etc.

A WorldPanel inherits all the behavior of a RootPanel meaning you can use it exactly the same as any standard UI panel, the only difference is it renders to the world.

Creating world panels

Creating world panels is nearly identical to creating any other UI panel, however instead of deriving from a Panel we derive from a WorldPanel.

using Sandbox.UI; using Sandbox.UI.Construct; class MyWorldPanel : WorldPanel { public MyWorldPanel() { StyleSheet.Load( "/UI/MyWorldPanel.scss" ); Add.Label( "hello world" ); } }

Spawning WorldPanels

The world panel can then be spawned simply as if it's an entity, setting it's position and rotation.

var worldPanel = new MyWorldPanel(); worldPanel.Transform = Local.Pawn.Transform;
WorldPanels have to be created clientside, if you want it synced across clients you can create it in a serverside entity using Entity.ClientSpawn().

Interacting with WorldPanels

In order to interact with WorldPanels your game needs to call WorldInput.Update periodically, this emulates mouse inputs on all WorldPanels.

[Event.Frame] public void UpdateWorldInput() { // Make a ray of where the local pawn is looking var ray = new Ray( Local.Pawn.EyePos, Local.Pawn.EyeRot.Forward ); // Emulate the mouse buttons with any inputs we want var leftMouseDown = Input.Down( InputButton.Attack1 ); var rightMouseDown = Input.Down( InputButton.Attack2 ); var scroll = new Vector2 { x = Input.MouseWheel }; // ( only x is used ) WorldInput.Update( ray, leftMouseDown, rightMouseDown, scroll ); }

Deleting WorldPanels

Like entities, world panels are not garbage collected - in order to remove your panel you need to call WorldPanel.Delete().

Special Pages



Render Time: 14ms

DB GetPage 2
Generate Html 1
SaveChanges (1) 6
Render Body 0
Render Sidebar 1