123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 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;
- /// <summary>
- /// 单位:分米
- /// </summary>
- [Range(0, 10f)]
- public float thickness = 0.145f;
- //public Material mainMaterial;
- public bool isNeedFront = true;
- /// <summary>
- /// 是否需要背景板
- /// </summary>
- 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<Renderer>(transform, "Front");
- boundLine = UnityUtil.GetBreadthChild<XRBoundLine>(transform, "Bian");
- Back = UnityUtil.GetBreadthChild<RectTransform>(transform, "Back");
- uIRoot = UnityUtil.GetBreadthChild<RectTransform>(transform, "UIRoot");
- }
- }
- public void SetBackActive()
- {
- if (Back && Back.gameObject.activeSelf != isNeedBg)
- {
- Back.gameObject.SetActive(isNeedBg);
- }
- }
- /// <summary>
- /// 更新边框的尺寸
- /// </summary>
- /// <param name="size"></param>
- 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);
- }
- }
- }
|