using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CreateKuang : MonoBehaviour
{
Rect rect;
Vector3 startpos;
Vector3 endpos;
public GameObject playlist;
Camera ca;
List<GameObject> playlists = new List<GameObject>();
float anger;
// Start is called before the first frame update
void Start()
{
ca = Camera.main;
rect = this.GetComponent<RectTransform>().rect;
}
bool isdraw;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
startpos = Input.mousePosition;
isdraw = true;
}
if (isdraw)
{
float w = Mathf.Abs(startpos.x - Input.mousePosition.x);
float h = Mathf.Abs(startpos.y - Input.mousePosition.y);
GetComponent<RectTransform>().sizeDelta = new Vector2(w, h);
float x = startpos.x < Input.mousePosition.x ? startpos.x + w / 2 : Input.mousePosition.x + w / 2;
float y = startpos.y < Input.mousePosition.y ? startpos.y + h / 2 : Input.mousePosition.y + h / 2;
GetComponent<RectTransform>().anchoredPosition = new Vector2(x - Screen.width / 2, y - Screen.height / 2);
rect = new Rect(x-w/2, y-h/2, w, h);
}
if (Input.GetMouseButtonUp(0))
{
GetComponent<RectTransform>().sizeDelta = Vector2.zero;
isdraw = false;
for (int i = 0; i < playlist.transform.childCount; i++)
{
playlist.transform.GetChild(i).GetComponent<Player>().quan.SetActive(false);
}
playlists.Clear();
for (int i = 0; i < playlist.transform.childCount; i++)
{
Vector3 pos = ca.WorldToScreenPoint(playlist.transform.GetChild(i).position);
if (rect.Contains(pos))
{
playlists.Add(playlist.transform.GetChild(i).gameObject);
playlist.transform.GetChild(i).GetComponent<Player>().quan.SetActive(true);
}
}
}
if (Input.GetMouseButtonDown(1))
{
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out RaycastHit hit))
{
anger = (2 * Mathf.PI) / playlists.Count;
for (int i = 0; i < playlists.Count; i++)
{
float x = Mathf.Sin(anger * i) * 1;
float z = Mathf.Cos(anger * i) * 1;
if (playlists.Count <=2 )
{
playlists[i].GetComponent<Player>().an.SetDestination(hit.point);
}
else
{
playlists[i].GetComponent<Player>().an.SetDestination(hit.point + new Vector3(x, 0, z));
}
}
}
}
}
public void FaShe(Vector3 target)
{
for(int i = 0; i < playlists.Count; i++)
{
GameObject daodan = Instantiate(Resources.Load<GameObject>("Sphere"));
daodan.transform.position = playlists[i].transform.position;
daodan.GetComponent<daodan>().pos3 = target;
}
}
}
很多游戏都会用到框选以及群体导航移动,比如魔兽争霸,红警,导航控制小兵。
做框选的时候要创建一个Image,然后让他中间镂空,方法在下边
框选其实就是当你鼠标摁下的坐标为Image初始位置,然后计算你鼠标划出去的距离,转换为Image的长和宽高,当鼠标抬起的时候获取中心镂空内物体的坐标