public Material mat;//默认的材质球
private Vector2 FirstMousePosition;
private Vector2 SecondMousePosition;
private bool StartRender;
private GameObject[] planes;
// Use this for initialization
void Start () {
planes = GameObject.FindGameObjectsWithTag("Planes");
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
StartRender = true;
FirstMousePosition = Input.mousePosition;
PickGameObject();
}
if (Input.GetMouseButtonUp(0))
{
StartRender = false;
changTwoPoint();
PickGameObject();
FirstMousePosition = Vector2.zero;
}
SecondMousePosition = Input.mousePosition;
}
void PickGameObject()
{
foreach (GameObject tt in planes)
{
Vector3 position = Camera.main.WorldToScreenPoint(tt.GetComponent<Renderer>().transform.position);
if (position.x >= FirstMousePosition.x & position.x <= SecondMousePosition.x & position.y >= FirstMousePosition.y & position.y <= SecondMousePosition.y)
{
tt.GetComponent<Renderer>().material.color = Color.black;
}
else
tt.GetComponent<Renderer>().material.color = Color.white;
}
}
void changTwoPoint()
{
if (FirstMousePosition.x>SecondMousePosition.x)
{
float position1 = FirstMousePosition.x;
FirstMousePosition.x = SecondMousePosition.x;
SecondMousePosition.x = position1;
}
if (FirstMousePosition.y>SecondMousePosition.y)
{
float position2 = FirstMousePosition.y;
FirstMousePosition.y = SecondMousePosition.y;
SecondMousePosition.y = position2;
}
}
void OnPostRender()
{
if (StartRender)
{
mat.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);
DrawLine(FirstMousePosition.x,FirstMousePosition.y,SecondMousePosition.x,SecondMousePosition.y);
GL.End();
}
}
void DrawLine(float x1,float y1,float x2,float y2)
{
GL.Vertex(new Vector3(x1 / Screen.width, y1 / Screen.height, 0));
GL.Vertex(new Vector3(x2 / Screen.width, y2 / Screen.height, 0));
GL.Vertex(new Vector3(x2 / Screen.width, y1 / Screen.height, 0));
GL.Vertex(new Vector3(x2 / Screen.width, y2 / Screen.height, 0));
GL.Vertex(new Vector3(x2 / Screen.width, y2 / Screen.height, 0));
GL.Vertex(new Vector3(x1 / Screen.width, y2 / Screen.height, 0));
GL.Vertex(new Vector3(x1 / Screen.width, y2 / Screen.height, 0));
GL.Vertex(new Vector3(x1 / Screen.width, y1 / Screen.height, 0));
}
鼠标勾选框,框内的物体变颜色
最新推荐文章于 2023-10-07 00:44:21 发布