using System; using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEngine.UI; using XRTool.Util; namespace XRTool.WorldUI { public class XRImage : Image { private XRBoundLine boundLine; private RectTransform back; private RectTransform uIRoot; private Renderer front; /// /// 单位:分米 /// [Range(0, 10f)] public float thickness = 0.145f; //public Material mainMaterial; public bool isNeedFront = true; /// /// 是否需要背景板 /// public bool isNeedBg = false; public bool isAutoScale = true; private const float BackScale = 1.46f; public RectTransform Back { get => back; set => back = value; } protected override void Awake() { base.Awake(); InitComponent(); Back.gameObject.SetActive(isNeedBg); SetBoundThickness(); } public void InitComponent() { if (!boundLine || !Back || !front || !uIRoot) { front = UnityUtil.GetBreadthChild(transform, "Front"); boundLine = UnityUtil.GetBreadthChild(transform, "Bian"); Back = UnityUtil.GetBreadthChild(transform, "Back"); uIRoot = UnityUtil.GetBreadthChild(transform, "UIRoot"); } } public void SetBackActive() { if (Back && Back.gameObject.activeSelf != isNeedBg) { Back.gameObject.SetActive(isNeedBg); } } /// /// 更新边框的尺寸 /// /// public void UpdateSize(Vector2 size) { if (Back) { //rectTransform.sizeDelta = size; //rectTransform.localScale = Vector3.one / WorldDlg.UIScale; Back.sizeDelta = size * BackScale; boundLine.SetLine(size); Vector3 scale = size; scale.z = size.x > size.y ? size.x : size.y; front.transform.localScale = scale; uIRoot.sizeDelta = size; //uIRoot.localScale = Vector3.one / WorldDlg.UIScale; } } public void SetBoundThickness() { if (thickness <= 0) { boundLine.gameObject.SetActive(false); } else { if (!boundLine.gameObject.activeSelf) { boundLine.gameObject.SetActive(true); } float scale = thickness * 10 * WorldDlg.UIScale; boundLine.SetThicknessScale(scale); Vector3 boundPos = boundLine.transform.localPosition; boundPos.z = scale / 2; boundLine.transform.localPosition = boundPos; boundPos = Back.anchoredPosition3D; boundPos.z = scale; Back.anchoredPosition3D = boundPos; } } public void SetMainMater(Material mainMaterial) { if (front) { front.material = mainMaterial; } } public void SetFrontActive() { if (front && front.gameObject.activeSelf != isNeedFront) { front.gameObject.SetActive(isNeedFront); } } protected override void OnRectTransformDimensionsChange() { base.OnRectTransformDimensionsChange(); UpdateSize(rectTransform.rect.size); } } }