栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

Unity实现引导页效果

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity实现引导页效果

本文实例为大家分享了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
{
  /// 
  /// 可移动的最大最小X轴坐标
  /// 
  private float minX, maxX;
  /// 
  /// 开始触摸时,算出偏移值,防止跳变
  /// 
  private float offsetX;

  /// 
  /// 灵敏度
  /// 
  private float sensitivityX;
  /// 
  /// 当前显示第几页
  /// 
  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);
  }
}

运行即可看到效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/122169.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号