按钮绕某个点环形显示

  • 大地图点击物体显示的按钮呈环形显示

代码如下,只要将该脚本挂载在UI上就可以了,子物体是需要显示的按钮

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtonGroup : MonoBehaviour {

    public List<RectTransform> list;
    public float radius = 0;
    public float angle = 40;

    private float width = 0;
    private Vector3 dir = Vector3.zero;
    private float halfAngle = 20;
   
    void Awake () {
        list = new List<RectTransform>();
        Vector3 pos = transform.localPosition;
        Vector3 temp = new Vector3(pos.x, pos.y - 100, pos.z) - pos;
        dir = temp.normalized;
        halfAngle = angle / 2;
    }
    [SerializeField]
    private bool isOrder = false;

    public bool IsOrder
    {
        set
        {
            isOrder = value;
        }
    }

    public void AddList(RectTransform rect)
    {
        list.Add(rect);
        rect.SetParent(transform);
        rect.localScale = Vector3.one;
        rect.localRotation = Quaternion.identity;
        rect.localPosition = Vector3.zero;
        if (width == 0)
        {
            width = rect.GetRectSize().y / 2;
        }
        if (radius == 0)
        {
            radius = GetComponent<RectTransform>().GetRectSize().y / 2 - width;
        }
    }

    public void Refresh()
    {
        Vector3 pos = transform.localPosition;
        Vector3 temp = new Vector3(pos.x, pos.y - 100, pos.z) - pos;
        dir = temp.normalized;
        halfAngle = angle / 2;
        int count = list.Count;
        int middle = Mathf.CeilToInt(count / 2);
        int remainder = count % 2;
        for (int i = 0; i < list.Count; i++)
        {
            Vector3 s = Vector3.zero;
            int index = i + 1;
           
            if (remainder == 0)
            {   
                if (index <= middle)
                {
                    if (isOrder)
                    {
                        index=middle - index+1;
                    }
                    s = Quaternion.Euler(0, 0, -angle * index + halfAngle) * dir;
                }
                else
                {
                    index = index - middle;
                    s = Quaternion.Euler(0, 0, angle * index - halfAngle) * dir;
                }
            }
            else
            {
                if (index <= middle)
                {
                    if (isOrder)
                    {
                        index = middle - index + 1;
                    }
                    s = Quaternion.Euler(0, 0, -angle * index) * dir;
                }
                else if (index == middle + 1)
                {
                    s = Quaternion.Euler(0, 0, 0) * dir;
                }
                else if(index > middle + 1)
                {
                    index = index - middle - 1;
                    s = Quaternion.Euler(0, 0, angle * index) * dir;
                }
            }
            list[i].localPosition = (s * radius);
        }
    }

    public void OnValidate()
    {
        Refresh();
    }

    public void Clear()
    {
        list.Clear();
        width = 0;
    }

    private void OnDestroy()
    {
        foreach (var item in list)
        {
            GameObject.Destroy(item.gameObject);
        }
        list.Clear();
        width = 0;
    }

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值