一、 点击滑动页面

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

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

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

附上代码

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using UnityEngine.UI;
  4.  
  5. public class Mask : MonoBehaviour {
  6.  
  7. public List<Sprite> sprite = new List<Sprite>();
  8. List<GameObject> image=new List<GameObject >();
  9. public GameObject pic;
  10. public Button but;
  11. public float time;
  12. float width, height;
  13. int num;
  14. void Start ()
  15. {
  16. num = sprite.Count;
  17. width = this.gameObject.GetComponent<RectTransform>().rect.width;
  18. height = this.gameObject.GetComponent<RectTransform>().rect.height;
  19. pic.transform.GetComponent<RectTransform>().sizeDelta=new Vector2(width,height);
  20. for (int i = ; i < num; i++)
  21. {
  22.  
  23. GameObject p = Instantiate(pic,new Vector3(transform.position.x+i*width,transform.position.y,),transform.rotation)as GameObject;
  24. p.transform.parent = this.gameObject.transform;
  25. image.Add (p) ;
  26. p.GetComponent<Image>().sprite = sprite[i];
  27. Button b = Instantiate(but, new Vector3(transform.position.x- ( * num - ) / +*i, transform.position.y - height/ + , ), transform.rotation) as Button;
  28. b.transform.parent = GameObject.FindWithTag("Button").transform;
  29. System.Object obj = (System.Object)i;
  30. b.onClick.AddListener(delegate(){this.MoveToPic((int)obj);});
  31. }
  32.  
  33. }
  34. void OnGUI()
  35. {
  36. if (GUI.Button(new Rect(transform.position.x + + width / , Screen.height - transform.position.y - , , ), ">"))
  37. {
  38. if (image[].transform.position.x < transform.position.x-)
  39. {
  40. Move();
  41. }
  42. }
  43. if (GUI.Button(new Rect(transform.position.x -width/-,Screen.height- transform.position.y- , , ), "<"))
  44. {
  45. if (image[num - ].transform.position.x > transform.position.x+)
  46. {
  47. Move(-);
  48. }
  49. }
  50. }
  51. public void Move(int dir)
  52. {
  53. for (int i = ; i <num; i++)
  54. {
  55. iTween.MoveAdd(image[i], iTween.Hash("x", width * dir, "time", time));
  56. }
  57. }
  58. public void MoveToPic(int i)
  59. {
  60.  
  61. float offset = transform.position.x - image[i].transform.position.x;
  62. for (int j = ; j < num; j++)
  63. {
  64. iTween.MoveAdd(image[j], iTween.Hash("x", offset, "time", time));
  65. }
  66. }
  67.  
  68. }

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

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. using System;
  6.  
  7. public class ScrollPage : MonoBehaviour, IBeginDragHandler, IEndDragHandler
  8. {
  9. ScrollRect rect;
  10. //页面:0,1,2,3 索引从0开始
  11. //每页占的比列:0/3=0 1/3=0.333 2/3=0.6666 3/3=1
  12. //float[] pages = { 0f, 0.333f, 0.6666f, 1f };
  13. List<float> pages = new List<float>();
  14. int currentPageIndex = -;
  15.  
  16. //滑动速度
  17. public float smooting = ;
  18.  
  19. //滑动的起始坐标
  20. float targethorizontal = ;
  21.  
  22. //是否拖拽结束 0 -结束 1-拖拽中 2-结束超过5s
  23. int isDrag = ;
  24. int curindex = ;
  25. int dir = ;
  26. float timecount = ;
  27. /// <summary>
  28. /// 用于返回一个页码,-1说明page的数据为0
  29. /// </summary>
  30. public System.Action<int,int> OnPageChanged;
  31.  
  32. float startime = 0f;
  33. float delay = 0.1f;
  34.  
  35. // Use this for initialization
  36. void Start()
  37. {
  38. rect = transform.GetComponent<ScrollRect>();
  39. //rect.horizontalNormalizedPosition = 0;
  40. //UpdatePages();
  41. startime = Time.time;
  42. }
  43.  
  44. void Update()
  45. {
  46. if (Time.time < startime + delay) return;
  47. UpdatePages();
  48. //如果不判断。当在拖拽的时候要也会执行插值,所以会出现闪烁的效果
  49. //这里只要在拖动结束的时候。在进行插值
  50. if (isDrag == && pages.Count > )
  51. {
  52. rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, Time.deltaTime * smooting);
  53. if (Mathf.Abs(rect.horizontalNormalizedPosition - targethorizontal) <= 0.001)
  54. { isDrag = ; timecount = ; }
  55. }
  56. if (isDrag == )
  57. {
  58. timecount += Time.deltaTime;
  59. if (timecount > )
  60. {
  61. AutoMove();
  62. rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, pages[curindex + dir * ], Time.deltaTime * smooting);
  63. if (pages == null || OnPageChanged == null)
  64. print("Error" + "," + (pages==null) + "," + (OnPageChanged == null));
  65. OnPageChanged(pages.Count, curindex + dir * );
  66. if (Mathf.Abs(rect.horizontalNormalizedPosition - pages[curindex + dir * ]) < 0.001)
  67. {
  68. timecount = ;
  69. }
  70. }
  71. }
  72. }
  73.  
  74. public void AutoMove()
  75. {
  76. for (int i = ; i < pages.Count; i++)
  77. {
  78. if (Mathf.Abs(pages[i] - rect.horizontalNormalizedPosition) < 0.001)
  79. {
  80. curindex = i;
  81. }
  82. }
  83. if (curindex == pages.Count - ) { dir = -; }
  84. if (curindex == ) { dir = ; }
  85. }
  86. public void OnBeginDrag(PointerEventData eventData)
  87. {
  88. isDrag = ;
  89.  
  90. }
  91. public void MoveToPage(int target)
  92. {
  93. isDrag = ;
  94. targethorizontal = pages [target];
  95. if(target!=currentPageIndex)
  96. {
  97. currentPageIndex = target;
  98. OnPageChanged(pages.Count, currentPageIndex);
  99. }
  100. }
  101. public void OnEndDrag(PointerEventData eventData)
  102. {
  103. isDrag = ;
  104.  
  105. float posX = rect.horizontalNormalizedPosition;
  106. int index = ;
  107. //假设离第一位最近
  108. float offset = Mathf.Abs(pages[index] - posX);
  109. for (int i = ; i < pages.Count; i++)
  110. {
  111. float temp = Mathf.Abs(pages[i] - posX);
  112. if (temp < offset)
  113. {
  114. index = i;
  115.  
  116. //保存当前的偏移量
  117. //如果到最后一页。反翻页。所以要保存该值,
  118. offset = temp;
  119. }
  120. }
  121.  
  122. if(index!=currentPageIndex)
  123. {
  124. currentPageIndex = index;
  125. OnPageChanged(pages.Count, currentPageIndex);
  126. }
  127.  
  128. targethorizontal = pages[index];
  129. }
  130.  
  131. void UpdatePages()
  132. {
  133. // 获取子对象的数量
  134. int count = this.rect.content.childCount;
  135. int temp = ;
  136. for(int i=; i<count; i++)
  137. {
  138. if(this.rect.content.GetChild(i).gameObject.activeSelf)
  139. {
  140. temp++;
  141. }
  142. }
  143. count = temp;
  144.  
  145. if (pages.Count!=count)
  146. {
  147. if (count != )
  148. {
  149. pages.Clear();
  150. for (int i = ; i < count; i++)
  151. {
  152. float page = ;
  153. if(count!=)
  154. page = i / ((float)(count - ));
  155. pages.Add(page);
  156. }
  157. }
  158. OnEndDrag(null);
  159. }
  160. }
  161. }

