pyunity.gui module

class pyunity.gui.Canvas(transform, is_dummy=False)[source]

Bases: pyunity.core.Component

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

Update(updated)[source]

Check if any components have been clicked on.

Parameters:updated (list) – List of already updated GameObjects.
class pyunity.gui.RectData(min_or_both=None, max=None)[source]

Bases: object

Class to represent a 2D rect.

Parameters:
  • min_or_both (Vector2 or RectData) – Minimum value, or another RectData object
  • max (Vector2 or None) – Maximum value. Default is None
class pyunity.gui.RectAnchors(min_or_both=None, max=None)[source]

Bases: pyunity.gui.RectData

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

SetPoint(p)[source]

Changes both the minimum and maximum anchor points.

Parameters:p (Vector2) – Point
RelativeTo(other)[source]

Get RectData of another Rect relative to the anchor points.

Parameters:other (RectData) – Querying rect
Returns:Relative rect to this
Return type:RectData
class pyunity.gui.RectOffset(min_or_both=None, max=None)[source]

Bases: pyunity.gui.RectData

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 (float or Vector2) – Size of offset
  • center (Vector2, optional) – Central point of RectOffset, by default Vector2.zero()
Returns:

The generated RectOffset

Return type:

RectOffset

Move(pos)[source]

Move the RectOffset by a specified amount.

Parameters:pos (Vector2) –
SetCenter(pos)[source]

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

Parameters:pos (Vector2) – Center point of the RectOffset
SetPoint(pos)[source]
class pyunity.gui.RectTransform(transform)[source]

Bases: pyunity.core.SingleComponent

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

anchors

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

Type:RectAnchors
offset

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

Type:RectOffset
pivot

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

Type:Vector2
rotation

Rotation in degrees

Type:float
parent
GetRect()[source]

Gets screen coordinates of the bounding box.

Returns:Screen coordinates
Return type:RectData
class pyunity.gui.GuiComponent(transform, is_dummy=False)[source]

Bases: pyunity.core.Component

A Component that represents a clickable area.

HoverUpdate()[source]
class pyunity.gui.NoResponseGuiComponent(transform, is_dummy=False)[source]

Bases: pyunity.gui.GuiComponent

A Component that blocks all clicks that are behind it.

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.Image2D(transform)[source]

Bases: pyunity.gui.NoResponseGuiComponent

A 2D image component, which is uninteractive.

texture

Texture to render

Type:Texture2D
depth

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

Type:float
class pyunity.gui.Button(transform)[source]

Bases: pyunity.gui.GuiComponent

A Component that calls a function when clicked.

callback

Callback function

Type:FunctionType
state

Which state triggers the callback

Type:KeyState
mouseButton

Which mouse button triggers the callback

Type:MouseCode
pressed

If the button is pressed down or not

Type:bool
state = 1
mouseButton = 1
HoverUpdate()[source]
class pyunity.gui.WinFontLoader[source]

Bases: pyunity.gui._FontLoader

classmethod LoadFile(name)[source]

Use the Windows registry to find a font file name.

Parameters:name (str) – Font name. This is not the same as the file name.
Returns:Font file name
Return type:str
Raises:PyUnityException – If the font is not found
class pyunity.gui.UnixFontLoader[source]

Bases: pyunity.gui._FontLoader

classmethod LoadFile(name)[source]

Use fc-match to find the font file name.

Parameters:name (str) – Font name. This is not the same as the file name.
Returns:Font file name
Return type:str
Raises:PyUnityException – If the font is not found
class pyunity.gui.FontLoader[source]

Bases: pyunity.gui.UnixFontLoader

class pyunity.gui.Font(name, size, imagefont)[source]

Bases: object

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:str
size

Font size, in points

Type:int
class pyunity.gui.TextAlign[source]

Bases: enum.IntEnum

An enumeration.

Left = 1
Center = 2
Right = 3
class pyunity.gui.Text(transform)[source]

Bases: pyunity.gui.NoResponseGuiComponent

Component to render text.

font

Font object to render

Type:Font
text

Contents of the Text

Type:str
color

Fill color

Type:Color
depth

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

Type:float
centeredX

How to align in the X direction

Type:TextAlign
centeredY

How to align in the Y direction

Type:TextAlign
rect

RectTransform of the GameObject. Can be None

Type:RectTransform
texture

Texture of the text, to save computation time.

Type:Texture2D

Notes

Modifying font, text, or color will call GenTexture().

centeredX = 1
centeredY = 2
GenTexture()[source]

Generate a Texture2D to render.

class pyunity.gui.CheckBox(transform, is_dummy=False)[source]

Bases: pyunity.gui.GuiComponent

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

checked

Current state of the checkbox

Type:bool
HoverUpdate()[source]

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

class pyunity.gui.Gui[source]

Bases: object

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 (str) – Name of the GameObject
  • scene (Scene) – Scene to add all generated GameObjects to
  • text (str, optional) – Text content of the button, by default “Button”
  • font (Font, optional) – Default font to use, if None then “Arial” is used
  • color (Color, optional) – Fill color of the button text, by default black
  • texture (Texture2D, optional) – Texture for the button background.
Returns:

A tuple containing the RectTransform of button, the Button component and the Text component.

Return type:

tuple

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 Button GameObject will have two components, Button and RectTransform. The Button GameObject will have two components, Image2D and RectTransform.

classmethod MakeCheckBox(name, scene)[source]

Create a CheckBox GameObject and add the appropriate components needed.

Parameters:
  • name (str) – Name of GameObject
  • scene (Scene) – Scene to add GameObject to
Returns:

A tuple of the RectTransform as well as the CheckBox component.

Return type:

tuple

Notes

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