ShuiWeiIconCtrl.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class ShuiWeiIconCtrl : MonoBehaviour
  6. {
  7. private RectTransform rect;
  8. private Camera _camera;
  9. public ShuiWeiData _data;
  10. private Text valueText;
  11. private Button histroyButton;
  12. public Transform targetTransForm;
  13. public void Init(ShuiWeiData data)
  14. {
  15. _camera = CameraManager.instance.mainCamera;
  16. _data = data;
  17. rect = this.GetComponent<RectTransform>();
  18. rect.anchoredPosition = new Vector2(2000, 0);
  19. valueText = this.transform.Find("valueText").GetComponent<Text>();
  20. valueText.text = $"{data.value} <size=12><color=FFFFFF>m</color></size>";
  21. histroyButton = this.transform.Find("histroyButton").GetComponent<Button>();
  22. histroyButton.onClick.RemoveAllListeners();
  23. histroyButton.onClick.AddListener(() =>
  24. {
  25. GCJKLayer._Instance.ShowHistoryPanle(_data);
  26. });
  27. }
  28. void Update()
  29. {
  30. if (targetTransForm != null)
  31. {
  32. if (IsObjectInCameraView(targetTransForm, _camera))
  33. {
  34. rect.transform.position=_camera.WorldToScreenPoint(targetTransForm.position);
  35. }
  36. else
  37. {
  38. rect.transform.position = new Vector3(2000, 0, 0);
  39. }
  40. }
  41. }
  42. bool IsObjectInCameraView(Transform objectTransform, Camera camera)
  43. {
  44. Vector3 objectScreenPosition = camera.WorldToScreenPoint(objectTransform.position);
  45. return objectScreenPosition.z > 0 &&
  46. objectScreenPosition.x > 0 &&
  47. objectScreenPosition.x < Screen.width &&
  48. objectScreenPosition.y > 0 &&
  49. objectScreenPosition.y < Screen.height;
  50. }
  51. }