unity3D 鼠标点击拖动画线
varmyColor:Color;varfirstPosition:Vector3;varsecondPosition:Vector3;varmiddlePosition...
var myColor:Color;
var firstPosition:Vector3;
var secondPosition:Vector3;
var middlePosition:Vector3;
var isClicked:boolean=false;
private var lineRenderer:LineRenderer;
function Start()
{
lineRenderer=gameObject.AddComponent(LineRenderer);
lineRenderer.material.color=myColor;
lineRenderer.SetWidth(0.1,0.1);
}
function Update()
{
var isMouseDown:boolean=Input.GetMouseButton(0);
if(isMouseDown &&!isClicked)
{
firstPosition=new Vector3(Input.mousePosition.x,Input.mousePositon.y,1);
lineRenderer.SetVertexCount(1);
lineRenderer.enabled=true;
lineRenderer.SetPosition(0,firstPosition);
isClicked=false;
}
else if(isMouseDown)
{
secondPosition=new Vector3(Input.mousePosition.x,Input.mousePositon.y,1);
lineRenderer.SetVertexCount(2);
lineRenderer.SetPosition(1,secondPosition);
}
if(Input.GetMouseButtonUp(0))
{
isClicked=false;
secondPosition=new Vector3(Input.mousePosition.x,Input.mousePositon.y,1);
lineRenderer.SetVertexCount(2);
lineRenderer.SetPosition(1,secondPosition);
}
}
这是我的代码,我估计是Update那里的点坐标没有记录下来。。可是不知道是不是…… 展开
var firstPosition:Vector3;
var secondPosition:Vector3;
var middlePosition:Vector3;
var isClicked:boolean=false;
private var lineRenderer:LineRenderer;
function Start()
{
lineRenderer=gameObject.AddComponent(LineRenderer);
lineRenderer.material.color=myColor;
lineRenderer.SetWidth(0.1,0.1);
}
function Update()
{
var isMouseDown:boolean=Input.GetMouseButton(0);
if(isMouseDown &&!isClicked)
{
firstPosition=new Vector3(Input.mousePosition.x,Input.mousePositon.y,1);
lineRenderer.SetVertexCount(1);
lineRenderer.enabled=true;
lineRenderer.SetPosition(0,firstPosition);
isClicked=false;
}
else if(isMouseDown)
{
secondPosition=new Vector3(Input.mousePosition.x,Input.mousePositon.y,1);
lineRenderer.SetVertexCount(2);
lineRenderer.SetPosition(1,secondPosition);
}
if(Input.GetMouseButtonUp(0))
{
isClicked=false;
secondPosition=new Vector3(Input.mousePosition.x,Input.mousePositon.y,1);
lineRenderer.SetVertexCount(2);
lineRenderer.SetPosition(1,secondPosition);
}
}
这是我的代码,我估计是Update那里的点坐标没有记录下来。。可是不知道是不是…… 展开
1个回答
展开全部
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public Material mat;
public Color color = Color.red;
public Vector3 pos1;
public Vector3 pos2;
public bool isReady = false;
void Start()
{
mat.color = color;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
pos1 = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
pos2 = Input.mousePosition;
isReady = true;
}
}
void OnPostRender()
{
if (isReady)
{
GL.PushMatrix();
mat.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);
GL.Color(color);
GL.Vertex3(pos1.x/Screen.width, pos1.y/Screen.height, pos1.z);
GL.Vertex3(pos2.x / Screen.width, pos2.y / Screen.height, pos2.z);
GL.End();
GL.PopMatrix();
}
}
}
希望能够帮到你
using System.Collections;
public class Test : MonoBehaviour
{
public Material mat;
public Color color = Color.red;
public Vector3 pos1;
public Vector3 pos2;
public bool isReady = false;
void Start()
{
mat.color = color;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
pos1 = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
pos2 = Input.mousePosition;
isReady = true;
}
}
void OnPostRender()
{
if (isReady)
{
GL.PushMatrix();
mat.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);
GL.Color(color);
GL.Vertex3(pos1.x/Screen.width, pos1.y/Screen.height, pos1.z);
GL.Vertex3(pos2.x / Screen.width, pos2.y / Screen.height, pos2.z);
GL.End();
GL.PopMatrix();
}
}
}
希望能够帮到你
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询