Unity實現(xiàn)引導頁效果
本文實例為大家分享了Unity實現(xiàn)引導頁效果的具體代碼,供大家參考,具體內容如下
效果圖:
1、創(chuàng)建Canvas,設置RenderMode=ScreenSpace-Overlay,UIScaleMode = ScaleWithScreenSize,
ReferenceResolution(x=1080,y=1920)
2、創(chuàng)建一個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); } }
運行即可看到效果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#去掉字符串中所有匹配的字符String.Replace方法
在C#中,如果你想要去掉字符串中所有匹配的字符,你可以使用String.Replace方法,本文主要介紹了C#去掉字符串中所有匹配的字符String.Replace方法,具有一定的參考價值,感興趣的可以了解一下2024-04-04C#使用TCP協(xié)議實現(xiàn)數(shù)據(jù)發(fā)送和接受的方法
這篇文章主要介紹了c#使用TCP協(xié)議實現(xiàn)數(shù)據(jù)發(fā)送和接受,使用TCP協(xié)議實現(xiàn)數(shù)據(jù)的發(fā)送和接受包括客戶端和服務端兩個部分,本文通過實例代碼介紹的非常詳細,需要的朋友可以參考下2024-04-04C#+無unsafe的非托管大數(shù)組示例詳解(large unmanaged array in c# without ‘u
這篇文章主要給大家介紹了關于C#+無unsafe的非托管大數(shù)組(large unmanaged array in c# without 'unsafe' keyword)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01Unity UGUI LayoutRebuilder自動重建布局介紹及使用
這篇文章主要為大家介紹了Unity UGUI LayoutRebuilder自動重建布局介紹及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07