实现半圆环状UI排版

2 篇文章 0 订阅

用于实现塔防游戏中,点击塔基后出现环形选项面板的效果
效果图:
这里写图片描述


    /// <summary>
    /// 中心点距离
    /// </summary>
    private float Radius = 0.5f;

    void Start()
    {
        RepostionCircle();
    }

    void RepostionCircle()
    {
        var childList = GetChildList();

        float baseAngle = 0, spaceAngle = 0;
        var centerPos = transform.position;
        if (childList.Count == 1)
        {
            baseAngle = -90;
            float x = centerPos.x + Radius * Mathf.Cos(baseAngle * 3.14f / 180f);
            float y = centerPos.y + Radius * Mathf.Sin(baseAngle * 3.14f / 180f);
            childList[0].transform.position = new Vector3(x, y, centerPos.z);
            return;
        }
        // 将所有子节点按照世界坐标轴X大小排序
        childList.Sort(SortUtils.SortByWorldposX);
        // 由于角度递减方式是采用顺时针方式显示的
        // 所以此处需要将list倒序
        childList.Reverse(0, childList.Count);
        baseAngle = -(180f / (childList.Count + 1));
        spaceAngle = (180f / (childList.Count + 1));
        for (int i = 0; i < childList.Count; i++)
        {
            float x = centerPos.x + Radius * Mathf.Cos(baseAngle * 3.14f / 180f);
            float y = centerPos.y + Radius * Mathf.Sin(baseAngle * 3.14f / 180f);
            childList[i].transform.position = new Vector3(x, y, centerPos.z);
            baseAngle -= spaceAngle;
        }
    }

    public List<Transform> GetChildList()
    {
        Transform myTrans = transform;
        List<Transform> list = new List<Transform>();

        for (int i = 0; i < myTrans.childCount; ++i)
        {
            Transform t = myTrans.GetChild(i);
            list.Add(t);
        }
        return list;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值