using System.Collections; using System.Collections.Generic; using UnityEngine; public class ZhaMenIcon : MonoBehaviour { public string targetName; private RectTransform rect; private Camera _camera; public Transform targetTransForm; public void Awake() { rect = this.GetComponent(); rect.anchoredPosition = new Vector2(2000, 0); _camera = ModelCameraCtrl._Instance._camera; GameObject[] targets = GameObject.FindGameObjectsWithTag("BuYuanZhaMen"); for (int i = 0; i < targets.Length; i++) { if (targets[i].name.Equals(targetName)) { targetTransForm=targets[i].transform; break; } } } void Update() { if (targetTransForm != null) { if (IsObjectInCameraView(targetTransForm, _camera)) { rect.transform.position=_camera.WorldToScreenPoint(targetTransForm.position); } else { rect.transform.position = new Vector3(2000, 0, 0); } } } bool IsObjectInCameraView(Transform objectTransform, Camera camera) { Vector3 objectScreenPosition = camera.WorldToScreenPoint(objectTransform.position); return objectScreenPosition.z > 0 && objectScreenPosition.x > 0 && objectScreenPosition.x < Screen.width && objectScreenPosition.y > 0 && objectScreenPosition.y < Screen.height; } }