通过添加组件 LineRenderer 来实现
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class HuaXianScripts : MonoBehaviour
{
//LineRenderer
public LineRenderer lineRenderer;
//定义一个Vector3,用来存储鼠标点击的位置
private Vector3 position;
//用来索引端点
private int index = 0;
//端点数
private int LengthOfLineRenderer = 0;
//LineRenderer个数标志
private int LineIndex = 0;
[Header("长度")]
public TextMeshProUGUI changDu;
// Start is called before the first frame update
void Start()
{
}
/// <summary>
/// 绘制颜色 长宽
/// </summary>
void LineParameterSetting()
{
//设置材质
// lineRenderer.material = new Material(Shader.Find("ceshi/red"));
//设置颜色
lineRenderer.startColor = Color.red;
lineRenderer.endColor = Color.yellow;
//设置宽度
lineRenderer.startWidth =0.2f;
lineRenderer.endWidth = 0.2f;
}
/// <summary>
/// 点下 抬起
/// </summary>
float dianxia, taiqi;
Vector3 dianxiaweizhi, taiqiweizhi;
// Update is called once per frame
void Update()
{
//鼠标左击
if (Input.GetMouseButton(0))
{
if (Input.GetMouseButtonDown(0))
{
// lineRenderer = new GameObject("Line" + LineIndex).AddComponent<LineRenderer>();
LineParameterSetting();
LineIndex++;
}
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 1000))
{
position = hitInfo.point;
}
//将鼠标点击的屏幕坐标转换为世界坐标,然后存储到position中
//position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z));
//端点数+1
LengthOfLineRenderer++;
//设置线段的端点数
lineRenderer.positionCount = LengthOfLineRenderer;
}
if (Input.GetMouseButtonDown(0))
{
//将鼠标点击的屏幕坐标转换为世界坐标,然后存储到position中
//position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z));
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 1000))
{
position = hitInfo.point;
dianxia = position.x;
dianxiaweizhi = position;
}
}
//连续绘制线段
while (index < LengthOfLineRenderer)
{
//两点确定一条直线,所以我们依次绘制点就可以形成线段了
lineRenderer.SetPosition(index, position);
index++;
}
if (Input.GetKeyDown(KeyCode.Delete))
{
if (LineIndex > 0)
{
Destroy(GameObject.Find("Line" + --LineIndex));
}
}
if (Input.GetMouseButtonUp(0))
{
index = 0;
LengthOfLineRenderer = 0;
taiqi = position.x;
taiqiweizhi = position;
//设置宽度
lineRenderer.SetPositions(new Vector3[2]);
lineRenderer.positionCount = 2;
//设置指示线的起点和终点
lineRenderer.SetPosition(0, dianxiaweizhi);
lineRenderer.SetPosition(1, taiqiweizhi);
}
float ca = Mathf.Round((dianxiaweizhi - taiqiweizhi).magnitude);
if (ca.ToString()!=null)
{
changDu.text = ca.ToString(); // "长度:" + ca.ToString();
}
//if (LineIndex <= 1)
//{
// changDu.text = "";
//}
// print(dianxia - taiqi + "dianxia- taiqi");
}
public void guanbi()
{
changDu.text = "";
}
public void OnDisable()
{
print("dxbuswbdg");
LineIndex = 0;
//设置宽度
lineRenderer.SetPositions(new Vector3[0]);
}
}
新建一个空物体,添加line renderer组件,把脚本挂上就可以使用了
textpor ===》TextMeshProUGUI