ScrollPage.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. using System;
  7. public class ScrollPage : MonoBehaviour, IBeginDragHandler, IEndDragHandler
  8. {
  9. //滑动速度
  10. public float smooting = 10;
  11. public float duration = 0.5f;
  12. public float minMoveOffset = 0.05f;
  13. private ScrollRect mRect;
  14. //页面:0,1,2,3 索引从0开始
  15. //每页占的比列:0/3=0 1/3=0.333 2/3=0.6666 3/3=1
  16. //float[] pages = { 0f, 0.333f, 0.6666f, 1f };
  17. private List<float> mPages = new List<float>();
  18. private int mCurrentPageIndex = 0;
  19. //滑动的起始坐标
  20. private float mTargethorizontal = 0;
  21. //是否拖拽结束
  22. private bool mIsDrag = false;
  23. private bool mIsStart = false;
  24. //最小缩放
  25. public float maxScale = 1.5f;
  26. public Transform content;
  27. public new Camera camera;
  28. /// <summary>
  29. /// 用于返回一个页码,-1说明page的数据为0
  30. /// </summary>
  31. public SystemDelegate.IntDelegate OnPageChanged;
  32. public SystemDelegate.IntDelegate OnPageStart;
  33. // Use this for initialization
  34. void Start()
  35. {
  36. mRect = transform.GetComponent<ScrollRect>();
  37. camera = GameObject.Find("UICamera").GetComponent<Camera>();
  38. //mRect.horizontalNormalizedPosition = 0;
  39. //UpdatePages();
  40. StartCoroutine(DelayStart() );
  41. }
  42. IEnumerator DelayStart(){
  43. yield return 1;
  44. yield return 1;
  45. mIsStart = true;
  46. UpdatePages();
  47. if (mCurrentPageIndex != 0){
  48. mTargethorizontal = mPages[mCurrentPageIndex];
  49. //OnEndDrag(null);
  50. }
  51. if (OnPageStart!=null ){
  52. OnPageStart(mCurrentPageIndex );
  53. }
  54. }
  55. public void SetPageIndex(int pageIndex ){
  56. //Debug.Log("page index is " + pageIndex );
  57. mCurrentPageIndex = pageIndex;
  58. if (!mIsStart){
  59. return;
  60. }
  61. mCurrentPageIndex = Mathf.Min(mPages.Count-1, mCurrentPageIndex);
  62. mCurrentPageIndex = Mathf.Max(0, mCurrentPageIndex);
  63. if (OnPageChanged != null ){
  64. OnPageChanged(mCurrentPageIndex);
  65. }
  66. mTargethorizontal = mPages[mCurrentPageIndex];
  67. }
  68. void Update()
  69. {
  70. UpdateItemScale();
  71. if (mIsStart ){
  72. UpdatePages();
  73. }
  74. //如果不判断。当在拖拽的时候要也会执行插值,所以会出现闪烁的效果
  75. //这里只要在拖动结束的时候。在进行插值
  76. if (!mIsDrag && mPages.Count>0)
  77. {
  78. mRect.horizontalNormalizedPosition =
  79. Mathf.Lerp(mRect.horizontalNormalizedPosition, mTargethorizontal, Time.deltaTime * smooting);
  80. }
  81. }
  82. public void UpdateItemScale()
  83. {
  84. List<GameObject> lis = new List<GameObject>();
  85. for (int i = 0; i < content.transform.childCount; i++)
  86. {
  87. lis.Add(content.transform.GetChild(i).gameObject);
  88. }
  89. foreach (var item in lis)
  90. {
  91. Vector2 pos = RectTransformUtility.WorldToScreenPoint(camera, item.transform.position);
  92. Vector2 dir = (pos - new Vector2(Screen.width / 2, pos.y)) / Screen.height;
  93. float cos;
  94. if (pos.x > Screen.width / 2)
  95. {
  96. cos = -dir.x;
  97. //缩放
  98. }
  99. else
  100. {
  101. cos = dir.x;
  102. }
  103. float scale = (float) Math.Round( cos + 1.6f,1);
  104. scale = Mathf.Clamp(scale, 1,maxScale);
  105. item.transform.localScale = new Vector3(scale, scale, scale);
  106. }
  107. }
  108. public int GetPageCount(){
  109. return mPages.Count;
  110. }
  111. public void OnBeginDrag(PointerEventData eventData)
  112. {
  113. mIsDrag = true;
  114. }
  115. public int GetSelectIndex(){
  116. return mCurrentPageIndex;
  117. }
  118. public void OnEndDrag(PointerEventData eventData)
  119. {
  120. mIsDrag = false;
  121. float posX = mRect.horizontalNormalizedPosition;
  122. float offset = mPages[mCurrentPageIndex] - posX;
  123. // Debug.Log("offset is " + offset );
  124. int lastIndex = mCurrentPageIndex;
  125. if (Mathf.Abs(offset ) > minMoveOffset ){
  126. if (offset < 0){
  127. mCurrentPageIndex++;
  128. }else{
  129. mCurrentPageIndex--;
  130. }
  131. Debug.Log("switch to " + mCurrentPageIndex );
  132. mCurrentPageIndex = Mathf.Min(mPages.Count-1, mCurrentPageIndex);
  133. mCurrentPageIndex = Mathf.Max(0, mCurrentPageIndex);
  134. if (lastIndex!=mCurrentPageIndex ){
  135. // Debug.Log("switch to " + mCurrentPageIndex );
  136. if (OnPageChanged != null ){
  137. OnPageChanged(mCurrentPageIndex);
  138. }
  139. mTargethorizontal = mPages[mCurrentPageIndex];
  140. Debug.LogWarning( "index "+ mCurrentPageIndex+ " value "+ mTargethorizontal);
  141. }
  142. }
  143. /*
  144. 因为这样效果不好。没有滑动效果。比较死板。所以改为插值
  145. */
  146. //rect.horizontalNormalizedPosition = page[index];
  147. }
  148. void UpdatePages()
  149. {
  150. mRect = transform.GetComponent<ScrollRect>();
  151. // 获取子对象的数量
  152. int count = this.mRect.content.childCount;
  153. int temp = 0;
  154. for(int i=0; i<count; i++)
  155. {
  156. if(this.mRect.content.GetChild(i).gameObject.activeSelf)
  157. {
  158. temp++;
  159. }
  160. }
  161. count = temp;
  162. if (mPages.Count!=count)
  163. {
  164. if (count != 0)
  165. {
  166. float unitX = mRect.content.GetComponent<RectTransform>().sizeDelta.x/ (float)count;
  167. float unitCount = mRect.viewport.GetComponent<RectTransform>().sizeDelta.x/unitX;
  168. if(unitCount<1)
  169. {
  170. unitCount = 1;
  171. }
  172. mPages.Clear();
  173. for (int i = 0; i < count; i++)
  174. {
  175. float page = 0;
  176. if(count>unitCount)
  177. page = i / ((float)(count-unitCount));
  178. page = page < 1 ? page : 1;
  179. mPages.Add(page);
  180. //Debug.LogWarning(i.ToString() + " page:" + page.ToString());
  181. }
  182. }
  183. //OnEndDrag(null);
  184. }
  185. }
  186. public GameObject GetItem(int index)
  187. {
  188. mRect = transform.GetComponent<ScrollRect>();
  189. return mRect.content.GetChild(index).gameObject;
  190. }
  191. }