using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SelectByRectangle1 : MonoBehaviour
{
public Color rectColor = Color.green;
public GameObject characterPrefab;
public static List<GameObject> characters = new List<GameObject>();
private List<GameObject> selectedCharacters = new List<GameObject>();
[HideInInspector]
public static bool MouseOverGUI = false;
private Vector3 downPosition = Vector3.zero;
public Material rectMat = null;
private bool drawRectangle = false;
private bool moved = false;
private float farPlane = 100;
// Use this for initialization
void Start()
{
rectMat.hideFlags = HideFlags.HideAndDontSave;
rectMat.shader.hideFlags = HideFlags.HideAndDontSave;
for (int i = 0; i < 3; i++)
{
GameObject obj = (GameObject)Instantiate(characterPrefab);
characters.Add(obj);
obj.transform.Rotate(0, Random.Range(-180, 180), 0);
obj.transform.Translate(Random.Range(-5, 5), 0, Random.Range(-5, 5));
}
}
//框选
void drawSelectionRectangle(Vector3 start, Vector3 end)
{
GL.PushMatrix();
if (!rectMat)
return;
rectMat.SetPass(0);
GL.LoadPixelMatrix();
GL.Begin(GL.QUADS);
GL.Color(new Color(rectColor.r, rectColor.g, rectColor.b, 0.1f));
GL.Vertex3(start.x, start.y, 0);
GL.Vertex3(end.x, start.y, 0);
GL.Vertex3(end.x, end.y, 0);
GL.Vertex3(start.x, end.y, 0);
GL.End();
GL.Begin(GL.LINES);
GL.Color(rectColor);
GL.Vertex3(start.x, start.y, 0);
GL.Vertex3(end.x, start.y, 0);
GL.Vertex3(end.x, start.y, 0);
GL.Vertex3(end.x, end.y, 0);
GL.Vertex3(end.x, end.y, 0);
GL.Vertex3(start.x, end.y, 0);
GL.Vertex3(start.x, end.y, 0);
GL.Vertex3(start.x, start.y, 0);
GL.End();
GL.PopMatrix();
}
void Update()
{
if (MouseOverGUI)
return;
if (Input.GetMouseButtonDown(0))
{
downPosition = Input.mousePosition;
moved = false;
return;
}
else if (Input.GetMouseButtonUp(0))
{
moved = false;
downPosition = Vector3.zero;
drawRectangle = false;
for(int i = 0;i < selectedCharacters.Count;i++)
{
print(selectedCharacters[i].name);
}
}
else if (Input.GetMouseButton(0) && !moved)
{
moved = true;
clearSelection();
drawRectangle = true;
return;
}
}
void addItem(GameObject obj)
{
if (!selectedCharacters.Contains(obj))
{
selectedCharacters.Add(obj);
//obj.SendMessage("doSelect");
//或者改变材质以及颜色
}
}
void removeItem(GameObject obj)
{
if (selectedCharacters.Contains(obj))
{
selectedCharacters.Remove(obj);
//obj.SendMessage("doDisselect");
//或者恢复材质以及颜色
}
}
void clearSelection()
{
for (int i = selectedCharacters.Count - 1; i >= 0; i--)
{
removeItem(selectedCharacters[i]);
}
}
void checkSelection(Vector3 start, Vector3 end)
{
Vector3 p1 = Vector3.zero;
Vector3 p2 = Vector3.zero;
if (start.x > end.x)
{
p1.x = end.x;
p2.x = start.x;
}
else
{
p1.x = start.x;
p2.x = end.x;
}
if (start.y > end.y)
{
p1.y = end.y;
p2.y = start.y;
}
else
{
p1.y = start.y;
p2.y = end.y;
}
foreach (GameObject obj in characters)
{
Vector3 location = GetComponent<Camera>().WorldToScreenPoint(obj.transform.position);
if (location.x < p1.x || location.x > p2.x || location.y < p1.y || location.y > p2.y
|| location.z < 0 || location.z > farPlane)
{
removeItem(obj);
//print("---" + obj.name);
}
else
{
addItem(obj);
//print("+++" + obj.name);
}
}
}
//程序每一帧渲染之后执行,GL相关的在这里调用
void OnPostRender()
{
if (drawRectangle)
{
Vector3 end = Input.mousePosition;
drawSelectionRectangle(downPosition, end);
checkSelection(downPosition, end);
}
}
}