Unity UGUI 按钮呈半圆型滑动的无限列表 可左右镜像排列

   上一篇是实现了按钮可拖拽并自动吸附在屏幕两侧,接着呈半圆分布。之前是道具按钮比较少的情况,道具按钮一多,屏幕空间又局促的情况下,按钮就要适当隐藏了,所以做成一个可以滑动的列表。滑动的轨迹是一个圆形。这个需求还挺有意思的,记录一下代码:

                                          

using DG.Tweening;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
namespace ArrowLegend
{
    /// <summary>
    /// 按钮自动吸附和呈半圆分布
    /// </summary>
    public class AutoAdsorptiontoEdge : MonoBehaviour, IBeginDragHandler, IDragHandler,  IEndDragHandler
    {
        private RectTransform rectTransform;
        
        
        public RectTransform SlidingZone;//旋转滑动区域
        public RectTransform Mask;//遮罩区域
        public RectTransform RightMask;//右遮罩区域
        public RectTransform LeftMask;//左遮罩区域
        //public RectTransform RightSlidingZone;//右滑动区域
        //public RectTransform LeftSlidingZone;//左滑动区域
        private CircleSliding circleSliding;
        private Button[] propButtons;
        private Button CenterBtn;
        private bool isRight;
        private float LeftAdsorptiontoX;//吸附到的位置,根据屏幕判断
        private float RightAdsorptiontoX;
        private float UpAdsorptiontoY;//上下位置判断,防止出现按钮到屏幕外的情况
        private float DownAdsorptionY;
        //private List<Button> currentPropBtnList = new List<Button> ();//当前正在展示的按钮
        private int showNum = 4;//展示个数
        
        void Start()
        {
            rectTransform = GetComponent<RectTransform>();
            circleSliding = SlidingZone.GetComponent<CircleSliding>();
            propButtons = circleSliding.PropBtns;
            CenterBtn = circleSliding.CenterPos.GetComponent<Button>();
        }
        public void OnBeginDrag(PointerEventData data)
        {
            if (!new Rect(0, 0, Screen.width,  Screen.height).Contains(Input.mousePosition)) return;
            for (int i = 0; i < propButtons.Length; i++)
            {
             
                propButtons[i].gameObject.SetActive(false);
            }
            Vector3 leftPos;
            RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, new  Vector2(Screen.width/15f, Screen.height/15f), data.enterEventCamera, out leftPos);
            LeftAdsorptiontoX = leftPos.x;
            DownAdsorptionY = leftPos.y;
           
            Vector3 rightPos;
            RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, new  Vector2(Screen.width/15f*14, Screen.height/15f*14), data.enterEventCamera, out rightPos);
            RightAdsorptiontoX = rightPos.x;
            UpAdsorptiontoY = rightPos.y;
        }
        public void OnDrag(PointerEventData data)
        {
            
            Vector3 pos;
            //限定在屏幕内操作
            if (!new Rect(0, 0, Screen.width,  Screen.height).Contains(Input.mousePosition)) return;
           
            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform,  data.position, data.enterEventCamera, out pos))
            {
               
                rectTransform.position = pos;
                //for (int i = 0; i < PropButtons.Length; i++)
                //{
                //
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Unity中使用UGUI动态创建按钮列表非常简单,以下是一个基本的步骤: 1.创建一个空的GameObject作为容器,用于存储所有的按钮。 2.编写脚本来创建按钮。你可以使用UnityUI.Button组件或自定义按钮。 3.在脚本中,使用循环来创建多个按钮,并将它们添加到容器中。 4.为每个按钮设置位置、大小、文本和其他属性。 5.为每个按钮添加事件处理程序,以便在单击按钮时执行相应的操作。 以下是一个示例脚本,它将创建一个包含5个按钮列表: ``` using UnityEngine; using UnityEngine.UI; public class ButtonList : MonoBehaviour { public GameObject buttonPrefab; //按钮预制件 public int numberOfButtons; //按钮数量 private void Start() { for (int i = 0; i < numberOfButtons; i++) { GameObject button = Instantiate(buttonPrefab) as GameObject; //创建按钮 button.transform.SetParent(transform, false); //将按钮添加到容器中 button.GetComponentInChildren<Text>().text = "Button " + (i + 1); //设置按钮文本 button.GetComponent<Button>().onClick.AddListener(() => { Debug.Log("Button " + (i + 1) + " clicked"); }); //添加事件处理程序 } } } ``` 这个脚本假定你有一个名为buttonPrefab的按钮预制件,并且你想要创建5个按钮。在Start()方法中,我们使用for循环创建每个按钮,并将它们添加到容器中。我们还设置了每个按钮的文本,并为它们添加了一个单击事件处理程序,以便在单击按钮时输出一条消息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值