UNITY: How to get the width of the line in pixels?

963 views Asked by At

I am working on a project where you have to draw anything using a LineRenderer Component and then I process your drawing using the positions of the Line. To get the positions is pretty simple, but I need to know which width of the line you choose in game. Here is an example:

enter image description here

As you can see, I've done a simple drawing, but the width of the line will be chosen by the player. Now, for this drawing I've used the Width of 0.15 for the LineRenderer Component: enter image description here

Now, what I have trouble with, is how can I get the width of the Line in pixels. Let's say that the White Image is 900x900 pixels. In this case, how can I get the width of the line in pixels relative to the width of the RawImage used as a Background to draw on top of? Thank you in advance.

2

There are 2 answers

1
luvjungle On BEST ANSWER

That depends on your import settings of the sprite, particulary pixels per unit property. Let's say, that you import your 900x900px image with 90 pixels per unit. Then it will be 10x10 units in Unity. Your line is 0.15 units width (assuming that scale is Vector3.One). 0.15 units * 90 pixels/units = 13.5 pixels.

0
Sebastián On

If your canvas is P pixels wide, it's transform.localScale.x is X and LineRenderer.widthMultiplier is W, then line width in pixels should be (P / X) * W.