ScreenTransform
Lens Studio v2.0.0+
Scripting Name: Component.ScreenTransform
Inherits from Component
Description
Used for positioning objects in 2d screen space. It overrides the regular Transform component on the SceneObject it’s attached to.
See the Screen Transform guide for more information.
Methods
containsScreenPoint(vec2 screenPoint)
: Boolean
Returns true if the screen position is within the boundaries of this ScreenTransform. Useful for checking if a touch event overlaps with this object.
containsWorldPoint(vec3 worldPoint)
: Boolean
Returns true if the world position is within the boundaries of this ScreenTransform. The z
value of the world position is ignored.
isInScreenHierarchy()
: Boolean
Returns true if this ScreenTransform is in a valid screen hierarchy, which is required for anchoring to work. To be in a valid screen hierarchy there must be a Camera component upward in the parent hierarchy, and every object between the Camera and this one must also have a ScreenTransform.
localPointToScreenPoint(vec2 relativeLocalPoint)
: vec2
Converts from a normalized (-1 to 1) position within this ScreenTransform’s bounds to a screen position.
localPointToWorldPoint(vec2 relativeLocalPoint)
: vec3
Converts from a normalized (-1 to 1) position within this ScreenTransform’s bounds to a world position.
screenPointToLocalPoint(vec2 screenPoint)
: vec2
Converts from a screen position to a normalized (-1 to 1) position within this ScreenTransform’s bounds.
screenPointToParentPoint(vec2 screenPoint)
: vec2
Converts from a screen position to a normalized (-1 to 1) position within the parent object’s bounds. This value is useful because it can be used directly for this ScreenTransform’s anchor positioning.
worldPointToLocalPoint(vec3 worldPoint)
: vec2
Converts from a world position to a normalized (-1 to 1) position within this ScreenTransform’s bounds.
worldPointToParentPoint(vec3 worldPoint)
: vec2
Converts from a world position to a normalized (-1 to 1) position within the parent object’s bounds. This value is useful because it can be used directly for this ScreenTransform’s anchor positioning.
Properties
anchors
: Rect
The anchor rect positioning this ScreenTransform proportional to its parent’s bounds. For each field, a value of 0
equals the parent’s center point, and value of -1
or 1
(depending on the side) equals the parent’s full boundary.
For example, a top
value of 1.0
means this ScreenTransform’s top edge will be exactly at the top edge of its parent.
A bottom
value of -0.5
means this ScreenTransform’s bottom edge will be halfway between its parent’s bottom edge and center.
A right
value of 0
means this ScreenTransform’s right edge will be exactly at its parent’s center.
A left
value of -2
means this ScreenTransform’s left edge will be twice as far from its parent’s center as its parent’s left edge is.
offsets
: Rect
This rect is applied after anchors
to determine the final boundaries of the ScreenTransform. It adds an offset in world units (based on the parent Camera’s size
property) to each edge of the ScreenTransform’s boundaries.
For example, a value of 0
for any side will have no effect on boundaries.
A value of 1.0
for any side will offset that edge by 1.0
world unit.
pivot
: vec2
Normalized (x, y) position of the center point used in rotation. (-1, -1) being bottom left, (0, 0) being center, and (1, 1) being top right of the image.
position
: vec3
Basic local position in world units relative to the parent’s center. Useful for animating screen elements with a simple offset.
rotation
: quat
Basic local rotation applied to the SceneObject.
scale
: vec3
Basic local scaling applied to the SceneObject.
Inherited Methods
destroy()
: void
Destroys the component.
getSceneObject()
: SceneObject
Returns the SceneObject the component is attached to.
getTransform()
: Transform
Returns the Transform this component is attached to.
isSame(SerializableWithUID other)
: Boolean
Returns true if this object is the same as other
. Useful for checking if two references point to the same thing.
getTypeName()
: String
Returns the name of this object’s type.
isOfType(String type)
: Boolean
Returns true if the object matches or derives from the passed in type.
Inherited Properties
enabled
: Boolean
If disabled, the Component will stop enacting its behavior.
Examples
// @input Component.ScreenTransform screenTransform
// Move the ScreenTransform's anchor center
script.screenTransform.anchors.setCenter(new vec2(-0.25, 0.5));
// Change the ScreenTransform's anchor size
script.screenTransform.anchors.setSize(new vec2(0.25, 0.25));
// @input Component.ScreenTransform screenTransform
// Move the ScreenTransform to match the position of touch events
function onTouch(eventData) {
var touchPos = eventData.getTouchPosition();
var parentPos = script.screenTransform.screenPointToParentPoint(touchPos);
script.screenTransform.anchors.setCenter(parentPos);
}
script.createEvent("TouchStartEvent").bind(onTouch);
script.createEvent("TouchMoveEvent").bind(onTouch);
// @input Component.ScreenTransform screenTransform
// Check if a touch starts inside the ScreenTransform
script.createEvent("TouchStartEvent").bind(function(eventData) {
var touchPos = eventData.getTouchPosition();
if (script.screenTransform.containsScreenPoint(touchPos)) {
print("contains screen point!");
}
});
// @input Component.ScreenTransform screenTransform
// @input Component.ScreenTransform other
// Move the ScreenTransform to match the world position of another ScreenTransform
var worldPos = script.other.localPointToWorldPoint(new vec2(0, 0));
var parentPos = script.screenTransform.worldPointToParentPoint(worldPos);
script.screenTransform.anchors.setCenter(parentPos);
Still Looking for help?
Visit Support