Draw line between two gameObjects on UI Canvas

In Unity I want to draw a line between 2 gameObjects on a UI canvas.

After seeing it work in the sample from the documentation I started to implement my own. After playing around with some values in the gizmos I ventured forward to the OnPopulateMesh method to see it for real, but eventhough the Gizmos show exactly what I want, I saw nothing.

I played around with the order of the vertex indexes in the AddTriangle method, but that did nothing.

[ExecuteInEditMode]
public class UILineRenderer : Graphic
{
    public Vector3 BeginPoint;
    public float BeginOffset;
    public Vector3 EndPoint;
    public float EndOffset;
    public float Thickness;

    protected override void OnPopulateMesh(VertexHelper vh)
    {
        var delta = EndPoint - BeginPoint;
        var perpendicularToUI = Vector3.Cross(delta, Vector3.forward);
        var thicknessVector = perpendicularToUI.normalized * (Thickness / 2f);

        var beginOrigin = BeginPoint + (delta.normalized * BeginOffset);
        var endOrigin = EndPoint - (delta.normalized * EndOffset);

        vh.Clear();

        UIVertex vertex = UIVertex.simpleVert;
        vertex.color = color;

        vertex.position = beginOrigin + thicknessVector;
        vh.AddVert(vertex);
        
        vertex.position = beginOrigin - thicknessVector;
        vh.AddVert(vertex);
        
        vertex.position = endOrigin + thicknessVector;
        vh.AddVert(vertex);

        vertex.position = endOrigin - thicknessVector;
        vh.AddVert(vertex);

        vh.AddTriangle(0, 1, 2);
        vh.AddTriangle(2, 3, 0);
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawLine(BeginPoint, EndPoint);

        var delta = EndPoint - BeginPoint;
        var perpen = Vector3.Cross(delta, Vector3.forward);
        var thicknessVector = perpen.normalized * (Thickness / 2f);

        var beginOrigin = BeginPoint + (delta.normalized * BeginOffset);
        var endOrigin = EndPoint - (delta.normalized * EndOffset);

        var v0 = beginOrigin + thicknessVector;
        var v1 = beginOrigin - thicknessVector;

        var v2 = endOrigin + thicknessVector;
        var v3 = endOrigin - thicknessVector;

        Gizmos.color = color;
        Gizmos.DrawLine(v0, v2);
        Gizmos.DrawLine(v1, v3);
    }
}

Also I tried referencing the child gameObject so I could make the RectTransform larger to enclose the total space the line would use.

Why won’t my line / triangles render?

1

You can use unity’s LineRenderer

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SetPosition_Line : MonoBehaviour
{
    private LineRenderer line;
    public GameObject TargetPoint;
    // Start is called before the first frame update
    void Start()
    {
        line = GetComponent<LineRenderer>();
        // line.useWorldSpace = false;
        // line.SetPosition(0, transform.position);
        Vector3[] pathPoints = { new Vector3(0,0,0), new Vector3(10,10,10) };
        line .positionCount = 2;
        line .SetPositions(pathPoints);
    }

    // Update is called once per frame
    void Update()
    {
        // in the start enough. not need to do it un the update to just draw the line
    }
}

Or a more comeplex proposal is to use GL.LINES. Find adapted script from the documentation.

using UnityEngine;

public class Example : MonoBehaviour
{
    // Draws a line from "startVertex" var to the curent mouse position.
    public Material mat;
    Vector3 startVertex;
    Vector3 mousePos;

    void Start()
    {
        startVertex = Vector3.zero;
    }

    void Update()
    {
        mousePos = Input.mousePosition;
        // Press space to update startVertex
        if (Input.GetKeyDown(KeyCode.Space))
        {
            startVertex = new Vector3(mousePos.x / Screen.width, mousePos.y / Screen.height, 0);
        }
    }

    void OnPostRender()
    {
        if (!mat)
        {
            Debug.LogError("Please Assign a material on the inspector");
            return;
        }
        GL.PushMatrix();
        mat.SetPass(0);
        GL.LoadOrtho();

        GL.Begin(GL.LINES);
        GL.Color(Color.red);
        //GL.Vertex(startVertex);
        //GL.Vertex(new Vector3(mousePos.x / Screen.width, mousePos.y / Screen.height, 0));
        
        GL.Vertex(new Vector3(0, 0, 0));
        GL.Vertex(new Vector3(0.3f, 0.3f, 0f));

        GL.Vertex(new Vector3(0.3f, 0.3f, 0f));
        GL.Vertex(new Vector3(0.3f, 0.5f, 0f));

        GL.Vertex(new Vector3(0.3f, 0.5f, 0f));
        GL.Vertex(new Vector3(0.7f, 0.5f, 0f));

        GL.End();

        GL.PopMatrix();
    }
}

Output:

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật