扇形分布(Circle Layout Group)(圆形平均分布)

using UnityEngine;
using UnityEngine.UI;

/// <summary>扇形布局组件</summary>
/// <author>Danny Yan</author>
[AddComponentMenu("Layout/Circle Layout Group", 150)]
public class CircleLayoutGroup : LayoutGroup
{
    public enum LayoutMode
    {
        /// <summar>平均分布</summary>
        Hypodispersion = 0,
        /// <summar>扇形分布</summary>
        Sector = 1,
    }

    [Header("分布模式: Hypodispersion(圆形平均分布) Sector(扇形分布)")]
    public LayoutMode mode = LayoutMode.Hypodispersion;

    [Header("半径")]
    public float radius = 0;

    [Header("起始角度")]
    public float initAngle = 0;

    [Header("是否保持弧度值不变")]
    public bool keepRadLen = false;
    [Header("弧度保持不变的值(keepRadLen为true时有效)")]
    public float keepRadLenVal = 0f;
    [Header("扇形分布范围")]
    public float sectorAngle = 0;
    [Header("扇形分布时且keepRadLen为false时,是否中间对齐到扇形中心,否则两端对齐")]
    public bool sectorAlignCenter = false;
    [Header("扇形分布且sectorAlignCenter为false时,是否为逆时针")]
    public bool sectorClockwise = true;
    [Header("圆心")]
    public Vector2 circleCenter;

    // public float fDistance;
    // [Range(0f, 360f)]
    // public float MinAngle, MaxAngle, StartAngle;
    protected override void OnEnable()
    {
        base.OnEnable();
        CalculateRadial();
    }
    public override void SetLayoutHorizontal()
    {
        // Util.Print("SetLayoutHorizontal");
        CalculateRadial();
    }
    public override void SetLayoutVertical()
    {
        // Util.Print("SetLayoutVertical");
        CalculateRadial();
    }
    public override void CalculateLayoutInputHorizontal()
    {
        base.CalculateLayoutInputHorizontal();
        // Util.Print("CalculateLayoutInputHorizontal");
        CalculateRadial();
    }
    public override void CalculateLayoutInputVertical()
    {
        // Util.Print("CalculateLayoutInputVertical");
        CalculateRadial();
    }
#if UNITY_EDITOR
    protected override void OnValidate()
    {
        base.OnValidate();
        CalculateRadial();
    }
#endif

    protected void CalculateRadial()
    {
        this.m_Tracker.Clear();
        if (transform.childCount == 0)
            return;

        if (this.mode == LayoutMode.Hypodispersion)
        {
            this.Hypodispersion();
        }
        else if (this.mode == LayoutMode.Sector)
        {
            this.Sector();
        }
    }

    /// 平均分布
    private void Hypodispersion()
    {
        // rectChildren来自父类,在CalculateLayoutInputHorizontal()中进行的非active和IgonreLayout的子节点剔除
        if (this.rectChildren.Count <= 0) return;

        float perRad = 2 * Mathf.PI / rectChildren.Count;
        // float perAngle = 360 / rectChildren.Count;
        float initRad = this.initAngle * Mathf.Deg2Rad;

        this.SetLayout(initRad, perRad);
    }

    /// 扇形分布
    private void Sector()
    {
        if (rectChildren.Count <= 0) return;

        float perRad = 0;
        float initRad = this.initAngle * Mathf.Deg2Rad;
        // 扇形弧度
        var sectorRad = this.sectorAngle * Mathf.Deg2Rad;

        if (this.keepRadLen)
        {
            // 弧长公式为: L = (angle * PI / 180) * radius = rad * radius, 要保持弧长不变,则需要改变rad即可, rad = L / radius
            perRad = keepRadLenVal / this.radius;
            if (sectorAlignCenter)
            {
                // 将initRad重置到sectorRad中线上
                initRad += (sectorClockwise ? sectorRad * .5f : -sectorRad * .5f);
                if (rectChildren.Count > 1)
                {
                    // 根据数量重置initRad
                    float _radOff = perRad * ((rectChildren.Count - 1) * 0.5f);
                    initRad -= (sectorClockwise ? _radOff : -_radOff);
                }
            }
            else
            {
                perRad = keepRadLenVal / this.radius;
            }
        }
        else
        {
            // 居中对齐到扇形中心
            if (sectorAlignCenter)
            {
                perRad = sectorRad / (rectChildren.Count + 1);
                initRad += sectorClockwise ? perRad : -perRad;
            }
            else
            {
                perRad = rectChildren.Count == 1 ? 0 : sectorRad / (rectChildren.Count - 1);
            }
        }

        if (!sectorClockwise)
        {
            perRad *= -1;
        }

        this.SetLayout(initRad, perRad);
    }

    private void SetLayout(float initRad, float perRad)
    {
        // 计算最佳大小
        float totalMin = 0;
        float totalPreferred = 0;
        float totalFlexible = 0;

        float minX = float.MaxValue;
        float maxX = float.MinValue;
        float minY = float.MaxValue;
        float maxY = float.MinValue;
        for (int i = 0; i < rectChildren.Count; i++)
        {
            var child = rectChildren[i];

            //禁用子节点recttransform相关属性
            m_Tracker.Add(this, child,
            DrivenTransformProperties.Anchors |
            DrivenTransformProperties.AnchoredPosition |
            DrivenTransformProperties.Pivot);

            var size = child.rect.size;
            child.pivot = new Vector2(0.5f, 0.5f);
            child.anchorMin = new Vector2(0.5f, 0.5f);
            child.anchorMax = new Vector2(0.5f, 0.5f);
            child.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x);
            child.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y);

            size *= 0.5f * child.localScale;
            Vector3 vPos = child.localPosition;
            var rad = initRad + perRad * i;
            vPos.x = circleCenter.x + this.radius * Mathf.Cos(rad);
            vPos.y = circleCenter.y + this.radius * Mathf.Sin(rad);
            child.localPosition = vPos;

            var left = vPos.x - size.x;
            if (left < minX) minX = left;
            var right = vPos.x + size.x;
            if (right > maxX) maxX = right;

            var bottom = vPos.y - size.y;
            if (bottom < minY) minY = bottom;
            var top = vPos.y + size.y;
            if (top > maxY) maxY = top;
        }

        // 此处宽高计算并不是很精确
        var w = Mathf.Abs(maxX - minX);
        var h = Mathf.Abs(maxY - minY);
        totalMin = this.radius;
        totalPreferred = w;
        if (this.mode == LayoutMode.Sector)
        {
            // totalPreferred += radius;
        }
        SetLayoutInputForAxis(totalMin, totalPreferred, totalFlexible, 0);
        totalPreferred = h;
        if (this.mode == LayoutMode.Sector)
        {
            // totalPreferred += radius;
        }
        SetLayoutInputForAxis(totalMin, totalPreferred, totalFlexible, 1);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A_Ankh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值