简体   繁体   中英

How can I draw/display a text on each corner of two connected line of linerenderer?

using UnityEngine;

public class Square : MonoBehaviour
{
    public LineRenderer lineRenderer;

    private void Start()
    {
        if(lineRenderer == null)
        {
            lineRenderer = GetComponent<LineRenderer>();

            lineRenderer.startWidth = 0.3f;
            lineRenderer.endWidth = 0.3f;
        }

        // 0,  0, 0
        // 5,  0, 0
        // 5, -5, 0
        // 0, -5, 0

        Vector3[] positions = new Vector3[4] { new Vector3(0, 0, 0), new Vector3(5, 0, 0), new Vector3(5, -5, 0), new Vector3(0, -5, 0) };
        DrawSquare(positions);
    }

    void DrawSquare(Vector3[] vertexPositions)
    {

        lineRenderer.positionCount = 4;
        lineRenderer.SetPositions(vertexPositions);
    }
}

The result is:

结果

And this is example of where I want to draw show text as a corner connected two lines marked it with blue circle:

角落的例子

And each connected two lines I want to display a text with the coordinates of the connected lines for example 0,0,0 or 0,5,0

Solution:

using UnityEngine;

public class DrawLines : MonoBehaviour
{
    public LineRenderer lineRenderer;
    public string text;

    private Vector3[] positions;

    private void Start()
    {
        if (lineRenderer == null)
        {
            lineRenderer = GetComponent<LineRenderer>();

            lineRenderer.startWidth = 0.3f;
            lineRenderer.endWidth = 0.3f;
        }

        // 0,  0, 0
        // 5,  0, 0
        // 5, -5, 0
        // 0, -5, 0

        positions = new Vector3[4] { new Vector3(0, 0, 0), new Vector3(5, 0, 0),
            new Vector3(5, -5, 0), new Vector3(0, -5, 0)};
        DrawLine(positions, Color.red, 0.2f);
    }

    void DrawLine(Vector3[] positions, Color color, float duration = 0.2f)
    {
        GameObject myLine = new GameObject();

        myLine.transform.position = positions[0];
        myLine.AddComponent<LineRenderer>();
        LineRenderer lr = myLine.GetComponent<LineRenderer>();
        lr.positionCount = positions.Length;
        lr.startColor = color;
        lr.endColor = color;
        lr.startWidth = 0.1f;
        lr.endWidth = 0.1f;
        lr.useWorldSpace = false;
        lr.SetPositions(positions);
    }

    void DrawText()
    {
        for (int i = 0; i < positions.Length; i++)
        {
            var pos = Camera.main.WorldToScreenPoint(positions[i]);
            text = positions[i].ToString();
            var textSize = GUI.skin.label.CalcSize(new GUIContent(text));
            GUI.contentColor = Color.red;
            GUI.Label(new Rect(pos.x, Screen.height - pos.y, textSize.x, textSize.y), text);
        }
    }

    private void OnGUI()
    {
        if (positions != null)
        {
            if (positions.Length > 0)
            {
                DrawText();
            }
        }
    }
}

The result:

文本

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM