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.
![game view screenshot of UI element in world space](https://private-user-images.githubusercontent.com/79327706/292641710-f7ecebd2-11ee-4282-aa2b-c1fd7118c359.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg5MzI1NDEsIm5iZiI6MTcxODkzMjI0MSwicGF0aCI6Ii83OTMyNzcwNi8yOTI2NDE3MTAtZjdlY2ViZDItMTFlZS00MjgyLWFhMmItYzFmZDcxMThjMzU5LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MjElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjIxVDAxMTA0MVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdhZjcyOTQ1ZTdkZjVkMDMyODgyMjhjYjlhMjkzNTYxNTY2NTU5ZjM0MmQ0Y2U3NzA3N2EwNWM3M2Y1MzQzMWQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.qDW_ZVLz0ZokSNTkOeWGVUwXBwbjBQgCLieY536DgCQ)
![scene/editor view screenshoot of UI element in world space](https://private-user-images.githubusercontent.com/79327706/292641889-e9bac69d-25a4-4d56-8fe8-ecc9e3b70e23.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg5MzI1NDEsIm5iZiI6MTcxODkzMjI0MSwicGF0aCI6Ii83OTMyNzcwNi8yOTI2NDE4ODktZTliYWM2OWQtMjVhNC00ZDU2LThmZTgtZWNjOWUzYjcwZTIzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MjElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjIxVDAxMTA0MVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY3MjQyNTcxZjg3ODkwMTgxOThmMWZkOGE2YTMxYTcwY2MwZjhkZjU1OWM4MjA2ZmI3NjRhZmYxMDVlMTJkMTMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.hKDi3f4bHZkaeJmPOH8J-UPgsdGCzXDvrr934UlyNA8)
- 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.