首页 技术 正文
技术 2022年11月15日
0 收藏 484 点赞 3,150 浏览 4502 个字

一、 点击滑动页面

新建了一个带mask的prefab,加上代码只需要将图片prefab、按钮prefab和所想添加的图片

拖进去会自动生成按钮,滑动速度可以随意调time,滑动效果用itween实现的,所以需要加上itween插件

效果如下:(图片是我最爱的马路小天使(¯﹃¯))

unity3d之实现各种滑动效果unity3d之实现各种滑动效果

附上代码

 using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI; public class Mask : MonoBehaviour { public List<Sprite> sprite = new List<Sprite>();
List<GameObject> image=new List<GameObject >();
public GameObject pic;
public Button but;
public float time;
float width, height;
int num;
void Start ()
{
num = sprite.Count;
width = this.gameObject.GetComponent<RectTransform>().rect.width;
height = this.gameObject.GetComponent<RectTransform>().rect.height;
pic.transform.GetComponent<RectTransform>().sizeDelta=new Vector2(width,height);
for (int i = ; i < num; i++)
{ GameObject p = Instantiate(pic,new Vector3(transform.position.x+i*width,transform.position.y,),transform.rotation)as GameObject;
p.transform.parent = this.gameObject.transform;
image.Add (p) ;
p.GetComponent<Image>().sprite = sprite[i];
Button b = Instantiate(but, new Vector3(transform.position.x- ( * num - ) / +*i, transform.position.y - height/ + , ), transform.rotation) as Button;
b.transform.parent = GameObject.FindWithTag("Button").transform;
System.Object obj = (System.Object)i;
b.onClick.AddListener(delegate(){this.MoveToPic((int)obj);});
} }
void OnGUI()
{
if (GUI.Button(new Rect(transform.position.x + + width / , Screen.height - transform.position.y - , , ), ">"))
{
if (image[].transform.position.x < transform.position.x-)
{
Move();
}
}
if (GUI.Button(new Rect(transform.position.x -width/-,Screen.height- transform.position.y- , , ), "<"))
{
if (image[num - ].transform.position.x > transform.position.x+)
{
Move(-);
}
}
}
public void Move(int dir)
{
for (int i = ; i <num; i++)
{
iTween.MoveAdd(image[i], iTween.Hash("x", width * dir, "time", time));
}
}
public void MoveToPic(int i)
{ float offset = transform.position.x - image[i].transform.position.x;
for (int j = ; j < num; j++)
{
iTween.MoveAdd(image[j], iTween.Hash("x", offset, "time", time));
}
} }

二、间隔时间自动滑动,也可点击滑动

 using UnityEngine;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System; public class ScrollPage : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
ScrollRect rect;
//页面:0,1,2,3 索引从0开始
//每页占的比列:0/3=0 1/3=0.333 2/3=0.6666 3/3=1
//float[] pages = { 0f, 0.333f, 0.6666f, 1f };
List<float> pages = new List<float>();
int currentPageIndex = -; //滑动速度
public float smooting = ; //滑动的起始坐标
float targethorizontal = ; //是否拖拽结束 0 -结束 1-拖拽中 2-结束超过5s
int isDrag = ;
int curindex = ;
int dir = ;
float timecount = ;
/// <summary>
/// 用于返回一个页码,-1说明page的数据为0
/// </summary>
public System.Action<int,int> OnPageChanged; float startime = 0f;
float delay = 0.1f; // Use this for initialization
void Start()
{
rect = transform.GetComponent<ScrollRect>();
//rect.horizontalNormalizedPosition = 0;
//UpdatePages();
startime = Time.time;
} void Update()
{
if (Time.time < startime + delay) return;
UpdatePages();
//如果不判断。当在拖拽的时候要也会执行插值,所以会出现闪烁的效果
//这里只要在拖动结束的时候。在进行插值
if (isDrag == && pages.Count > )
{
rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, Time.deltaTime * smooting);
if (Mathf.Abs(rect.horizontalNormalizedPosition - targethorizontal) <= 0.001)
{ isDrag = ; timecount = ; }
}
if (isDrag == )
{
timecount += Time.deltaTime;
if (timecount > )
{
AutoMove();
rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, pages[curindex + dir * ], Time.deltaTime * smooting);
if (pages == null || OnPageChanged == null)
print("Error" + "," + (pages==null) + "," + (OnPageChanged == null));
OnPageChanged(pages.Count, curindex + dir * );
if (Mathf.Abs(rect.horizontalNormalizedPosition - pages[curindex + dir * ]) < 0.001)
{
timecount = ;
}
}
}
} public void AutoMove()
{
for (int i = ; i < pages.Count; i++)
{
if (Mathf.Abs(pages[i] - rect.horizontalNormalizedPosition) < 0.001)
{
curindex = i;
}
}
if (curindex == pages.Count - ) { dir = -; }
if (curindex == ) { dir = ; }
}
public void OnBeginDrag(PointerEventData eventData)
{
isDrag = ; }
public void MoveToPage(int target)
{
isDrag = ;
targethorizontal = pages [target];
if(target!=currentPageIndex)
{
currentPageIndex = target;
OnPageChanged(pages.Count, currentPageIndex);
}
}
public void OnEndDrag(PointerEventData eventData)
{
isDrag = ; float posX = rect.horizontalNormalizedPosition;
int index = ;
//假设离第一位最近
float offset = Mathf.Abs(pages[index] - posX);
for (int i = ; i < pages.Count; i++)
{
float temp = Mathf.Abs(pages[i] - posX);
if (temp < offset)
{
index = i; //保存当前的偏移量
//如果到最后一页。反翻页。所以要保存该值,
offset = temp;
}
} if(index!=currentPageIndex)
{
currentPageIndex = index;
OnPageChanged(pages.Count, currentPageIndex);
} targethorizontal = pages[index];
} void UpdatePages()
{
// 获取子对象的数量
int count = this.rect.content.childCount;
int temp = ;
for(int i=; i<count; i++)
{
if(this.rect.content.GetChild(i).gameObject.activeSelf)
{
temp++;
}
}
count = temp; if (pages.Count!=count)
{
if (count != )
{
pages.Clear();
for (int i = ; i < count; i++)
{
float page = ;
if(count!=)
page = i / ((float)(count - ));
pages.Add(page);
}
}
OnEndDrag(null);
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731