Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw multi-stroke lines in unity

Tags:

c#

unity3d

In my unity 2D project, i want to be able draw multistroke lines..
As i tried for now, i can draw unistroke line using LineRenderer component, like drawing "C" or "S" alphabet. But for example "A" or "X" alphabet need more than 1 stroke (multistroke) and i cannot draw that using a LineRenderer. Because LineRenderer store points and connect that points with a line, so we cannot have disconnected parts in out lines.
Do you guys have any idea how to do this?
As additional information, i am using C# language and Unity 4.5 Free. Thank you

This is my Line Renderer Configuration:
Line Renderer Configuration

This is my result example for "C" and "A" alphabet, "A" is the failure one:
AC

like image 834
Schro Avatar asked Mar 18 '23 16:03

Schro


1 Answers

So, i got an answer for my question..
To draw multistroke lines in Unity, i need to make each stroke (each continuous lines) as one line renderer. This is the illustration to make "A" with 2 stroke:
Illustration

To do this in unity:
First step, Make a prefabs for each LineRenderer and assign an unique tag for it. This tag is required to delete the prefabs when it is unneeded. In this example i give "LineDraw" tag for the prefab.
Prefabs

Second step, create an empty gameObject and attach scripts to detect Mouse Down event. In this example i use left mouse button click to create a stroke line, and right mouse button click to clear all the lines.

public class TestLineRenderer : MonoBehaviour {
public GameObject lineDrawPrefabs; // this is where we put the prefabs object

private bool isMousePressed;
private GameObject lineDrawPrefab;
private LineRenderer lineRenderer;
private List<Vector3> drawPoints = new List<Vector3>();

// Use this for initialization
void Start () {
    isMousePressed = false;
}

// Update is called once per frame
void Update () {
    if(Input.GetMouseButtonDown(1))
    {
        // delete the LineRenderers when right mouse down
        GameObject [] delete = GameObject.FindGameObjectsWithTag("LineDraw");
        int deleteCount = delete.Length;
        for(int i = deleteCount - 1; i >= 0; i--)
            Destroy(delete[i]);
    }

    if(Input.GetMouseButtonDown(0))
    {
        // left mouse down, make a new line renderer
        isMousePressed = true;
        lineDrawPrefab = GameObject.Instantiate(lineDrawPrefabs) as GameObject;
        lineRenderer = lineDrawPrefab.GetComponent<LineRenderer>();
        lineRenderer.SetVertexCount(0);
    }
    else if(Input.GetMouseButtonUp(0))
    {
        // left mouse up, stop drawing
        isMousePressed = false;
        drawPoints.Clear ();
    }

    if(isMousePressed)
    {
        // when the left mouse button pressed
        // continue to add vertex to line renderer
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        if (!drawPoints.Contains (mousePos)) 
        {
            drawPoints.Add (mousePos);
            lineRenderer.SetVertexCount (drawPoints.Count);
            lineRenderer.SetPosition(drawPoints.Count - 1, mousePos);
        }
    }
}

}

Its done!

like image 174
Schro Avatar answered Mar 29 '23 04:03

Schro