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


pyunity.gui.createTask(loop, coro)[source]
class pyunity.gui.Canvas[source]

Bases: Component

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: object

Class to represent a 2D rect.

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

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

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

Changes both the minimum and maximum points.

Parameters

pos (Vector2) – Point

class pyunity.gui.RectAnchors[source]

Bases: RectData

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 (RectData) – Querying rect

Returns

Relative rect to this

Return type

RectData

class pyunity.gui.RectOffset[source]

Bases: 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

class pyunity.gui.RectTransform[source]

Bases: SingleComponent

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

RectAnchors

offset = None

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

Type

RectOffset

pivot = None

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

Type

Vector2

rotation = 0.0

Rotation in degrees

Type

float

property parent
GetRect(bb=None)[source]

Gets screen coordinates of the bounding box not including offset.

Parameters

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

Returns

Screen coordinates

Return type

RectData

class pyunity.gui.GuiComponent[source]

Bases: Component

A Component that represents a clickable area.

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

Bases: GuiComponent

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]

Bases: NoResponseGuiComponent

A Component that renders something in its RectTransform.

flipX = 0

Type:    int

flipY = 0

Type:    int

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

Bases: GuiRenderComponent

A 2D image component, which is uninteractive.

texture = None

Texture to render

Type

Texture2D

depth = 0.0

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

Type

float

class pyunity.gui.RenderTarget[source]

Bases: GuiRenderComponent

flipY = 1

Type:    int

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

Bases: GuiComponent

A Component that calls a function when clicked.

callback = None

Callback function. Must be a method of a Component.

Type

Event

state = KeyState.UP

Which state triggers the callback

Type

KeyState

mouseButton = MouseCode.Left

Which mouse button triggers the callback

Type

MouseCode

pressed = False

If the button is pressed down or not

Type

bool

async HoverUpdate()[source]
class pyunity.gui.WinFontLoader[source]

Bases: _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: _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: UnixFontLoader

class pyunity.gui.Font[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: IntEnum

An enumeration.

Left = 1

Type:    int

Center = 2

Type:    int

Right = 3

Type:    int

Top = 1

Type:    int

Bottom = 3

Type:    int

class pyunity.gui.Text[source]

Bases: GuiRenderComponent

Component to render text.

font = None

Font object to render

Type

Font

text = Text

Contents of the Text

Type

str

color = None

Fill color

Type

Color

depth = 0.1

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

Type

float

centeredX = TextAlign.Left

How to align in the X direction

Type

TextAlign

centeredY = TextAlign.Center

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().

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

Generate a Texture2D to render.

class pyunity.gui.CheckBox[source]

Bases: GuiComponent

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

checked = False

Current state of the checkbox

Type

bool

async 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.