unity3d之实现各种滑动效果的更多相关文章

  1. a 锚点跳转滑动效果

    点击a链接时,跳转到相应id的位置处,有一个滑动效果. <a href="#my">我是跳转到div</a><div id="my" ...

  2. Android Scroll分析——滑动效果产生

    相对于在Android2.x版本上出现的长按.点击事件的效果,不得不说,滑动操作具有更好的用户体验.因此,从Android 4.X版本开始,出现了更多滑动操作的效果.越来越多第三方应用模仿这样的效果, ...

  3. jquery左右滑动效果的实现

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. bootstrap实现 手机端滑动效果,滑动到下一页,jgestures.js插件

    bootstrap能否实现 手机端滑动效果,滑动到下一页 jgestures.js插件可以解决,只需要引入一个JS文件<script src="js/jgestures.min.js& ...

  5. Android实现多页左右滑动效果,支持子view动态创建和cache

    要实现多页滑动效果,主要是需要处理onTouchEvent和onInterceptTouchEvent,要处理好touch事件的子控件和父控件的传递问题. 滚动控制可以利用android的Scroll ...

  6. 【Android 界面效果27】利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果

    本文主要介绍如何利用ViewPager.Fragment.PagerTabStrip实现多页面滑动效果.即google play首页.新浪微博消息(at.评论.私信.广播)页面的效果.ViewPage ...

  7. 十六、Android 滑动效果汇总

    Android 滑动效果入门篇(一)—— ViewFlipper Android 滑动效果入门篇(二)—— Gallery Android 滑动效果基础篇(三)—— Gallery仿图像集浏览 And ...

  8. Android 滑动效果进阶篇(六)—— 倒影效果

    上篇介绍了使用Animation实现3D动画旋转翻页效果,现在介绍图片倒影实现,先看效果图 本示例主要通过自定义Gallery和ImageAdapter(继承自BaseAdapter)实现 1.倒影绘 ...

  9. Android 滑动效果入门篇(二)—— Gallery

    Gallery 是Android官方提供的一个View容器类,继承于AbsSpinner类,用于实现页面滑动效果. 从上面的继承关系可以看出,AbsSpinner类继承自AdapterView,因此我 ...

