1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- using UnityEngine.UIElements;
- public class ZhaMenIconCtrl : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
- {
- private RectTransform rect;
- private Camera _camera;
- private UnityEngine.UI.Button _button;
- public FloodGate _data;
- public Transform targetTransForm;
- public GameObject tint;
- public Text tintName;
- public void Init(FloodGate data,string name,GameObject targetObj)
- {
- _data = data;
- tintName.text = name;
- rect = this.GetComponent<RectTransform>();
- _button = this.transform.Find("Icon").GetComponent<UnityEngine.UI.Button>();
- _button.onClick.AddListener(() =>
- {
-
- });
- rect.anchoredPosition = new Vector2(2000, 0);
- _camera = CameraManager.instance.mainCamera;
- targetTransForm = targetObj.transform;
- }
- 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;
- }
- public void OnPointerEnter(PointerEventData eventData)
- {
- tint.gameObject.SetActive(true);
- LayoutRebuilder.ForceRebuildLayoutImmediate(tintName.GetComponent<RectTransform>()); // 强制重新计算布局
- LayoutRebuilder.ForceRebuildLayoutImmediate(tint.GetComponent<RectTransform>()); // 强制重新计算布局
- }
- public void OnPointerExit(PointerEventData eventData)
- {
- tint.gameObject.SetActive(false);
- }
- }
|