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

作者:鬼谷传人 时间:2021-08-04 04:15:08 

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


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;

}
   }
}

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

来源:https://blog.csdn.net/u012519228/article/details/57083006

标签:Unity,LineRenderer,直线
0
投稿

猜你喜欢

  • Android Studio和Gradle使用不同位置JDK的问题解决

    2023-06-27 17:35:04
  • 详解springboot测试类注解

    2023-06-04 21:04:37
  • Java多线程常见案例分析线程池与单例模式及阻塞队列

    2022-06-01 06:42:42
  • Java毕业设计实战之共享租车信息管理系统的实现

    2022-08-02 13:37:32
  • Java利用POI实现导入导出Excel表格示例代码

    2023-05-15 03:02:19
  • 关于Java中HashCode方法的深入理解

    2022-05-28 03:29:33
  • 手把手带你实现一个萌芽版的Spring容器

    2023-03-10 15:45:44
  • Android缓存机制——LruCache的详解

    2023-07-30 07:26:34
  • springboot整合vue实现上传下载文件

    2023-11-14 07:10:37
  • C#中Socket通信用法实例详解

    2022-07-10 03:42:03
  • SpringBoot项目中分页插件PageHelper无效的问题及解决方法

    2021-07-08 23:25:55
  • 使用Jenkins来构建GIT+Maven项目的方法步骤

    2021-11-15 07:57:33
  • 一文看懂RabbitMQ消息丢失如何防止

    2022-03-03 20:36:24
  • Spring Boot 集成Shiro的多realm配置过程

    2023-09-17 10:07:26
  • android异步消息机制 从源码层面解析(2)

    2023-08-06 15:55:39
  • Unity实现简单虚拟摇杆

    2023-08-04 17:33:09
  • 对比Java中的Comparable排序接口和Comparator比较器接口

    2023-10-29 04:29:51
  • Java 数组获取最大和最小值的实例实现

    2021-06-18 15:53:06
  • Java发送邮箱验证码、session校验功能

    2023-09-11 02:44:21
  • 快速了解如何在.NETCORE中使用Generic-Host建立主机

    2022-05-08 15:04:12
  • asp之家 软件编程 m.aspxhome.com