ObsIconCtrl.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. using UnityEngine.UIElements;
  8. public class ObsIconCtrl : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
  9. {
  10. private RectTransform rect;
  11. private Camera _camera;
  12. private UnityEngine.UI.Button _button;
  13. public ObsData _data;
  14. public Transform targetTransForm;
  15. public GameObject tint;
  16. public Text tintName;
  17. public void Init(ObsData data)
  18. {
  19. _data = data;
  20. tintName.text = data.name;
  21. rect = this.GetComponent<RectTransform>();
  22. _button = this.transform.Find("Icon").GetComponent<UnityEngine.UI.Button>();
  23. if (!data.status)
  24. {
  25. this.transform.Find("Icon").GetComponent<UnityEngine.UI.Image>().color = Color.red;
  26. this.transform.Find("Icon").GetChild(0).GetComponent<UnityEngine.UI.Image>().color = Color.red;
  27. }
  28. else
  29. {
  30. _button.onClick.AddListener(() =>
  31. {
  32. if (GlobalData.pageIndex == PageIndex.Page1)
  33. {
  34. GCJKLayer._Instance.ShowObsPlayerPanel(_data);
  35. }
  36. if (GlobalData.pageIndex == PageIndex.Page3)
  37. {
  38. SPJKLayer._Instance.ShowObsPanel(_data);
  39. }
  40. });
  41. }
  42. rect.anchoredPosition = new Vector2(2000, 0);
  43. _camera = CameraManager.instance.mainCamera;
  44. targetTransForm=StaticLod.instance.GetStaticObj(_data.targetName).transform;
  45. }
  46. void Update()
  47. {
  48. if (targetTransForm != null)
  49. {
  50. if (IsObjectInCameraView(targetTransForm, _camera))
  51. {
  52. rect.transform.position=_camera.WorldToScreenPoint(targetTransForm.position);
  53. }
  54. else
  55. {
  56. rect.transform.position = new Vector3(2000, 0, 0);
  57. }
  58. }
  59. }
  60. bool IsObjectInCameraView(Transform objectTransform, Camera camera)
  61. {
  62. Vector3 objectScreenPosition = camera.WorldToScreenPoint(objectTransform.position);
  63. return objectScreenPosition.z > 0 &&
  64. objectScreenPosition.x > 0 &&
  65. objectScreenPosition.x < Screen.width &&
  66. objectScreenPosition.y > 0 &&
  67. objectScreenPosition.y < Screen.height;
  68. }
  69. public void OnPointerEnter(PointerEventData eventData)
  70. {
  71. tint.gameObject.SetActive(true);
  72. LayoutRebuilder.ForceRebuildLayoutImmediate(tintName.GetComponent<RectTransform>()); // 强制重新计算布局
  73. LayoutRebuilder.ForceRebuildLayoutImmediate(tint.GetComponent<RectTransform>()); // 强制重新计算布局
  74. }
  75. public void OnPointerExit(PointerEventData eventData)
  76. {
  77. tint.gameObject.SetActive(false);
  78. }
  79. }