Home › Page › UghAlign

UghAlign

Inherits from MonoBehaviour

Use UghAlign to keep a Transform aligned relative to the screen or another UghSprite.

To keep groups of Transforms (such as UghButtons and UghSprites) aligned together:  Create a new GameObject.  Add an UghAlign component to the object.  Place the desired Transforms as children of this new object.  Finally, change any settings of the UghAlign component to match your desired behavior.

Click here to go to the UghAlign Tutorial.

Variables
name type description
alignTo  UghSprite The sprite to align this transform to. If this is set to null, the transform will align itself to the screen.
alignX  boolean If false, the transform's x-coordinate will not change.
alignY  boolean If false, the transform's y-coordinate will not change.
IsDirty  boolean Returns true if the UghAlign needs to be repositioned.
pointToAnchorTo  UghSprite.Anchor Tells the UghAlign which part of the alignTo sprite (or the screen) to align itself to.
customAnchorOffset  Vector3 The custom offset from the alignTo sprite, or the screen. Note: only takes effect if pointToAnchorTo is equal to Custom.
useWorldSpace  boolean Set to true to align using world space and false otherwise.
Functions
name type description
Align void Align the transform this UghAlign is attached to. Should not need to be called manually if AutoUpdate is set to true.
Class Variables
name type description
AutoUpdate boolean If true, all UghAligns update their positions automatically if their alignTo sprites move or their IsDirty is set to true.
Class Functions
name type description
AlignToScreen void Aligns the given transform to the given UghSprite.Anchor on the screen.
AlignToSprite void Aligns the given transform to a sprite.

_

Using UghAlign in script

UghAligns are typically managed completely from the Unity hierarchy and inspector. A tutorial for using UghAligns in the hierarchy and inspector views can be found here.

UghAligns can also be modified via script.  A good example of modifying an UghAlign through script is to move the object that the UghAlign is anchored to, but prevent the UghAlign’s position from also changing. In such cases, make sure to set UghAlign’s AutoUpdate member to false.  If an UghAlign is not using AutoUpdate, you may cause it to align at any time by calling the Align() method.

Using UghAlign’s class functions

UghAlign has two static class functions that can be used to perform a one-time align on any transform.

static void AlignToScreen(Transform transform, UghSprite.Anchor anchor, Vector3 customOffset)

This function will align the given Transform to a point on the screen which corresponds to the given Anchor. For example, passing UghSprite.Anchor.TopCenter as the Anchor would align the center of the Transform with the center of the top edge of the screen.

static void AlignToSprite(Transform transform, UghSprite alignTo, UghSprite.Anchor anchor, Vector3 customOffset)

This function will align the given Transform to the given UghSprite according to the given Anchor.  For example, if you align a Transform to an UghSprite using the UghSprite.Anchor.BottomRight, the center of the Transform will align with the bottom right corner of the UghSprite.