Hi, bit of a weird issue here, I’m looking to get the “canvas space” location of a UI element.
So the position it’d be at if it were in the root of the canvas.
I’ve tried various things like the RectTransformUtility PixelAdjustPoint method, and RectTransform’s TransformPoint method, but the issue with this is that it doesn’t seem to provide the actual location the element would be at.
All I can find online is world-space stuff, which is obviously not what I’m looking for.
So, essentially I have a series of elements nested within each other, and I’m trying to get the location of one of these if it were in the root of the canvas, but I can’t seem to find anything on Google that would do this, unless I’m missing something.
in visual scripting, id use this to get the rect, same as transform.
then find center by dividing height and width by 2
Unfortunately I’m not using visual scripting, old-fashioned.
Did find something that works though.
Not sure why RectTransform doesn’t seem to have a feature for this, but for those searching, the following seems to work.
No idea how performance-efficient it is though.
canvasObject.transform.InverseTransformPoint(uiElement.transform.position)
3 Likes
ahh beauty. good stuff.
This is why i love Visual scripting here in unity, how the fuzzy finder tells you what each node does.
and you can see all the related stuff next to it.
This is not the visual scripting sub-forum. Questions here are generally assumed to be about C# scripting.
If it’s inappropriate I’ll not reply to these using VS. Sorry!.
The idea is that it would pop up for folks who google “how do i do X_” and don’t care about what style of code is used. But I understand the need to keep things organized.
Like I was going from c# html python over to visual nodes. Sorta’ works like shader graph and geometry nodes in Blender, so made sense to me lol. My hands are rekt from years of typin’ and I was lookin for a new lazy clicky way.
When I was tryin’ to translate regular code ideas into VS I would often find myself being directed here by google. I would find some code and study it, then make a graph that worked in the same manor. Helped to understand how unity worked a little better. like an alternate angle.
But what would have saved a lot of time was just seeing how to litrally plug the nodes togeather and the actual names for the nodes. as the documentation is … wanting
The more appropriate thing to do would be, to make posts in the VS channel that guide new folks looking to just figure unity out and hope the tags work better for others. as you suggested ^.^
People here are asking for C# questions and expect C# answers, not an entirely different format.
This is one of those small things that only as important as who you ask. It’s trivial to make an extension method out of it and not have to worry about it again.