Hi, I need some help with Rect Transforms. So in my script I have a function that builds the skill tree (I only do it once, in editor, not in playtime ofc), and I need some way to calculate and set the length of these lines.
I used a bit of quaternion math to make them look in the right direction, now length is the last part.
The lines are just Images (I’m not using line renderers to stay as customizable as possible)
Here’s a screenshot of how it’s Transform Rect set up:
Thanks!
You could add these kinds of extension methods for RectTransform.
public static void SetSize(this RectTransform self, Vector2 size)
{
Vector2 oldSize = self.rect.size;
Vector2 deltaSize = size - oldSize;
self.offsetMin = self.offsetMin - new Vector2(
deltaSize.x * self.pivot.x,
deltaSize.y * self.pivot.y);
self.offsetMax = self.offsetMax + new Vector2(
deltaSize.x * (1f - self.pivot.x),
deltaSize.y * (1f - self.pivot.y));
}
public static void SetWidth(this RectTransform self, float size)
{
self.SetSize(new Vector2(size, self.rect.size.y));
}
public static void SetHeight(this RectTransform self, float size)
{
self.SetSize(new Vector2(self.rect.size.x, size));
}
1 Like
Yeah I figured out the delta size part myself but for some reason it doesn’t calculate the distance correctly
Vector2 sd = lineTransform.sizeDelta;
sd.y = (lineTransform.position - thisRectTransform.position).magnitude;
lineTransform.sizeDelta = sd;
This code is supposed to connect the line all the way to the center of the upper skill.
Here’s what this code does:
I think I need to convert the distance between perks to the canvas space (it’s in world space) , because that size delta thing is in canvas space. Any idea how to do it?
Something like this might work. canvasRt is the RectTransform of the root canvas.
public Vector2 WorldToCanvas(Camera camera, RectTransform canvasRt, Vector3 wpos)
{
Vector2 viewportPoint = camera.WorldToViewportPoint(wpos);
Vector2 min = new Vector2(canvasRt.rect.xMin, canvasRt.rect.yMin);
return (min + Vector2.Scale(viewportPoint, canvasRt.rect.size));
}
EDIT: Check this page: Unity - Scripting API: RectTransformUtility
RectTransformUtility has some nice methods for conversion.
Didn’t work…
Ok, I need logic here. When I do this:
RectTransform rt = blah-blah, get component;
rt.position = myPosition;
What exactly rt.position is? Is it position of the thing in world space or something else entirely?
EDIT:
I also tried this and all lines had 0 length (apparently pointA and pointB remained as zero vectors after conversion):
RectTransform canvas = GetComponentInParent<Canvas>().GetComponent<RectTransform>();
Vector2 pointA, pointB;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, lineTransform.position, Camera.main, out pointA);
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, thisRectTransform.position, Camera.main, out pointB);
sd.y = Vector2.Distance(pointA, pointB);
EDIT2:
It might be important to mention that the canvas in question has the Scale With Screen Size scaling mode.
I got it to work by switching canvas to Constant Pixel Scale :
The code’s plain and simple (the very first version I had from the beginning):
sd.y = Vector2.Distance(thisRectTransform.position, lineTransform.position);
So there’s no really any problem at all, unless I have to use Scale With Screen Size mode on canvas, for which scenario I still have no solution.
EDIT:
Actually, I do. I remembered canvas has scaleFactor!
sd.y = Vector2.Distance(thisRectTransform.position, lineTransform.position) / canvas.scaleFactor;
Now this works even with Scale With Screen Size mode.
Ok, problem solved xD
1 Like
Final result:
float radiusA = thisRectTransform.sizeDelta.y / 2.0f;
sd.y = Vector2.Distance(thisRectTransform.position, lineTransform.position) / canvas.scaleFactor - radiusA;
HellSpawnGames:
I got it to work by switching canvas to Constant Pixel Scale :
The code’s plain and simple (the very first version I had from the beginning):
sd.y = Vector2.Distance(thisRectTransform.position, lineTransform.position);
So there’s no really any problem at all, unless I have to use Scale With Screen Size mode on canvas, for which scenario I still have no solution.
EDIT:
Actually, I do. I remembered canvas has scaleFactor!
sd.y = Vector2.Distance(thisRectTransform.position, lineTransform.position) / canvas.scaleFactor;
Now this works even with Scale With Screen Size mode.
Ok, problem solved xD
thanks, that was a huge one for me. Didn’t ever get the correct results, until dividing my canvas scale!