您当前的位置: 首页 >  ui

unity工具人

暂无认证

  • 4浏览

    0关注

    205博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

UGUI 可循环展示的图集(列表)

unity工具人 发布时间:2021-09-26 21:40:04 ,浏览量:4

自动循环滚动,图片尺寸自适应 节点结构如下 在这里插入图片描述

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 ScrollView脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class ScrollViewScript : MonoBehaviour
{
    public Transform Content;
    public List Ctts = new List();

    public float MaxTime_ChangeImage;
    public float time;

    public Transform ReferencePoint;

    //public SerialNumber serial;

    [SerializeField]
    private int index;
    public int Index
    {
        get { return index; }
        set {
            if (value>= Content.childCount)
            {
                //print("value>= Content.childCount");
                value = 0;
            }            
            index = value;
            ChangeImage(index);                       
        }
    }
    private void Awake()
    {
        Content = transform.Find("Viewport/Content");
        ReferencePoint = transform.Find("ReferencePoint");
        //serial = transform.parent.Find("SerialNumber").GetComponent();
        ResetTime();
        InitCttSelfIndex();
    }
    private void OnEnable()
    {
        ResetAll();
    }


    private void Start()
    {      
        MaxTime_ChangeImage = Page0Script.Instance.MaxTime_C;
        ResetTime();
        print(Page0Script.Instance.MaxTime_C);
    }

    public void InitCttSelfIndex()
    {
        foreach (Transform ctt in Content)
        {
            Ctts.Add(ctt);
        }
    }  

    void FixedUpdate()
    {
        if (time > 0)
        {
            time -= Time.deltaTime;
        }
        else
        {
            //切换画面
            Index++;
            ResetTime();
        }
    }

    public void ResetTime()
    {
        time = MaxTime_ChangeImage;
    }

    //初始化
    private void ResetAll()
    {
        //计数器计时器图片定位
        index = 0;
        ResetTime();
        StartCoroutine(ResetIndex002());
    }

    //图片切换
    public void ChangeImage(int i)
    {
        StartCoroutine(ResetIndex(0.2f));       
    }


    /// 
    /// 1.把子物体列表中的第一个移至末尾 2.同时当前显示图片会前移一个单位我们需要让content闪现把画面复原 3.然后再进行向左移动的操作
    /// 
    /// 
    /// 
    IEnumerator ResetIndex(float t)
    {
        if (Ctts[index].GetSiblingIndex() == Ctts.Count-1)
        {
            //闪现距离
            float D_idx3 = ReferencePoint.position.x - Content.GetChild(Content.childCount-3).position.x;
            //刷新顺序
            Content.GetChild(0).SetSiblingIndex(Ctts.Count - 1);
            //闪现
            float TPx = Content.position.x + D_idx3;
            Content.position = new Vector3(TPx, Content.position.y, Content.position.z);
        }
        yield return new WaitForEndOfFrame();
        //用索引找对应的图片,计算距离并位移
        float d = ReferencePoint.position.x - Ctts[index].position.x;
        float TragetPoint = Content.position.x + d;
        Content.DOMoveX(TragetPoint, t);
    }

    //初始化专用
    IEnumerator ResetIndex002()
    {
        if (Ctts[index].GetSiblingIndex() == Ctts.Count - 1)
        {
            //闪现距离
            float D_idx3 = ReferencePoint.position.x - Content.GetChild(1).position.x;
            //刷新顺序
            Content.GetChild(0).SetSiblingIndex( Ctts.Count - 1);
            //闪现
            float TPx = Content.position.x + D_idx3;            
            Content.position = new Vector3(TPx, Content.position.y, Content.position.z);
        }
        yield return new WaitForEndOfFrame();
        //用索引找对应的图片,计算距离并位移
        float d = ReferencePoint.position.x - Ctts[index].position.x;
        float TragetPoint = Content.position.x + d;
        Content.DOMoveX(TragetPoint, 0F);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

Ctt脚本
public class CttScript : MonoBehaviour
    public int SelfIndex;

MTexture脚本

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

/// 
/// 图片参考父物体大小等比例缩放
/// 
public class X_RectAutoSize : MonoBehaviour
{
    //原始尺寸
    private Vector2 olSize;
    //缩放后的尺寸
    private Vector2 size;
    //原始尺寸宽高比
    private float al;
    private RectTransform self;

    //是否锁定位置
    public bool lockPos = true;
    
    internal float ReferHeight;
    internal float ReferWidth;

    //父物体尺寸
    private Vector2 parentSize;

    //边框厚度
    public float FrameThickness;

    void Update()
    {
        SetWidthHight();
    }

    //设置宽高
    public void SetWidthHight()
    {
        self = GetComponent();       
        //父物体尺寸
        parentSize = self.parent.GetComponent().rect.size;
        ReferHeight = parentSize.y - FrameThickness;
        ReferWidth = parentSize.x - FrameThickness;
        self.GetComponent().SetNativeSize();
        olSize = self.sizeDelta;
        al = olSize.x / olSize.y;

        //决定以宽或高为标准
        if (olSize.x             
关注
打赏
1656671177
查看更多评论
0.0512s