欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity 通过LineRenderer绘制两点之间的直线操作

程序员文章站 2022-06-16 09:42:18
我就废话不多说了,大家还是直接看代码吧~private linerenderer line;//画线line = this.gameobject.addcomponent

我就废话不多说了,大家还是直接看代码吧~

private linerenderer line;
//画线
line = this.gameobject.addcomponent<linerenderer>();
//只有设置了材质 setcolor才有作用
line.material = new material(shader.find("particles/additive"));
line.setvertexcount(2);//设置两点
line.setcolors(color.yellow, color.red); //设置直线颜色
line.setwidth(0.01f, 0.01f);//设置直线宽度
//设置指示线的起点和终点
line.setposition(0, initposition);
line.setposition(1, newposition);

绘制圆

下面是以物体position为圆心,半径为r,在xz平面上的画圆

 public float r;//半径
 public int n;//不要超过45
 line.setvertexcount(n+1);//这里要加1,达成闭合曲线
 for (int i = 0; i < n + 1; i++)
{
  float x = r * mathf.cos((360 / n * i) * mathf.deg2rad) + transform.position.x; //确定x坐标
  float z = r * mathf.sin((360 / n * i) * mathf.deg2rad) + transform.position.z; //确定z坐标
  line.setposition(i, new vector3(x, transform.position.y, z));         
}

补充:unity 通过linerenderer组件画线段

Unity 通过LineRenderer绘制两点之间的直线操作

using system.collections;
using system.collections.generic;
using unityengine; 
public class drawlinebymouse : monobehaviour { 
    //画线组件预制体
    public transform gestureonscreenprefab; 
    private int strokeid = -1; 
    private vector3 virtualkeyposition = vector2.zero;
    private rect drawarea;
    private runtimeplatform platform;
    private int vertexcount = 0; 
    private list<linerenderer> gesturelinesrenderer = new list<linerenderer>();
    private linerenderer currentgesturelinerenderer; 
    //gui
    private string message="what is this ?";
    private bool recognized;
    private string newgesturename = ""; 
    void start()
    {
        platform = application.platform;
        drawarea = new rect(0, 0, screen.width - screen.width / 3, screen.height);
    } 
    void update()
    { 
        if (platform == runtimeplatform.android || platform == runtimeplatform.iphoneplayer)
        {
            if (input.touchcount > 0)
            {
                virtualkeyposition = new vector3(input.gettouch(0).position.x, input.gettouch(0).position.y);
            }
        }
        else
        {
            if (input.getmousebutton(0))
            {
                virtualkeyposition = new vector3(input.mouseposition.x, input.mouseposition.y);
            }
        }
 
        if (drawarea.contains(virtualkeyposition))
        {
 
            if (input.getmousebuttondown(0))
            {
               ++strokeid;
 
               // transform tmpgesture = instantiate(gestureonscreenprefab, transform.position, transform.rotation) as transform;
               // currentgesturelinerenderer = tmpgesture.getcomponent<linerenderer>();
 
                gameobject go = new gameobject("linerenderer");
                go.transform.position = camera.main.transform.position;
                go.transform.rotation = camera.main.transform.rotation;
                currentgesturelinerenderer = go.addcomponent<linerenderer>();              
                currentgesturelinerenderer.startwidth = 0.1f; 
                gesturelinesrenderer.add(currentgesturelinerenderer); 
                vertexcount = 0;
            }
 
            if (input.getmousebutton(0))
            {            
                currentgesturelinerenderer.setvertexcount(++vertexcount);
                currentgesturelinerenderer.setposition(vertexcount - 1, camera.main.screentoworldpoint(new vector3(virtualkeyposition.x, virtualkeyposition.y, 10)));
            }
        }
    } 
    void ongui()
    {
 
        gui.box(drawarea, "draw area"); 
        gui.label(new rect(10, screen.height - 40, 500, 50), message); 
        if (gui.button(new rect(screen.width - 100, 10, 100, 30), "clear line"))
        {
            foreach (linerenderer linerenderer in gesturelinesrenderer)
            {
                linerenderer.setvertexcount(0);
                destroy(linerenderer.gameobject);
            }
            gesturelinesrenderer.clear();
            recognized = true;
 
        }
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。