Adds 2D world space functionality to Unity's UI Toolkit Document
Utilize this script in your Unity projects to convert screen space UI Toolkit trees/documents into your 2D world space.
The MonoBehaviour script allows:
- Using the attached gameObject's transform to change the tree/documents position, z-axis rotation, and scale in 2D world space.
- Scale in world space based on a reference unit size on either horizontal or vertical axis.
- Additional scaling in world space from the attached transform's localscale.
- Bounding box displayed in editor window using debug lines for easy positioning/reference.
- Your target UI document has a top tree asset with dimensions
(600px, 480px)
. - You set the property
unitSize
to 16 and thereferenceDimension
enum toHorizontal
. - The final world space dimensions of the object would be
(16 units, 12.8 units)
- If the
localScale.z
of the attached transform was0.5
, the final world space dimensions would be(8 units, 6.4 units)
.
Do not add a UI Document
component to the gameObject
, the script attaches this component on initialization and handles instances of the provided PanelSettings
.
You can copy the script contents to a local script, or pull the repository and install locally.