| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- 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<GameController>();
- myRTF = transform as RectTransform;
- screenLocate = FindAnyObjectByType<ScreenLocate>();
- }
- 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<UnityEngine.UI.Image>().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<UnityEngine.UI.Image>().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<Box>()?.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<Box>()?.Bomb();
- }
- }
- }
- }
|