2025-11-03 10:59:33 +08:00

151 lines
4.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
using UnityEngine.EventSystems;
namespace BF
{
public abstract class BFLayoutBase : MonoBehaviour
{
[SerializeField] private float left;
[SerializeField] private float right;
[SerializeField] private float top;
[SerializeField] private float bottom;
public float Left
{
get { return left; }
set
{
SetProperty(ref left, value);
}
}
public float Right
{
get { return right; }
set
{
SetProperty(ref right, value);
}
}
public float Top
{
get { return top; }
set
{
SetProperty(ref top, value);
}
}
public float Bottom
{
get { return bottom; }
set
{
SetProperty(ref bottom, value);
}
}
[SerializeField] protected TextAnchor childAlignment = TextAnchor.UpperLeft;
public TextAnchor ChildAlignment
{
get { return childAlignment; }
set
{
SetProperty(ref childAlignment, value);
}
}
[System.NonSerialized] private RectTransform rect;
protected RectTransform RectTransform
{
get
{
if (rect == null)
rect = GetComponent<RectTransform>();
return rect;
}
}
protected DrivenRectTransformTracker tracker;
[System.NonSerialized] private List<RectTransform> rectChildren = new List<RectTransform>();
protected List<RectTransform> RectChildren { get { return rectChildren; } }
protected bool isDirty = false;
protected Vector2 leftUpAnchor = new Vector2(0 ,1);
protected void SetDirty()
{
isDirty = true;
}
protected void SetProperty<T>(ref T currentValue, T newValue)
{
if ((currentValue == null && newValue == null) || (currentValue != null && currentValue.Equals(newValue)))
return;
currentValue = newValue;
SetDirty();
}
protected void OnEnable()
{
SetDirty();
}
protected void SetChildAlongAxis(RectTransform rect, int axis, float pos, float size = 0f)
{
if (rect == null)
return;
// tracker.Add(this, rect,
// DrivenTransformProperties.Anchors |
// (axis == 0 ?
// (DrivenTransformProperties.AnchoredPositionX | DrivenTransformProperties.SizeDeltaX) :
// (DrivenTransformProperties.AnchoredPositionY | DrivenTransformProperties.SizeDeltaY)
// )
// );
tracker.Add(this, rect,
DrivenTransformProperties.Anchors |
(axis == 0 ?
DrivenTransformProperties.AnchoredPositionX :
DrivenTransformProperties.AnchoredPositionY
)
);
rect.anchorMin = leftUpAnchor;
rect.anchorMax = leftUpAnchor;
if (axis == 0)
{
if (size > 0)
{
rect.anchoredPosition = new Vector2(pos + size / 2, rect.anchoredPosition.y);
}
else
{
rect.anchoredPosition = new Vector2(pos + rect.sizeDelta.x * rect.localScale.x / 2, rect.anchoredPosition.y);
}
}
else
{
if (size > 0)
{
rect.anchoredPosition = new Vector2(rect.anchoredPosition.x, - pos - size / 2 * rect.localScale.y);
}
else
{
rect.anchoredPosition = new Vector2(rect.anchoredPosition.x, -pos - rect.sizeDelta.y * rect.localScale.y / 2);
}
}
}
}
}