Unity制作引导页效果

效果图:
这里写图片描述

1、创建Canvas,设置RenderMode=ScreenSpace-Overlay,UIScaleMode = ScaleWithScreenSize,
ReferenceResolution(x=1080,y=1920)
2、创建一个RawImage,命名为(parentGoImg),并做如下设置,
这里写图片描述
3、在parentGoImg下建几个RawImage,赋予想展示的图片,并做如下设置
这里写图片描述
4、添加如下脚本给parentGoImg

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

public class Asd : MonoBehaviour,IBeginDragHandler, IDragHandler,IEndDragHandler
{
    /// <summary>
    /// 可移动的最大最小X轴坐标
    /// </summary>
    private float minX, maxX;
    /// <summary>
    /// 开始触摸时,算出偏移值,防止跳变
    /// </summary>
    private float offsetX;

    /// <summary>
    /// 灵敏度
    /// </summary>
    private float sensitivityX;
    /// <summary>
    /// 当前显示第几页
    /// </summary>
    private int currentShowIndex = 1;

    private void Start()
    {
        (transform as RectTransform).pivot = new Vector2(0, 0.5f);
        Debug.Log(Screen.width + "   " + Screen.height);
        for (int i = 0; i < transform.childCount; i++)
        {
            (transform.GetChild(i) as RectTransform).sizeDelta = new Vector2(0, 0);
            //canvas的RenderMode要设置成Overlay形式
            //这里i*1080是因为canvas的UIScaleMode设置成了ScaleWithScreenSize,Resolution为x=1080,y=1920
            //如果canvas的UIScaleMode设置成ConstantPixelSize则吧这里的i*1080改成i*Screen.width
            (transform.GetChild(i) as RectTransform).anchoredPosition = new Vector2(i * 1080.0f, 0);
        }

        minX = -((transform.childCount - 1) * Screen.width);
        maxX = 0.0f;
        //如果移动超过页面的五分之一,则切换页面
        sensitivityX = Screen.width / 5;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        offsetX = transform.position.x - Input.mousePosition.x;
    }

    public void OnDrag(PointerEventData eventData)
    {
        //将物体坐标限制在最大最小X轴坐标内
        transform.position = new Vector2(Input.mousePosition.x + offsetX, transform.position.y);
        if (transform.position.x <= minX)
        {
            transform.position = new Vector2(minX, transform.position.y);
        }
        else if (transform.position.x >= maxX)
        {
            transform.position = new Vector2(maxX, transform.position.y);
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        //判断坐标,是否需要切换页面
        if (transform.position.x > GetLeftX())
        {
            currentShowIndex--;
        }
        else if (transform.position.x < GetRightX())
        {
            currentShowIndex++;
        }
        transform.DOMoveX(-(currentShowIndex - 1) * Screen.width, 0.2f);
    }

    float GetLeftX() {
        return -((currentShowIndex - 1) * Screen.width - sensitivityX);
    }

    float GetRightX() {
        return -((currentShowIndex - 1) * Screen.width + sensitivityX);
    }
}

运行即可看到效果

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值