XRImage.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using XRTool.Util;
  8. namespace XRTool.WorldUI
  9. {
  10. public class XRImage : Image
  11. {
  12. private XRBoundLine boundLine;
  13. private RectTransform back;
  14. private RectTransform uIRoot;
  15. private Renderer front;
  16. /// <summary>
  17. /// 单位:分米
  18. /// </summary>
  19. [Range(0, 10f)]
  20. public float thickness = 0.145f;
  21. //public Material mainMaterial;
  22. public bool isNeedFront = true;
  23. /// <summary>
  24. /// 是否需要背景板
  25. /// </summary>
  26. public bool isNeedBg = false;
  27. public bool isAutoScale = true;
  28. private const float BackScale = 1.46f;
  29. public RectTransform Back { get => back; set => back = value; }
  30. protected override void Awake()
  31. {
  32. base.Awake();
  33. InitComponent();
  34. Back.gameObject.SetActive(isNeedBg);
  35. SetBoundThickness();
  36. }
  37. public void InitComponent()
  38. {
  39. if (!boundLine || !Back || !front || !uIRoot)
  40. {
  41. front = UnityUtil.GetBreadthChild<Renderer>(transform, "Front");
  42. boundLine = UnityUtil.GetBreadthChild<XRBoundLine>(transform, "Bian");
  43. Back = UnityUtil.GetBreadthChild<RectTransform>(transform, "Back");
  44. uIRoot = UnityUtil.GetBreadthChild<RectTransform>(transform, "UIRoot");
  45. }
  46. }
  47. public void SetBackActive()
  48. {
  49. if (Back && Back.gameObject.activeSelf != isNeedBg)
  50. {
  51. Back.gameObject.SetActive(isNeedBg);
  52. }
  53. }
  54. /// <summary>
  55. /// 更新边框的尺寸
  56. /// </summary>
  57. /// <param name="size"></param>
  58. public void UpdateSize(Vector2 size)
  59. {
  60. if (Back)
  61. {
  62. //rectTransform.sizeDelta = size;
  63. //rectTransform.localScale = Vector3.one / WorldDlg.UIScale;
  64. Back.sizeDelta = size * BackScale;
  65. boundLine.SetLine(size);
  66. Vector3 scale = size;
  67. scale.z = size.x > size.y ? size.x : size.y;
  68. front.transform.localScale = scale;
  69. uIRoot.sizeDelta = size;
  70. //uIRoot.localScale = Vector3.one / WorldDlg.UIScale;
  71. }
  72. }
  73. public void SetBoundThickness()
  74. {
  75. if (thickness <= 0)
  76. {
  77. boundLine.gameObject.SetActive(false);
  78. }
  79. else
  80. {
  81. if (!boundLine.gameObject.activeSelf)
  82. {
  83. boundLine.gameObject.SetActive(true);
  84. }
  85. float scale = thickness * 10 * WorldDlg.UIScale;
  86. boundLine.SetThicknessScale(scale);
  87. Vector3 boundPos = boundLine.transform.localPosition;
  88. boundPos.z = scale / 2;
  89. boundLine.transform.localPosition = boundPos;
  90. boundPos = Back.anchoredPosition3D;
  91. boundPos.z = scale;
  92. Back.anchoredPosition3D = boundPos;
  93. }
  94. }
  95. public void SetMainMater(Material mainMaterial)
  96. {
  97. if (front)
  98. {
  99. front.material = mainMaterial;
  100. }
  101. }
  102. public void SetFrontActive()
  103. {
  104. if (front && front.gameObject.activeSelf != isNeedFront)
  105. {
  106. front.gameObject.SetActive(isNeedFront);
  107. }
  108. }
  109. protected override void OnRectTransformDimensionsChange()
  110. {
  111. base.OnRectTransformDimensionsChange();
  112. UpdateSize(rectTransform.rect.size);
  113. }
  114. }
  115. }