123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- using System;
- public class ScrollPage : MonoBehaviour, IBeginDragHandler, IEndDragHandler
- {
- //滑动速度
- public float smooting = 10;
- public float duration = 0.5f;
- public float minMoveOffset = 0.05f;
- private ScrollRect mRect;
- //页面: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 };
- private List<float> mPages = new List<float>();
- private int mCurrentPageIndex = 0;
- //滑动的起始坐标
- private float mTargethorizontal = 0;
- //是否拖拽结束
- private bool mIsDrag = false;
- private bool mIsStart = false;
- //最小缩放
- public float maxScale = 1.5f;
- public Transform content;
- public new Camera camera;
- /// <summary>
- /// 用于返回一个页码,-1说明page的数据为0
- /// </summary>
- public SystemDelegate.IntDelegate OnPageChanged;
- public SystemDelegate.IntDelegate OnPageStart;
- // Use this for initialization
- void Start()
- {
- mRect = transform.GetComponent<ScrollRect>();
- camera = GameObject.Find("UICamera").GetComponent<Camera>();
-
- //mRect.horizontalNormalizedPosition = 0;
- //UpdatePages();
- StartCoroutine(DelayStart() );
- }
- IEnumerator DelayStart(){
- yield return 1;
- yield return 1;
- mIsStart = true;
- UpdatePages();
- if (mCurrentPageIndex != 0){
- mTargethorizontal = mPages[mCurrentPageIndex];
- //OnEndDrag(null);
- }
- if (OnPageStart!=null ){
- OnPageStart(mCurrentPageIndex );
- }
- }
- public void SetPageIndex(int pageIndex ){
- //Debug.Log("page index is " + pageIndex );
- mCurrentPageIndex = pageIndex;
-
- if (!mIsStart){
- return;
- }
- mCurrentPageIndex = Mathf.Min(mPages.Count-1, mCurrentPageIndex);
- mCurrentPageIndex = Mathf.Max(0, mCurrentPageIndex);
-
- if (OnPageChanged != null ){
- OnPageChanged(mCurrentPageIndex);
- }
- mTargethorizontal = mPages[mCurrentPageIndex];
-
- }
-
- void Update()
- {
- UpdateItemScale();
- if (mIsStart ){
- UpdatePages();
- }
- //如果不判断。当在拖拽的时候要也会执行插值,所以会出现闪烁的效果
- //这里只要在拖动结束的时候。在进行插值
- if (!mIsDrag && mPages.Count>0)
- {
- mRect.horizontalNormalizedPosition =
- Mathf.Lerp(mRect.horizontalNormalizedPosition, mTargethorizontal, Time.deltaTime * smooting);
- }
- }
- public void UpdateItemScale()
- {
- List<GameObject> lis = new List<GameObject>();
- for (int i = 0; i < content.transform.childCount; i++)
- {
- lis.Add(content.transform.GetChild(i).gameObject);
- }
- foreach (var item in lis)
- {
- Vector2 pos = RectTransformUtility.WorldToScreenPoint(camera, item.transform.position);
- Vector2 dir = (pos - new Vector2(Screen.width / 2, pos.y)) / Screen.height;
- float cos;
- if (pos.x > Screen.width / 2)
- {
- cos = -dir.x;
- //缩放
- }
- else
- {
- cos = dir.x;
- }
- float scale = (float) Math.Round( cos + 1.6f,1);
-
- scale = Mathf.Clamp(scale, 1,maxScale);
- item.transform.localScale = new Vector3(scale, scale, scale);
- }
- }
- public int GetPageCount(){
- return mPages.Count;
- }
- public void OnBeginDrag(PointerEventData eventData)
- {
- mIsDrag = true;
- }
- public int GetSelectIndex(){
- return mCurrentPageIndex;
- }
- public void OnEndDrag(PointerEventData eventData)
- {
-
- mIsDrag = false;
- float posX = mRect.horizontalNormalizedPosition;
- float offset = mPages[mCurrentPageIndex] - posX;
- // Debug.Log("offset is " + offset );
- int lastIndex = mCurrentPageIndex;
- if (Mathf.Abs(offset ) > minMoveOffset ){
- if (offset < 0){
- mCurrentPageIndex++;
- }else{
- mCurrentPageIndex--;
- }
-
- Debug.Log("switch to " + mCurrentPageIndex );
- mCurrentPageIndex = Mathf.Min(mPages.Count-1, mCurrentPageIndex);
- mCurrentPageIndex = Mathf.Max(0, mCurrentPageIndex);
- if (lastIndex!=mCurrentPageIndex ){
- // Debug.Log("switch to " + mCurrentPageIndex );
- if (OnPageChanged != null ){
- OnPageChanged(mCurrentPageIndex);
- }
- mTargethorizontal = mPages[mCurrentPageIndex];
- Debug.LogWarning( "index "+ mCurrentPageIndex+ " value "+ mTargethorizontal);
- }
-
- }
- /*
- 因为这样效果不好。没有滑动效果。比较死板。所以改为插值
- */
- //rect.horizontalNormalizedPosition = page[index];
- }
- void UpdatePages()
- {
- mRect = transform.GetComponent<ScrollRect>();
- // 获取子对象的数量
- int count = this.mRect.content.childCount;
- int temp = 0;
- for(int i=0; i<count; i++)
- {
- if(this.mRect.content.GetChild(i).gameObject.activeSelf)
- {
- temp++;
- }
- }
- count = temp;
-
- if (mPages.Count!=count)
- {
- if (count != 0)
- {
- float unitX = mRect.content.GetComponent<RectTransform>().sizeDelta.x/ (float)count;
- float unitCount = mRect.viewport.GetComponent<RectTransform>().sizeDelta.x/unitX;
- if(unitCount<1)
- {
- unitCount = 1;
- }
- mPages.Clear();
- for (int i = 0; i < count; i++)
- {
- float page = 0;
- if(count>unitCount)
- page = i / ((float)(count-unitCount));
- page = page < 1 ? page : 1;
- mPages.Add(page);
- //Debug.LogWarning(i.ToString() + " page:" + page.ToString());
- }
- }
- //OnEndDrag(null);
- }
- }
- public GameObject GetItem(int index)
- {
- mRect = transform.GetComponent<ScrollRect>();
- return mRect.content.GetChild(index).gameObject;
- }
- }
|