using System.Collections; using System.Collections.Generic; using UnityEngine; using ZIM; public class GameCrossHair : MonoBehaviour { GameController gameController; [SerializeField] ScreenLocate screenLocate; RectTransform myRTF; Vector2 targetPos; [SerializeField] int infraredSpotsIndex; Vector3? _lastPosition; void Start() { gameController = GetComponentInParent(); myRTF = transform as RectTransform; screenLocate = FindAnyObjectByType(); } void Update() { _lastPosition = myRTF.position; if (screenLocate) { Vector2? locateResult = null; InfraredSpot[] InfraredSpots = screenLocate.InfraredSpots; if (InfraredSpots != null && infraredSpotsIndex < InfraredSpots.Length && InfraredSpots[infraredSpotsIndex].ScreenUV != null) { Vector2 v2 = InfraredSpots[infraredSpotsIndex].ScreenUV.Value; if (v2.x > 0 && v2.y > 0 && v2.x < 1 && v2.y < 1) { locateResult = v2; } } gameObject.GetComponent().enabled = locateResult != null && gameController.crossHairIsOn; if (locateResult == null) { _lastPosition = null; return; } Vector2 np = ((Vector2)locateResult).pixelToLocalPosition_AnchorCenter(Vector2.one, (transform.parent as RectTransform).rect); if (Vector2.Distance(np, targetPos) >= 3) { targetPos = np; //插值移动 //myRTF.anchoredPosition = Vector2.Lerp(myRTF.anchoredPosition, targetPos, Time.deltaTime * 15); myRTF.anchoredPosition = targetPos; } } else { gameObject.GetComponent().enabled = gameController.crossHairIsOn; if (Input.GetMouseButtonDown(infraredSpotsIndex)) { transform.position = Input.mousePosition; } } if (gameController.gameNode.activeSelf == false) return; Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, (transform as RectTransform).rect.width / 2); foreach (var item in colliders) item.GetComponent()?.Bomb(); if (_lastPosition != null) { Vector3 lp = (Vector3)_lastPosition; float displace = Vector3.Distance(lp, myRTF.position); if (displace > 0.1) { Vector3 pointer = myRTF.position - lp; RaycastHit2D[] raycastHit2Ds = Physics2D.CircleCastAll(lp, (transform as RectTransform).rect.width / 2, pointer, displace); foreach (var item in raycastHit2Ds) item.transform.GetComponent()?.Bomb(); } } } }