随机推荐

  1. js计算器案例

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>j ...

  2. POJ1330 Nearest Common Ancestors (JAVA)

    经典LCA操作.. 贴AC代码 import java.lang.reflect.Array; import java.util.*; public class POJ1330 { // 并查集部分 ...

  3. php 返回数组中指定多列的方法

    php array_column 方法可以返回数组中指定的一列,但不能返回多列,本文将介绍array_column方法的使用,并用代码演示返回数组中指定多列的方法. 1.array_column说明 ...

  4. java中锁

    s锁的作用是就是保证线程安全,但是从另外成都讲影响了效率: 1 synchronized关键字 这个是虚拟机底层实现的, java中的关键字,内部实现为监视器锁,主要是通过对象监视器在对象头中的字段来 ...

  5. Codeforces Round #556 题解

    Codeforces Round #556 题解 Div.2 A Stock Arbitraging 傻逼题 Div.2 B Tiling Challenge 傻逼题 Div.1 A Prefix S ...

  6. python全栈开发学习_day1_计算机五大组成部分及操作系统

    一.计算机五大组成部分: 1)五大组成: 1.控制器(指挥系统,用于控制其他计算机硬件的工作) 2.运算器(用于数学运算及逻辑运算) 3.存储器(寄存器,高速缓存,内存,磁盘(机械,固态),磁带) 4 ...

  7. Bowen

    Advertise Window大小 注册表键值位于:regedit->HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Pa ...

  8. 论文阅读 | FPN:Feature Pyramid Networks for Object Detection

    论文地址:https://arxiv.org/pdf/1612.03144v2.pdf 代码地址:https://github.com/unsky/FPN 概述 FPN是FAIR发表在CVPR 201 ...

  9. hive Data Types

    在文章最开始,就先强调一下,hive不是仅仅支持string类型,hive不是仅仅支持string类型,hive不是仅仅支持string类型. Numeric Types tinyint,-128到1 ...

  10. Android Watchdog

    http://androidxref.com/6.0.1_r10/xref/frameworks/base/services/core/java/com/android/server/Watchdog ...