делаю так:
с помощью selectRect.Contains(camera.WorldToScreenPoint(UM.Unitses[i].transform.position)) определяю есть ли в выбраном прямоугольнике обьекты из массива UM.Unitses и заношу их в другой массив.
вот полный код:
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UnitControl : MonoBehaviour
{
Vector3 poin1, point2;
//int layerMaskDefault=1<<0;
float height, width;
public Rect selectRect,allRect;
public Texture2D selectTextur;
public bool DragMouse;
public GameObject Selectr;
UnitManager UM;
void Start(){
UM=gameObject.GetComponent<UnitManager>();
}
void Update(){
// allRect=new Rect(0,0,Screen.width,Screen.height);
for(int t=0; 1000>t;t++)
{
//Debug.Log("PAROVOZIK TIR TIR TIR");
GameObject mad=GameObject.FindGameObjectWithTag("Unit");
if(mad){
mad.gameObject.tag="UnitMaked";
UM.Unitses.Add(mad);
}
else{
break;
}
}
if (Input.GetMouseButtonDown(0)){
poin1=Input.mousePosition;
Clear();
}
if (Input.GetMouseButton(0)){
point2=Input.mousePosition;
DragMouse=true;
}
if (Input.GetMouseButtonUp(0)){
DragMouse=false;
Select();
}
}
void OnGUI(){
if (DragMouse){
width=-poin1.x+point2.x;
height=(Screen.height-point2.y)-(Screen.height-poin1.y);
selectRect=new Rect(poin1.x,Screen.height-poin1.y,width,height);
GUI.DrawTexture(selectRect,selectTextur,ScaleMode.StretchToFill,true);
}
}
void Select(){
width=-poin1.x+point2.x;
height=(Screen.height-point2.y)-(Screen.height-poin1.y);
if (width<0){
width-=width+width;
}
if (height<0){
height-=height+height;
}
if(point2.x<poin1.x){
var x1=poin1.x;
var x2=point2.x;
poin1.x=x2;
point2.x=x1;
}
if(point2.y<poin1.y){
var y1=poin1.y;
var y2=point2.y;
poin1.y=y2;
point2.y=y1;
}
selectRect=new Rect(poin1.x,Screen.height-poin1.y,width,height);
for(int i=0; UM.Unitses.Count>i;i++){
if(selectRect.Contains(camera.WorldToScreenPoint(UM.Unitses[i].transform.position))){
Debug.Log("PAROVOZIK TIR TIR TIR");
UM.Unitses[i].gameObject.tag="UnitSelected";
//Instantiate(Selectr, UM.Unitses[i].transform.position, Quaternion.identity);
UM.UnitsesSelected.Add(UM.Unitses[i]);
}else{
Clear();
}
}
}
void Clear(){
for(int i=0; UM.UnitsesSelected.Count>i;i++){
UM.UnitsesSelected[i].gameObject.tag="UnitMaked";
//Instantiate(Selectr, UM.Unitses[i].transform.position, Quaternion.identity);
//UM.UnitsesSelected.Add(UM.Unitses[i]);
}
UM.UnitsesSelected.Clear();
}
}
using System.Collections;
using System.Collections.Generic;
public class UnitControl : MonoBehaviour
{
Vector3 poin1, point2;
//int layerMaskDefault=1<<0;
float height, width;
public Rect selectRect,allRect;
public Texture2D selectTextur;
public bool DragMouse;
public GameObject Selectr;
UnitManager UM;
void Start(){
UM=gameObject.GetComponent<UnitManager>();
}
void Update(){
// allRect=new Rect(0,0,Screen.width,Screen.height);
for(int t=0; 1000>t;t++)
{
//Debug.Log("PAROVOZIK TIR TIR TIR");
GameObject mad=GameObject.FindGameObjectWithTag("Unit");
if(mad){
mad.gameObject.tag="UnitMaked";
UM.Unitses.Add(mad);
}
else{
break;
}
}
if (Input.GetMouseButtonDown(0)){
poin1=Input.mousePosition;
Clear();
}
if (Input.GetMouseButton(0)){
point2=Input.mousePosition;
DragMouse=true;
}
if (Input.GetMouseButtonUp(0)){
DragMouse=false;
Select();
}
}
void OnGUI(){
if (DragMouse){
width=-poin1.x+point2.x;
height=(Screen.height-point2.y)-(Screen.height-poin1.y);
selectRect=new Rect(poin1.x,Screen.height-poin1.y,width,height);
GUI.DrawTexture(selectRect,selectTextur,ScaleMode.StretchToFill,true);
}
}
void Select(){
width=-poin1.x+point2.x;
height=(Screen.height-point2.y)-(Screen.height-poin1.y);
if (width<0){
width-=width+width;
}
if (height<0){
height-=height+height;
}
if(point2.x<poin1.x){
var x1=poin1.x;
var x2=point2.x;
poin1.x=x2;
point2.x=x1;
}
if(point2.y<poin1.y){
var y1=poin1.y;
var y2=point2.y;
poin1.y=y2;
point2.y=y1;
}
selectRect=new Rect(poin1.x,Screen.height-poin1.y,width,height);
for(int i=0; UM.Unitses.Count>i;i++){
if(selectRect.Contains(camera.WorldToScreenPoint(UM.Unitses[i].transform.position))){
Debug.Log("PAROVOZIK TIR TIR TIR");
UM.Unitses[i].gameObject.tag="UnitSelected";
//Instantiate(Selectr, UM.Unitses[i].transform.position, Quaternion.identity);
UM.UnitsesSelected.Add(UM.Unitses[i]);
}else{
Clear();
}
}
}
void Clear(){
for(int i=0; UM.UnitsesSelected.Count>i;i++){
UM.UnitsesSelected[i].gameObject.tag="UnitMaked";
//Instantiate(Selectr, UM.Unitses[i].transform.position, Quaternion.identity);
//UM.UnitsesSelected.Add(UM.Unitses[i]);
}
UM.UnitsesSelected.Clear();
}
}
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UnitManager : MonoBehaviour {
public List<GameObject> Unitses=new List<GameObject>();//Список для хранения всех юнитов
public List<GameObject> UnitsesSelected=new List<GameObject>();//Список для хранения выделеных юнитов
}
using System.Collections;
using System.Collections.Generic;
public class UnitManager : MonoBehaviour {
public List<GameObject> Unitses=new List<GameObject>();//Список для хранения всех юнитов
public List<GameObject> UnitsesSelected=new List<GameObject>();//Список для хранения выделеных юнитов
}
И вообще правильно ли управлять юнитами с помощью тегов?