首页 > 其他分享 >框选人物的例子+改变鼠标图标(类似红警)

框选人物的例子+改变鼠标图标(类似红警)

时间:2023-03-15 10:46:35浏览次数:44  
标签:红警 鼠标 transform GetChild Input 框选 图标 selects playerBox

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SetBox : MonoBehaviour
{
//这个是换鼠标图标
public Texture2D tex1, tex2;
RectTransform rectTransform;
Rect rect;
Transform playerBox;
Vector3 startPos, endPos;
Ray ray;
//被选择的人物集合
List<GameObject> selects = new List<GameObject>();
void Start()
{
//找到组件
rectTransform = GetComponent<RectTransform>();
//找到存储物品的盒子
playerBox = GameObject.Find("playerBox").transform;
}

// Update is called once per frame
void Update()
{
//记录鼠标左键按下时候的位置
if(Input.GetMouseButtonDown(0))
{
startPos = Input.mousePosition;
}
//鼠标左键按住
if (Input.GetMouseButton(0))
{
endPos = Input.mousePosition;
Vector3 centerPos = (startPos + endPos) / 2;
Vector3 localSize = new Vector2(Mathf.Abs(endPos.x - startPos.x), Mathf.Abs(endPos.y - startPos.y));
rectTransform.position = centerPos;
rectTransform.sizeDelta = localSize;

rect = new Rect(centerPos - localSize / 2, localSize);
}
//鼠标左键抬起
if(Input.GetMouseButtonUp(0))
{
foreach (var item in selects)
{
//item.transform.Find("lightRing").gameObject.SetActive(false);
//玩家移动脚本
//item.transform.GetComponent<PlayerMove>().enabled = false;
}
//清理集合
selects.Clear();
for (int i = 0; i < playerBox.childCount; i++)
{
Vector3 screenPos = Camera.main.WorldToScreenPoint(playerBox.GetChild(i).transform.position);
if (rect.Contains(screenPos))
{
selects.Add(playerBox.GetChild(i).gameObject);
//脚底的光圈
//playerBox.GetChild(i).transform.Find("lightRing").gameObject.SetActive(true);
//playerBox.GetChild(i).transform.GetComponent<PlayerMove>().enabled = true;
}
else
{
//playerBox.GetChild(i).transform.GetComponent<PlayerMove>().enabled = false;
//playerBox.GetChild(i).transform.Find("lightRing").gameObject.SetActive(false);
}
}
rectTransform.sizeDelta = Vector2.zero;
}
if (selects.Count > 0)
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
}

if (selects.Count > 0 && Physics.Raycast(ray, out RaycastHit hit, 1000, 1 << 7))
{
Cursor.SetCursor(tex1,Vector2.zero, CursorMode.Auto);
}
else
{
Cursor.SetCursor(tex2, Vector2.zero, CursorMode.Auto);
}
}
}

标签:红警,鼠标,transform,GetChild,Input,框选,图标,selects,playerBox
From: https://www.cnblogs.com/zanzz/p/17217492.html

相关文章