本文介绍了如何统一绘制多行程线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的统一2D项目中,我希望能够绘制多线..
正如我现在尝试的那样,我可以使用LineRenderer组件绘制单笔画线,例如绘制"C"或"S"字母.但是例如"A"或"X"字母需要超过1个笔画(多笔画),而我无法使用LineRenderer绘制它.由于LineRenderer会存储点并将其与一条线连接,因此我们不能在出线中断开零件的连接.
你们有什么想法怎么做吗?
作为附加信息,我正在使用C#语言和Unity 4.5 Free.谢谢

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

这是我的Line Renderer配置:

This is my Line Renderer Configuration:

这是我的"C"和"A"字母的结果示例,"A"是失败的一个:

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

推荐答案

因此,我为我的问题找到了答案..
要在Unity中绘制多笔画线,我需要将每个笔画(每条连续线)作为一个线渲染器.这是用2个笔画制作"A"的插图:


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:


要统一执行此操作:
第一步,为每个LineRenderer制作一个预制件,并为其分配一个唯一的标签.不需要此标签时,将其删除是不需要的.在此示例中,我给预制件添加了"LineDraw"标签.


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.


第二步,创建一个空的gameObject并附加脚本以检测Mouse Down事件.在此示例中,我使用鼠标左键单击以创建描边线,然后单击鼠标右键以清除所有线.

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);
        }
    }
}

}

完成了!

这篇关于如何统一绘制多行程线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 20:17