0
\$\begingroup\$
using UnityEngine;

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

    private Vector3[] positions;
    private string oldText;

    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[5] { new Vector3(0, 0, 0), new Vector3(5, 0, 0),
            new Vector3(5, -5, 0), new Vector3(0, -5, 0), new Vector3(0, 0, 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++)
        {
            if (oldText != text)
            {
                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);
                GUI.contentColor = Color.green;
                GUI.Label(new Rect(pos.x, Screen.height - pos.y - 20, textSize.x, textSize.y), i.ToString());
            }
        }
    }

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

I added oldText variable but not sure how use it more then I did so far. I want that if the text is the same in the loop then don't show this text not in red not in green. Each time show the text once if it's not equal.

Lines with text

When the square is closed :

Closed square with the text in green

\$\endgroup\$
  • 1
    \$\begingroup\$ if (oldText != text) is how you would check if they are not equal. Other than that, I don't understand what you are asking. \$\endgroup\$ – Kevin Jul 31 at 23:50
  • \$\begingroup\$ @Kevin I know how to make this comparison but oldText will be empty all the time. Where do I make oldText to be equal to the text ? I need some where to update the oldText. like oldText = text; but not sure where. \$\endgroup\$ – Benzi Avrumi Jul 31 at 23:56
  • 1
    \$\begingroup\$ What is the feature you're trying to achieve? \$\endgroup\$ – Vaillancourt Aug 1 at 0:34
  • \$\begingroup\$ @Vaillancourt I'm using linerenderer to draw lines and in each lines corner where two lines meet I write with a text and label gui the position coordinate. I added now a screenshot to my question. It's working fine with the text but there is a case/s that the last point meeting position is the same like the first for example if I will close the square back to the position 0,0,0 then it will show the same text and position on the one that already exist. So for example in this case of the first and end positions I want it to show the text only once. \$\endgroup\$ – Benzi Avrumi Aug 1 at 0:47
  • \$\begingroup\$ @Vaillancourt there is no need to show twice the position. That is why I want to check that if it's the same text/position don't show the text again. \$\endgroup\$ – Benzi Avrumi Aug 1 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.