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;
///
/// 厚度比例
///
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;
}
///
/// 设置厚度
///
public void SetThicknessScale(float thickness = thicknessScale)
{
Vector3 scale = transform.localScale;
scale.z = thickness / thicknessScale;
transform.localScale = scale;
}
///
/// 同步UI的背景框比例
///
///
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;
}
}
}
///
/// 设置框的长宽
///
///
///
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;
}
}
}
}