XRImage.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if (front)
  69. {
  70. front.transform.localScale = scale;
  71. }
  72. uIRoot.sizeDelta = size;
  73. //uIRoot.localScale = Vector3.one / WorldDlg.UIScale;
  74. }
  75. }
  76. public void SetBoundThickness()
  77. {
  78. if (thickness <= 0)
  79. {
  80. boundLine.gameObject.SetActive(false);
  81. }
  82. else
  83. {
  84. if (!boundLine.gameObject.activeSelf)
  85. {
  86. boundLine.gameObject.SetActive(true);
  87. }
  88. float scale = thickness * 10 * WorldDlg.UIScale;
  89. boundLine.SetThicknessScale(scale);
  90. Vector3 boundPos = boundLine.transform.localPosition;
  91. boundPos.z = scale / 2;
  92. boundLine.transform.localPosition = boundPos;
  93. boundPos = Back.anchoredPosition3D;
  94. boundPos.z = scale;
  95. Back.anchoredPosition3D = boundPos;
  96. }
  97. }
  98. public void SetMainMater(Material mainMaterial)
  99. {
  100. if (front)
  101. {
  102. front.material = mainMaterial;
  103. }
  104. }
  105. public void SetFrontActive()
  106. {
  107. if (front && front.gameObject.activeSelf != isNeedFront)
  108. {
  109. front.gameObject.SetActive(isNeedFront);
  110. }
  111. }
  112. protected override void OnRectTransformDimensionsChange()
  113. {
  114. base.OnRectTransformDimensionsChange();
  115. UpdateSize(rectTransform.rect.size);
  116. }
  117. }
  118. }