当前位置: 首页 > article >正文

⭐ Unity 横向滑动列表 首尾相连 轮转图

效果如下:

场景挂载:

代码部分:

using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Printing;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Create2dLzt : MonoBehaviour, IDragHandler, IEndDragHandler
{
    public Image prefab;
    float r;
    float c;
    float ang;
    float space = 400;//间距
    float n = 7;

    public List<Image> Images = new List<Image>();
    public List<Transform> lists = new List<Transform>();

    float moveAng = 0;

    /// <summary>
    /// 缩放最大小值
    /// </summary>
    float max = 3;
    float min = 1f;


    // Start is called before the first frame update
    void Start()
    {
        c = (space + prefab.rectTransform.rect.width) * n;
        r = c / (2 * Mathf.PI);
        ang = (2 * Mathf.PI) / n;

        Move();
    }

    private void Move()
    {
        for (int i = 0; i < n; i++)
        {
            if (Images.Count <= i)
            {
                Images.Add(Instantiate(prefab, transform));
                Images[i].sprite = Resources.Load<Sprite>("Img_" + i);
                lists.Add(Images[i].transform);
                Images[i].name = "Img_" + i;
            }
            float x = Mathf.Sin(ang * i + moveAng) * r;
            float z = Mathf.Cos(ang * i + moveAng) * r;
            float p = (r + z) / (2 * r);
            float scale = (max - min) * p + min;
            Images[i].transform.localScale = Vector3.one * scale;
            Images[i].rectTransform.anchoredPosition = new Vector2(x, 0);
        }

        lists.Sort((a, b) =>
        {
            if (a.localScale.x < b.localScale.x)
            {
                return -1;
            }
            else if (a.localScale.x == b.localScale.x)
            {
                return 0;
            }
            else
            {
                return 1;
            }

        });

        for (int i = 0; i < lists.Count; i++)
        {
            lists[i].SetSiblingIndex(i);
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        float dis = eventData.delta.x;
        float ang = dis / r;
        moveAng += ang;
        Move();
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        float dis = eventData.delta.x;
        float time = Mathf.Abs(dis) / 100;
        DOTween.To((a) =>
        {
            float ang = a / r;
            moveAng += ang;
            Move();
        }, dis, 0, time).OnComplete(() =>
        {
            Align();
        });
    }

    private void Align()
    {
        float ang = Mathf.Asin(lists[lists.Count - 1].localPosition.x / r);
        float dis = ang * r;
        float time = Mathf.Abs(dis) / 100;
        DOTween.To((a) =>
        {
            moveAng = a;
            Move();
        }, moveAng, moveAng - ang, time);
    }
}


http://www.kler.cn/a/554109.html

相关文章:

  • Rust 面试题
  • 机器学习·数据处理
  • 【Python爬虫(17)】突破爬虫IP限制,解锁数据抓取新姿势
  • 【Scrapy】Scrapy教程4——命令行工具
  • 实现一个专注应用-后端开发(一)-搭建
  • QML Image 圆角设置
  • 从猜想终结到算法革新,弹性哈希开启数据存储新篇章
  • docker run --ipc=host参数含义
  • UniApp 面试题 超基础
  • C++效率掌握之STL库:vector函数全解
  • ubuntu 创建交换分区 或者扩容交换分区
  • 鸿蒙中,UIAbility组件启动模式(3种分别是Singleton(单实例模式)Multiton(多实例模式)Specified(指定实例模式))
  • Python常见面试题的详解13
  • 解决 Nginx 代理后 HTTP 头部丢失的问题:以 access_token 为例
  • 【ISO 14229-1:2023 UDS诊断(会话控制0x10服务)测试用例CAPL代码全解析⑤】
  • rust学习五、认识所有权
  • unity学习47:寻路和导航,unity2022后版本如何使用 Navmesh 和 bake
  • 图解MySQL【日志】——Buffer Pool
  • Java Applet 学习笔记(详细版)
  • Redis 过期键(expires)机制详解