123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using XRTool.Util;
- namespace XRTool.WorldUI
- {
- public enum ScaleType
- {
- UnChange = 0,
- Min = 1,
- Max = 2,
- Math
- }
- public class XRBoundLine : MonoBehaviour
- {
- [HideInInspector]
- public Transform[] horizontals;
- [HideInInspector]
- public Transform[] verticals;
- [HideInInspector]
- public Transform[] corners;
- /// <summary>
- /// 厚度比例
- /// </summary>
- private const float thicknessScale = 0.0145f;
- private const float BoundLen = 0.875f;
- private const float BoundCorner = 0.0625f;
- private float width;
- private float height;
- private float thickness;
- private bool isLocal = true;
- [HideInInspector]
- public ScaleType scaleType = ScaleType.Min;
- public Vector3 Scale
- {
- get
- {
- if (isLocal)
- {
- return transform.localScale;
- }
- return transform.lossyScale;
- }
- set
- {
- if (isLocal)
- {
- transform.localScale = value;
- }
- else
- {
- Vector3 scale = value;
- scale.x *= transform.localScale.x / transform.lossyScale.x;
- scale.y *= transform.localScale.y / transform.lossyScale.y;
- scale.z *= transform.localScale.z / transform.lossyScale.z;
- transform.localScale = value;
- }
- }
- }
- private void Awake()
- {
- width = transform.localScale.x;
- height = transform.localScale.y;
- thickness = transform.localScale.z;
- }
- /// <summary>
- /// 设置厚度
- /// </summary>
- public void SetThicknessScale(float thickness = thicknessScale)
- {
- Vector3 scale = transform.localScale;
- scale.z = thickness / thicknessScale;
- transform.localScale = scale;
- }
- /// <summary>
- /// 同步UI的背景框比例
- /// </summary>
- /// <param name="scale"></param>
- public void SetLine(Vector2 scale)
- {
- if (scale.x > 0 && scale.y > 0)
- {
- Vector3 tmp = transform.localScale;
- tmp.x = scale.x;
- tmp.y = scale.y;
- transform.localScale = tmp;
- Vector3 normalScale = Vector3.one;
- Vector3 h = Vector3.one;
- Vector3 v = Vector3.one;
- if (scaleType == ScaleType.UnChange)
- {
- }
- else if (scaleType == ScaleType.Min)
- {
- if (scale.x > scale.y)
- {
- normalScale.x = tmp.y / tmp.x;
- float bei = tmp.x / tmp.y;
- h.x = (BoundLen * bei / 2 + (bei * (1 - BoundLen) / 2 - BoundCorner)) / (BoundLen * bei / 2);
- }
- else
- {
- normalScale.y = tmp.x / tmp.y;
- float bei = tmp.y / tmp.x;
- v.y = (BoundLen * bei / 2 + (bei * (1 - BoundLen) / 2 - BoundCorner)) / (BoundLen * bei / 2);
- }
- }
- else if (scaleType == ScaleType.Max)
- {
- if (scale.x < scale.y)
- {
- normalScale.x = tmp.y / tmp.x;
- float bei = tmp.x / tmp.y;
- h.x = (BoundLen * bei / 2 + (bei * (1 - BoundLen) / 2 - BoundCorner)) / (BoundLen * bei / 2);
- }
- else
- {
- normalScale.y = tmp.x / tmp.y;
- float bei = tmp.y / tmp.x;
- v.y = (BoundLen * bei / 2 + (bei * (1 - BoundLen) / 2 - BoundCorner)) / (BoundLen * bei / 2);
- }
- }
- for (int i = 0; i < corners.Length; i++)
- {
- Transform corner = corners[i];
- corner.localScale = normalScale;
- }
- ///同步水平长度
- for (int i = 0; i < horizontals.Length; i++)
- {
- Transform horizontal = horizontals[i];
- horizontal.localScale = h;
- }
- ///同步垂直长度
- for (int i = 0; i < verticals.Length; i++)
- {
- Transform vertical = verticals[i];
- vertical.localScale = v;
- }
- }
- }
- /// <summary>
- /// 设置框的长宽
- /// </summary>
- /// <param name="width"></param>
- /// <param name="height"></param>
- public void SetLine(float width = 1, float height = 1, float thickness = thicknessScale)
- {
- if (thickness != this.thickness)
- {
- SetThicknessScale(thickness);
- this.thickness = thickness;
- }
- if (this.width != width || this.height != height)
- {
- ///比例相差不大,直接同比例缩放
- if (Mathf.Abs(width - height) < 0.0001f)
- {
- Vector3 scale = transform.localScale;
- scale.x = scale.y = width;
- transform.localScale = scale;
- }
- else
- {
- }
- this.width = width;
- this.height = height;
- }
- }
- }
- }
|