U_框选 UI
屏幕框选效果
using System.Collections.Generic;
using UnityEngine;
public class SelectionManager : MonoBehaviour {
// 框选 UI
public RectTransform selectionScreenCube;
// 延迟时间
public float delay;
// 是否长按
public bool isLongClicked = false;
private float btnDownTime;
private Vector3 clickPos;
private void Update() {
GetInput();
Interact();
}
private void GetInput() {
if (Input.GetMouseButtonDown(0)) {
// 按下时间
btnDownTime = Time.realtimeSinceStartup;
// 点击起始位置
clickPos = Input.mousePosition;
}
if (Input.GetMouseButton(0)) {
// 延迟时间
if (Time.realtimeSinceStartup > btnDownTime + delay) {
isLongClicked = true;
}
}
if (Input.GetMouseButtonUp(0)) {
selectionScreenCube.gameObject.SetActive(false);
btnDownTime = 0;
isLongClicked = false;
}
}
private void Interact() {
if (isLongClicked) {
DrawSelect();
}
}
private void DrawSelect() {
if (!selectionScreenCube.gameObject.activeInHierarchy) {
selectionScreenCube.gameObject.SetActive(true);
}
var nowPos = Input.mousePosition;
var sizeX = Mathf.Abs(clickPos.x - nowPos.x);
var sizeY = Mathf.Abs(clickPos.y - nowPos.y);
// UI 位置
selectionScreenCube.position = (clickPos + nowPos) / 2f;
// UI 大小
selectionScreenCube.sizeDelta = new Vector2(sizeX, sizeY);
}
}


浙公网安备 33010602011771号