如何统一绘制多笔划线 [英] How to draw multi-stroke lines in unity

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

问题描述

在我的 Unity 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

这是我的线渲染器配置:

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.


第二步,创建一个空的游戏对象并附加脚本来检测鼠标按下事件.在本例中,我使用鼠标左键单击创建一条笔划线,然后单击鼠标右键清除所有线条.

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

}

大功告成!

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