S&box Wiki

Revision Difference

World_UI#547364

<cat>Dev.UI</cat> <title>World UI</title> <upload src="a5727/8d982ea77614757.png" size="1495562" name="image.png" /> WorldPanels let you render and interact with [UI](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](UI), however instead of deriving from a Panel we derive from a WorldPanel. ```csharp 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. ```csharp var worldPanel = new MyWorldPanel(); worldPanel.Transform = Local.Pawn.Transform; ``` <note>WorldPanels have to be created **clientside**, if you want it synced across clients you can create it in a serverside entity using [Entity.ClientSpawn()](Sandbox.Entity.ClientSpawn).</note> # Interacting with WorldPanels In order to interact with WorldPanels your game needs a WorldInput - you provide a ray and simulated mouse inputs. ```csharp partial class MinimalPlayer : Player { WorldInput WorldInput = new(); public override void BuildInput( InputBuilder input ) { WorldInput.Ray = new Ray( EyePos, EyeRot.Forward ); WorldInput.MouseLeftPressed = input.Down( InputButton.Attack1 ); WorldInput.Ray = new Ray( EyePosition, EyeRotation.Forward ); WorldInput.MouseLeftPressed = input.Down( InputButton.PrimaryAttack); } } ``` # Deleting WorldPanels Like entities, world panels are not garbage collected - in order to remove your panel you need to call `WorldPanel.Delete()`.