Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to draw custom lines on top of UI Grid

Discussion in 'UGUI & TextMesh Pro' started by Max_power1965, Jul 14, 2022.

  1. Max_power1965

    Max_power1965

    Joined:
    Oct 31, 2013
    Posts:
    127
    Hello,
    I want to achieve the same result as the image attached, basically, I have a grid layout and I want to draw a Line while dragging, while I have no problem getting the position or each object in the grid, I have no idea how to draw a line like that between the objects.
    Especially because the start and the end of the line have rounded shapes.

    I had a look at the line rendered component bug that doesn't work on the unity Ui system, can someone point me in the right direction?
     

    Attached Files:

  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    604
    One way would be using a 9-slice image and little bit of math to calculate the rotation angle and endpoint offsets. Preferably the the line thickness in sprite should be the same as what you use when drawing. Changing thickness is little bit tricky but doable (requires modifiying pixels per unit multiplier or scale in combination with rect dimensions to ensure that ends don't stretch). But in case of game like you showed there shouldn't be issue preparing appropriate size sprite.

    upload_2022-7-14_22-23-31.png
     
  3. Max_power1965

    Max_power1965

    Joined:
    Oct 31, 2013
    Posts:
    127
    This seems a nice solution that can sole the problem of having a sprite with rounded corners, but once I have a sprite sliced, what is the best solution to draw ot on the screen onto of the UI?