LineRenderer线渲染器主要是用于3D中渲染线段,在这里要注意LineRenderer渲染出的线段的两个端点是3D世界中的点,即它是属于世界坐标(World Space)中的。
虽然我们也可以使用GL图像库来渲染线段,但是使用LineRenderer我们可以对线段进行更多的操作,例如:设置颜色,宽度等。在这里要注意LineRenderer渲染出的线段的两个端点是3D世界中的点,即他是属于世界坐标(World Point)中的。
LineRenderer通过组件形成存在的,首先我们新建一个空的Game Object,然后我们选择“ Component→Effects→Line Renderer”,即可添加LineRenderer组件了。
其实我们也可以通过脚本来为此添加LineRenderer组件:
LineRenderer lineRenderer = gameObject.AddComponent <LineRenderer>();
获取LineRenderer组件:
lineRenderer = GetComponent <LineRenderer>();
【案例】根据鼠标左击的位置,来持续重新行线段
首先我们在场景中新建一个空的GameObject,并重置一下。然后将Script1脚本添加给他。
使用UnityEngine;
使用System.Collections;
使用System.Collections.Generic;
// [RequireComponent(typeof(LineRenderer))]
公共类DrawLine:MonoBehaviour {
private LineRenderer line;
私人布尔isMousePressed;
私有列表<Vector3> pointsList;
私有Vector3 mousePos;
//私有Vector3 m_DownCamPos;
//私有Vector3 m_mouseDownStartPos;
//主相机坐标下
///线点的
结构struct myLine {
public Vector3 StartPoint;
公共Vector3端点;
};
// -----------------------------------
void Awake(){_
Init();
}
私人void _Init(){
如果(m_init)返回;
m_init = true;
//创建线渲染器组件并设置其属性
// line = this.GetComponent <LineRenderer>();
line = gameObject.AddComponent <LineRenderer>();
line.material =新Material(Shader.Find(“ Particles / Additive”));
line.SetVertexCount(0);
line.SetWidth(0.1f,0.1f);
line.SetColors(Color.green,Color.green);
line.useWorldSpace = true;
isMousePressed = false;
pointsList =新列表<Vector3>();
line.sortingLayerName =“忽略射线广播”;
line.sortingOrder = 999;
// renderer.material.SetTextureOffset(
// m_mainCam = this.GetComponent <Camera>();
}
bool m_init = false;
//相机m_mainCam;
// ---------------- -------------------
void Update(){
//如果按下鼠标键,则删除旧行,
如果(Input.GetMouseButtonDown(1)){
isMousePressed = true;
line.SetVertexCount(0);
pointsList.RemoveRange(0,pointsList.Count);
line.SetColors(Color.green,Color.green);
// m_DownCamPos = Camera.main.transform.position;
// m_mouseDownStartPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,Camera.main.nearClipPlane 1f));
}否则,如果(Input.GetMouseButtonUp(1)){
isMousePressed = false;
}
// if(Input.GetMouseButton(0)|| Input.GetMouseButton(1)|| Input.GetMouseButton(2)){
// Vector3 crelpos = Camera.main.transform.position;
// for(int i = 0; i <pointsList.Count; i){
// line.SetPosition(i,crelpos pointsList [i]);
// //line.SetPosition(pointsList.Count-1,(Vector3)pointsList [pointsList.Count-1]);
// //}
//}
//鼠标移动时绘制线条(按)
if(isMousePressed){
Vector3 crelpos = Camera.main.transform.position;
//将鼠标点击的屏幕坐标转换为世界坐标,然后存储到位置中
mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,Camera.main.nearClipPlane 1f));
Vector3 offsetpos = mousePos-crelpos;
// Vector3 hitp = Vector3.zero;
// bool ok = GetRayCastZero2DPlanePoint(Camera.main,Camera.main.transform.TransformPoint(0f,0f,Camera.main.nearClipPlane 1f).z,out hitp);
// mousePos = hitp;
如果(!pointsList.Contains(offsetpos)){
pointsList.Add(offsetpos);
line.SetVertexCount(pointsList.Count);
for(int i = 0; i <pointsList.Count; i){
line.SetPosition(i,crelpos pointsList [i]);
//line.SetPosition(pointsList.Count-1,(Vector3)pointsList [pointsList.Count-1]);
}
如果(isLineCollide()){
isMousePressed = false;
line.SetColors(Color.red,Color.red);
}
}
}
}
// -----------------------------------
//下面的方法检查是currentLine(最后两个画的线点)与线碰撞
// // -----------------------------------
私人布尔isLineCollide(){
如果( pointsList.Count <2)返回false;
int TotalLines = pointsList.Count-1;
myLine [] lines =新的myLine [TotalLines];
if(TotalLines> 1){
for(int i = 0; i <TotalLines; i){
lines [i] .StartPoint =(Vector3)pointsList [i];
lines [i] .EndPoint =(Vector3)pointsList [i 1];
}(
)
for(int i = 0; i <TotalLines-1; i){
myLine currentLine;
currentLine.StartPoint =(Vector3)pointsList [pointsList.Count-2];
currentLine.EndPoint =(Vector3)pointsList [pointsList.Count-1];
如果(isLinesIntersect(lines [i],currentLine))返回true;
}
返回false;
}
// -----------------------------------
//以下方法检查给定的两个点是否相同不是
// -----------------------------------
private bool checkPoints(Vector3 pointA,Vector3 pointB){
返回(pointA.x == pointB.x && pointA.y == pointB.y);
}
// -----------------------------------
//以下方法检查给定的两条线是否相交
//------------------------------------
private bool isLinesIntersect(myLine L1,myLine L2){
if(checkPoints(L1.StartPoint,L2.StartPoint)|| checkPoints(L1.StartPoint,L2.EndPoint)|| checkPoints(L1.EndPoint,L2.StartPoint)|| checkPoints( L1.EndPoint,L2.EndPoint))返回false;
return(((Mathf.Max(L1.StartPoint.x,L1.EndPoint.x)> = Mathf.Min(L2.StartPoint.x,L2.EndPoint.x)))&&(Mathf.Max(L2.StartPoint.x, L2.EndPoint.x)> = Mathf.Min(L1.StartPoint.x,L1.EndPoint.x))&&(Mathf.Max(L1.StartPoint.y,L1.EndPoint.y)> = Mathf.Min(L2 .StartPoint.y,L2.EndPoint.y))&&(Mathf.Max(L2.StartPoint.y,L2.EndPoint.y)> = Mathf.Min(L1.StartPoint.y,L1.EndPoint.y))) ;
}
}
转载声明:本文来源于网络,不作任何商业用途
IOS下载
安卓下载
小程序