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);
    }
}

运行即可看到效果

### Unity 中实现 App 页面之间跳转的方法 在 Unity 开发中,页面间的跳转可以通过多种方式来实现。具体取决于目标平台以及开发的需求场景。 #### 使用 `SceneManager` 切换场景 Unity 提供了一个强大的工具——Scene Management API 来管理不同场景之间的切换。通过加载不同的场景文件可以轻松完成页面间跳转的功能。以下是基本的代码示例: ```csharp using UnityEngine; using UnityEngine.SceneManagement; public class SceneSwitcher : MonoBehaviour { public void LoadNextScene() { int currentSceneIndex = SceneManager.GetActiveScene().buildIndex; if (currentSceneIndex + 1 < SceneManager.sceneCountInBuildSettings) SceneManager.LoadScene(currentSceneIndex + 1); } } ``` 上述代码展示了如何基于当前活动场景索引来加载下一个场景[^1]。 #### 使用 URL Scheme 或 Deep Linking 跳转至外部应用 如果需要从 Unity 应用程序内部跳转到其他应用程序(例如 App Store、淘宝等),则通常会采用 URL Scheme 或 Deeplinking 技术。下面是一个例子展示如何调用 iOS 的 App Store 并引导用户撰写评论: ```csharp #if UNITY_IPHONE || UNITY_EDITOR || UNITY_IOS const string APP_ID = "1361497551"; var url = string.Format( "itms-apps://itunes.apple.com/cn/app/id{0}?mt=8&action=write-review", APP_ID ); Application.OpenURL(url); #endif ``` 这段脚本仅适用于 iOS 设备,并且依赖于有效的 iTunes 链接结构[^2]。 对于 Android 和其他情况下的跨应用导航,则可能涉及更多复杂的配置过程,比如定义自定义协议或者利用 Intent 系统[^3]。 #### WebGL 上的网页重定向 当项目被导出为 Web 版本时,还可以考虑直接修改浏览器地址栏的方式来进行页面转换操作。这里给出一段针对 WebGL 构建类型的简单实例说明: ```javascript mergeInto(LibraryManager.library, { OpenPage: function (str) { let pageUrl = Pointer_stringify(str); window.location.href = pageUrl; }, }); ``` 此函数接受字符串参数作为新网址输入源,在执行过程中改变窗口位置属性从而达到重新定位的目的[^4]。 综上所述,根据实际应用场景的不同可以选择合适的技术手段去达成目的。无论是本地资源还是远程服务都可以找到相应的解决方案加以解决。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值