using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; using UnityEngine.UI; /// <summary> /// /// </summary> public class SetBox : MonoBehaviour { Vector3 starPos; public Camera cam; public Transform playerbox;//人物所在父级 bool isDrap = false; RectTransform rectTransform; public float p = 0.1f; public float max = 5; List<GameObject> selects = new List<GameObject>(); // Start is called before the first frame update void Start() { rectTransform = GetComponent<RectTransform>(); } // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { starPos = Input.mousePosition; isDrap = true; } if (isDrap) { Vector2 size = new Vector2(Mathf.Abs(Input.mousePosition.x - starPos.x), Mathf.Abs(Input.mousePosition.y - starPos.y)); rectTransform.sizeDelta = size; float mixX = (Input.mousePosition.x < starPos.x ? Input.mousePosition.x : starPos.x) - Screen.width / 2; float mixY = (Input.mousePosition.y < starPos.y ? Input.mousePosition.y : starPos.y) - Screen.height / 2; rectTransform.anchoredPosition = new Vector2(mixX, mixY); } if (Input.GetMouseButtonUp(0)) { isDrap = false; SeclePlayer(); rectTransform.sizeDelta = Vector2.zero; } if (Input.GetMouseButtonDown(1)) { Ray ray = cam.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray,out RaycastHit hit)) { if (selects.Count > 0) { float c = 2 * selects.Count; float r = c / (Mathf.PI * 2); float ang = Mathf.PI * 2 / selects.Count; for (int i = 0; i < selects.Count; i++) { float x = Mathf.Sin(ang * i) * r + hit.point.x; float z = Mathf.Cos(ang * i) * r + hit.point.z; float noise = Mathf.PerlinNoise((x + 50) * p, (z + 50) * p); float y = noise * max; selects[i].GetComponent<NavMeshAgent>().SetDestination(new Vector3(x, y, z)); } } } } } public void SeclePlayer() { selects.Clear(); for (int i = 0; i < playerbox.childCount; i++) { Vector3 pos = cam.WorldToScreenPoint(playerbox.GetChild(i).position); Rect rect = new Rect(rectTransform.anchoredPosition + new Vector2(Screen.width / 2, Screen.height / 2), rectTransform.sizeDelta); if (rect.Contains(new Vector2(pos.x, pos.y))) { playerbox.GetChild(i).Find("ring").gameObject.SetActive(true); selects.Add(playerbox.GetChild(i).gameObject); } else { playerbox.GetChild(i).Find("ring").gameObject.SetActive(false); } } } }