Button

Detailed reference for the Ursina Button prefab

Button

Button(text='', parent=camera.ui, model=Default, radius=-1, origin=(0,0), text_origin=(0,0), text_size=1, color=Default, collider='box', highlight_scale=1, pressed_scale=1, disabled=False, **kwargs)

Located in ursina/prefabs/button.py

Overview

The Button class extends Entity and provides an interactive UI element. It supports text, icons, mouse interactions, and custom sounds.

Constructor Arguments

ArgumentTypeDefaultDescription
textstr''The text displayed on the button.
parentEntitycamera.uiThe parent entity in the scene graph.
modelModelDefaultThe 3D model used for the button background.
radiusfloat-1Corner radius for rounding the button shape.
origintuple(float)(0,0)Anchor point of the button entity.
text_origintuple(float)(0,0)Anchor point of the text within the button.
text_sizefloat1Scale of the text.
colorColorDefaultBase color of the button.
colliderstr'box'Type of collider for mouse interaction.
highlight_scalefloat1Scale multiplier when hovered.
pressed_scalefloat1Scale multiplier when pressed.
disabledboolFalseIf True, disables input responses.
**kwargsanyAdditional keyword arguments passed to the base Entity class.

Attributes

NameTypeDescription
origintupleCurrent origin point of the button.
colorColorBase color (tinted).
highlight_colorColorColor when hovered (color.tint(.2)).
pressed_colorColorColor when pressed (color.tint(-.2)).
highlight_scalefloatHover scale multiplier.
pressed_scalefloatPressed scale multiplier.
highlight_soundSoundSound played on hover (if set).
pressed_soundSoundSound played on click (if set).
colliderstrCollider shape for input detection.
disabledboolDisabled state flag.
text_entityTextInternal text entity instance.
text_origintupleOrigin of the text entity.

Properties

PropertyDescription
.textGet or set the button text.
.text_originGet or set the text origin.
.text_colorGet or set the text color.
.iconGet or set the button icon.
.icon_world_scaleGet or set the icon scale in world units.
.text_sizeGet or set the text size.
.originGet or set the button origin.

Methods

input(key)

Called on input events. Override to handle custom key actions.

on_mouse_enter()

Called when the cursor enters the button collider.

on_mouse_exit()

Called when the cursor exits the button collider.

fit_to_text(radius=1, padding=Vec2(Text.size*1.5, Text.size))

Adjusts the button size to fit its text content.

  • radius: corner radius multiplier
  • padding: Vec2 padding around text

Example Usage

from ursina import *
app = Ursina()

# Default button
Button.default_color = color.red
b1 = Button(model='quad', scale=.05, x=-.5, color=color.lime, text='test', text_size=.5, text_color=color.black)
b1.on_click = lambda: print('Button clicked')

# Icon button
b2 = Button(text='hello world!', icon='sword', scale=.25, text_origin=(-.5,0), x=.5)
b2.tooltip = Tooltip('exit')

# Nested under a UI panel
panel = Entity(parent=camera.ui, scale=.2, y=-.2)
b3 = Button(parent=panel, text='new text', scale_x=1, origin=(-.5,.5))

# Custom sounds
Button(text='sound', scale=.2, position=(-.25,-.2), color=color.pink,
       highlight_sound='blip_1', pressed_sound=Audio('coin_1', autoplay=False))

Text('Text size reference', x=.15)

def input(key):
    if key == 'd':
        scene.clear()
    if key == 'space':
        b1.text = 'updated text'

app.run()