Attention

You are viewing PyUnity docs under the develop branch. As such, they are only applicable if you installed from source. Go to https://docs.pyunity.x10.bz/en/latest/ for the most recent release.

# pyunity.gui module¶

class pyunity.gui.Canvas[source]

Bases:

A Component that manages GUI interactions and 2D rendering. Only GameObjects which are a descendant of a Canvas will be rendered.

Update(loop)[source]

Check if any components have been hovered over.

class pyunity.gui.RectData[source]

Bases:

Class to represent a 2D rect.

Parameters
• minOrBoth ( or ) – Minimum value, or another RectData object

• max ( or None) – Maximum value. Default is None

size()[source]
SetPoint(pos)[source]

Changes both the minimum and maximum points.

Parameters

pos () – Point

class pyunity.gui.RectAnchors[source]

Bases:

A type of RectData which represents the anchor points of a RectTransform.

RelativeTo(other)[source]

Get RectData of another Rect relative to the anchor points.

Parameters

other () – Querying rect

Returns

Relative rect to this

Return type

class pyunity.gui.RectOffset[source]

Bases:

Rect to represent the offset from the anchor points of a RectTransform.

static Rectangle(size, center=Vector2(0, 0))[source]

Create a rectangular RectOffset.

Parameters
• size ( or ) – Size of offset

• center (, optional) – Central point of RectOffset, by default Vector2.zero()

Returns

The generated RectOffset

Return type

Move(pos)[source]

Move the RectOffset by a specified amount.

Parameters

pos () –

SetCenter(pos)[source]

Sets the center of the RectOffset. The size is preserved.

Parameters

pos () – Center point of the RectOffset

class pyunity.gui.RectTransform[source]

A Component that represents the size, position and orientation of a 2D object.

anchors = None

Anchor points of the RectTransform. Measured between Vector2(0, 0) and Vector2(1, 1)

Type

offset = None

Offset vectors representing the offset of opposite corners from the anchors. Measured in pixels

Type

pivot = None

Point in which the object rotates around. Measured between Vector2(0, 0) and Vector2(1, 1)

Type

rotation = 0.0

Rotation in degrees

Type

property parent
GetRect(bb=None)[source]

Gets screen coordinates of the bounding box not including offset.

Parameters

bb (, optional) – Bounding box to base anchors off of

Returns

Screen coordinates

Return type

class pyunity.gui.GuiComponent[source]

Bases:

A Component that represents a clickable area.

HoverUpdate()[source]
class pyunity.gui.NoResponseGuiComponent[source]

Bases:

A Component that blocks all clicks that are behind it.

async HoverUpdate()[source]

Empty HoverUpdate function. This is to ensure nothing happens when the component is clicked, and so components behind won’t be updated.

class pyunity.gui.GuiRenderComponent[source]

A Component that renders something in its RectTransform.

flipX = 0

Type:

flipY = 0

Type:

PreRender()[source]
class pyunity.gui.Image2D[source]

A 2D image component, which is uninteractive.

texture = None

Texture to render

Type

depth = 0.0

Z ordering of image. Higher depths are drawn on top.

Type

class pyunity.gui.RenderTarget[source]
flipY = 1

Type:

PreRender()[source]
saveImg(path)[source]
genBuffers(force=False)[source]
setSize(size)[source]
class pyunity.gui.Button[source]

Bases:

A Component that calls a function when clicked.

callback = None

Callback function. Must be a method of a Component.

Type

state = KeyState.UP

Which state triggers the callback

Type

mouseButton = MouseCode.Left

Which mouse button triggers the callback

Type

pressed = False

If the button is pressed down or not

Type

async HoverUpdate()[source]

Bases: _FontLoader

Use the Windows registry to find a font file name.

Parameters

name () – Font name. This is not the same as the file name.

Returns

Font file name

Return type

Raises

Bases: _FontLoader

Use fc-match to find the font file name.

Parameters

name () – Font name. This is not the same as the file name.

Returns

Font file name

Return type

Raises

Bases:

class pyunity.gui.Font[source]

Bases:

Font object to represent font data.

_font

Image font object. Do not use unless you know what you are doing.

Type

ImageFont.FreeTypeFont

name

Font name

Type

size

Font size, in points

Type

class pyunity.gui.TextAlign[source]

Bases:

An enumeration.

Left = 1

Type:

Center = 2

Type:

Right = 3

Type:

Top = 1

Type:

Bottom = 3

Type:

class pyunity.gui.Text[source]

Component to render text.

font = None

Font object to render

Type

text = Text

Contents of the Text

Type

color = None

Fill color

Type

depth = 0.1

Z ordering of the text. Higher values are on top.

Type

centeredX = TextAlign.Left

How to align in the X direction

Type

centeredY = TextAlign.Center

How to align in the Y direction

Type

rect

RectTransform of the GameObject. Can be None

Type

texture

Texture of the text, to save computation time.

Type

Notes

Modifying , , or will call .

PreRender()[source]
GenTexture()[source]

Generate a Texture2D to render.

class pyunity.gui.CheckBox[source]

Bases:

A component that updates the Image2D of its GameObject when clicked.

checked = False

Current state of the checkbox

Type

async HoverUpdate()[source]

Inverts and updates the texture of the Image2D, if there is one.

class pyunity.gui.Gui[source]

Bases:

Helper class to create GUI GameObjects. Do not instantiate.

classmethod MakeButton(name, scene, text='Button', font=None, color=None, texture=None)[source]

Create a Button GameObject and add all relevant GameObjects to the scene.

Parameters
• name () – Name of the GameObject

• scene () – Scene to add all generated GameObjects to

• text (, optional) – Text content of the button, by default “Button”

• font (, optional) – Default font to use, if None then “Arial” is used

• color (, optional) – Fill color of the button text, by default black

• texture (, optional) – Texture for the button background.

Returns

A tuple containing the of button, the component and the component.

Return type

Notes

This will create 3 GameObjects in this hierarchy:

<specified button name>
|- Button
|- Text


The generated GameObject can be accessed from the gameObject property of the returned components. The GameObject will have two components, and . The GameObject will have two components, and .

classmethod MakeCheckBox(name, scene)[source]

Create a CheckBox GameObject and add the appropriate components needed.

Parameters
• name () – Name of GameObject

• scene () – Scene to add GameObject to

Returns

A tuple of the as well as the component.

Return type

Notes

The generated GameObject can be accessed from the gameObject property of the returned components. The GameObject will have 3 properties added: a , a and an .