using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class ScrollPage1 : MonoBehaviour, IBeginDragHandler,IEndDragHandler
{
private ScrollRect rect;
//存取得到的页面
private List<GameObject> pages = new List<GameObject> ();
private Transform mainGrid;
private bool doNot = false;
private bool startPosFlag = true;
private Vector3 startFingerPos;
private Vector3 nowFingerPos;
private float xMoveDistance;
//private float yMoveDistance;
//存放页面显示对应的按钮
public GameObject[] point;
//存放页面没有显示对应的按钮
public GameObject[] lostPoint;
private float time;
//记录OnBeginDrag时经过的时间
private float saveTime;
private bool isDrag=false;
private float lastHosi=-1;
private float smoothing =4;
float targethorizontal = 0;
private bool freePlay=false;
//页面所对应的 rect.horizontalNormalizedPosition值
private List<float> pagesHosi = new List<float>();
// Use this for initialization
void Start () {
rect = this.transform.GetComponent<ScrollRect>();
rect.horizontalNormalizedPosition = 0;
mainGrid = rect.transform.GetChild(0);
#region
//point[0].SetActive(true);
//point[1].SetActive(false);
//point[2].SetActive(false);
//point[3].SetActive(false);
//for (int i = 0; i < lostPoint.Length; i++)
//{
// lostPoint[i].GetComponent<Button>().onClick.AddListener(delegate()
// {
// Onclick(lostPoint[i].name);
// });
//}
#endregion
ThisClick();
Point(0);
PageHosi();
}
// Update is called once per frame
void Update () {
if (!doNot)
{
UpdatePages();
}
//是否自动播放
if (freePlay == false && pages.Count > 0)
{
time += Time.deltaTime;
AutoPlay();
}
//判断是否加载完了,是否在自动播放,切换界面
if (!isDrag && pages.Count > 0)
{
rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, Time.deltaTime * smoothing);
if (rect.horizontalNormalizedPosition > 1)
{
rect.horizontalNormalizedPosition = 1;
}
else if (rect.horizontalNormalizedPosition < 0)
{
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class ScrollPage1 : MonoBehaviour, IBeginDragHandler,IEndDragHandler
{
private ScrollRect rect;
//存取得到的页面
private List<GameObject> pages = new List<GameObject> ();
private Transform mainGrid;
private bool doNot = false;
private bool startPosFlag = true;
private Vector3 startFingerPos;
private Vector3 nowFingerPos;
private float xMoveDistance;
//private float yMoveDistance;
//存放页面显示对应的按钮
public GameObject[] point;
//存放页面没有显示对应的按钮
public GameObject[] lostPoint;
private float time;
//记录OnBeginDrag时经过的时间
private float saveTime;
private bool isDrag=false;
private float lastHosi=-1;
private float smoothing =4;
float targethorizontal = 0;
private bool freePlay=false;
//页面所对应的 rect.horizontalNormalizedPosition值
private List<float> pagesHosi = new List<float>();
// Use this for initialization
void Start () {
rect = this.transform.GetComponent<ScrollRect>();
rect.horizontalNormalizedPosition = 0;
mainGrid = rect.transform.GetChild(0);
#region
//point[0].SetActive(true);
//point[1].SetActive(false);
//point[2].SetActive(false);
//point[3].SetActive(false);
//for (int i = 0; i < lostPoint.Length; i++)
//{
// lostPoint[i].GetComponent<Button>().onClick.AddListener(delegate()
// {
// Onclick(lostPoint[i].name);
// });
//}
#endregion
ThisClick();
Point(0);
PageHosi();
}
// Update is called once per frame
void Update () {
if (!doNot)
{
UpdatePages();
}
//是否自动播放
if (freePlay == false && pages.Count > 0)
{
time += Time.deltaTime;
AutoPlay();
}
//判断是否加载完了,是否在自动播放,切换界面
if (!isDrag && pages.Count > 0)
{
rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, Time.deltaTime * smoothing);
if (rect.horizontalNormalizedPosition > 1)
{
rect.horizontalNormalizedPosition = 1;
}
else if (rect.horizontalNormalizedPosition < 0)
{
rect.horizontalNormalizedPosition = 0;
}
}
#region
//else
//{
// judueFinger();
//}
//rect.horizontalNormalizedPosition = 2 / ((float)(3));
//if (pages.Count == 4&&s==false)
//{
// //if (Input.GetMouseButtonDown(0))
// //{
// // Debug.Log(pages[0].transform.position);
// // Debug.Log(pages[1].transform.position);
// // Debug.Log(pages[2].transform.position);
// // Debug.Log(pages[3].transform.position);
// //}
// rect.horizontalNormalizedPosition = 2/ ((float)(3));
// s = true;
}
}
#region
//else
//{
// judueFinger();
//}
//rect.horizontalNormalizedPosition = 2 / ((float)(3));
//if (pages.Count == 4&&s==false)
//{
// //if (Input.GetMouseButtonDown(0))
// //{
// // Debug.Log(pages[0].transform.position);
// // Debug.Log(pages[1].transform.position);
// // Debug.Log(pages[2].transform.position);
// // Debug.Log(pages[3].transform.position);
// //}
// rect.horizontalNormalizedPosition = 2/ ((float)(3));
// s = true;
//}
#endregion
}
private void ThisClick()
{
for (int i = 0; i < lostPoint.Length; i++)
{
BtnOnClick(i, this.Onclick);
#endregion
}
private void ThisClick()
{
for (int i = 0; i < lostPoint.Length; i++)
{
BtnOnClick(i, this.Onclick);
}
}
private void PageHosi()
{
pagesHosi.Add(0);
pagesHosi.Add(0.3333333f);
pagesHosi.Add(0.666667f);
pagesHosi.Add(1);
}
//得到所有的页面
void UpdatePages()
{
#region
// 获取子对象的数量
////int count = this.rect.content.childCount;
////int temp = 0;
////for (int i = 0; i < count; i++)
////{
//// if (this.rect.content.GetChild(i).gameObject.activeSelf)
//// {
//// temp++;
//// }
////}
////count = temp;
}
private void PageHosi()
{
pagesHosi.Add(0);
pagesHosi.Add(0.3333333f);
pagesHosi.Add(0.666667f);
pagesHosi.Add(1);
}
//得到所有的页面
void UpdatePages()
{
#region
// 获取子对象的数量
////int count = this.rect.content.childCount;
////int temp = 0;
////for (int i = 0; i < count; i++)
////{
//// if (this.rect.content.GetChild(i).gameObject.activeSelf)
//// {
//// temp++;
//// }
////}
////count = temp;
////if (pages.Count != count)
////{
//// if (count != 0)
//// {
//// pages.Clear();
//// for (int i = 0; i < count; i++)
//// {
//// float page = 0;
//// if (count != 1)
//// page = i / ((float)(count - 1));
//// pages.Add(page);
//// // Debug.Log(i.ToString() + " page:" + page.ToString());
//// }
//// }
////}
#endregion
if (mainGrid.childCount==4)
{
Button[] btn = mainGrid.GetComponentsInChildren<Button>();
foreach (Button item in btn)
{
pages.Add(item.gameObject);
}
doNot = true;
}
}
public void OnBeginDrag(PointerEventData eventData)
{
saveTime = time;
isDrag = true;
freePlay = true;
lastHosi = rect.horizontalNormalizedPosition;
startFingerPos = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
isDrag = false;
nowFingerPos = Input.mousePosition;
Debug.Log(saveTime);
judueFinger();
}
public void Point(int index)
{
for (int i = 0; i < point.Length; i++)
{
if (i!=index)
{
point[i].SetActive(false);
}
else
{
point[i].SetActive(true);
}
}
}
//点击按钮切换页面
public void Onclick(int go)
{
if (go==0)
{
targethorizontal=pagesHosi[0];
time = 0;
Point(0);
}
else if (go==1)
{
targethorizontal=pagesHosi[1];
if (saveTime > 15.2)
{
time = 25.3f;
}
else
{
time = 5.3f;
}
Point(1);
}
else if (go==2)
{
targethorizontal=pagesHosi[2];
if (saveTime > 15.2f)
{
time = 20.3f;
}
else
{
time = 10.3f;
}
Point(2);
}
else if (go==3)
{
targethorizontal=pagesHosi[3];
time = 15.3f;
Point(3);
}
////{
//// if (count != 0)
//// {
//// pages.Clear();
//// for (int i = 0; i < count; i++)
//// {
//// float page = 0;
//// if (count != 1)
//// page = i / ((float)(count - 1));
//// pages.Add(page);
//// // Debug.Log(i.ToString() + " page:" + page.ToString());
//// }
//// }
////}
#endregion
if (mainGrid.childCount==4)
{
Button[] btn = mainGrid.GetComponentsInChildren<Button>();
foreach (Button item in btn)
{
pages.Add(item.gameObject);
}
doNot = true;
}
}
public void OnBeginDrag(PointerEventData eventData)
{
saveTime = time;
isDrag = true;
freePlay = true;
lastHosi = rect.horizontalNormalizedPosition;
startFingerPos = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
isDrag = false;
nowFingerPos = Input.mousePosition;
Debug.Log(saveTime);
judueFinger();
}
public void Point(int index)
{
for (int i = 0; i < point.Length; i++)
{
if (i!=index)
{
point[i].SetActive(false);
}
else
{
point[i].SetActive(true);
}
}
}
//点击按钮切换页面
public void Onclick(int go)
{
if (go==0)
{
targethorizontal=pagesHosi[0];
time = 0;
Point(0);
}
else if (go==1)
{
targethorizontal=pagesHosi[1];
if (saveTime > 15.2)
{
time = 25.3f;
}
else
{
time = 5.3f;
}
Point(1);
}
else if (go==2)
{
targethorizontal=pagesHosi[2];
if (saveTime > 15.2f)
{
time = 20.3f;
}
else
{
time = 10.3f;
}
Point(2);
}
else if (go==3)
{
targethorizontal=pagesHosi[3];
time = 15.3f;
Point(3);
}
}
public delegate void BtnHandler(int go);
public int BtnOnClick(int i, BtnHandler Onclickl)
{
//GameObject btnObj = GameObject.Find(btnName);
//GameObject btnObj = GameUtility.FindDeepChild(this.gameObject, btnName).gameObject;
//UnityEngine.UI.Button btn = btnObj.GetComponent<UnityEngine.UI.Button>();
//btn.onClick.AddListener(delegate()
//{
// onClick(btnObj);
//});
lostPoint[i].GetComponent<Button>().onClick.AddListener(delegate()
{
Onclickl(i);
});;
return i;
}
public void AutoPlay()
{
#region
//if (time>=5)
//{
// for (int i = 0; i < pagesHosi.Count; i++)
// {
// if (pagesHosi[i] - rect.horizontalNormalizedPosition < 0.01f)
// {
// if (i==3)
// {
// targethorizontal=pagesHosi[0];
// Point(0);
// }
// else
// {
// targethorizontal = pagesHosi[i + 1];
// Point(i+1);
// }
// {
// if (i==3)
// {
// targethorizontal=pagesHosi[0];
// Point(0);
// }
// else
// {
// targethorizontal = pagesHosi[i + 1];
// Point(i+1);
// }
// }
// }
// //Debug.Log(rect.horizontalNormalizedPosition);
// time = 0;
//}
#endregion
//0到1
if (time >= 5 && time < 5.2f)
{
targethorizontal = pagesHosi[1];
Point(1);
}
//1到2
if (time >= 10 && time < 10.2f)
{
targethorizontal = pagesHosi[2];
Point(2);
}
//2到3
if (time >= 15 && time < 15.2f)
{
targethorizontal = pagesHosi[3];
Point(3);
}
//3到2
if (time >= 20 && time < 20.2f)
{
targethorizontal = pagesHosi[2];
Point(2);
// }
// //Debug.Log(rect.horizontalNormalizedPosition);
// time = 0;
//}
#endregion
//0到1
if (time >= 5 && time < 5.2f)
{
targethorizontal = pagesHosi[1];
Point(1);
}
//1到2
if (time >= 10 && time < 10.2f)
{
targethorizontal = pagesHosi[2];
Point(2);
}
//2到3
if (time >= 15 && time < 15.2f)
{
targethorizontal = pagesHosi[3];
Point(3);
}
//3到2
if (time >= 20 && time < 20.2f)
{
targethorizontal = pagesHosi[2];
Point(2);
}
//2到1
if (time >= 25 && time < 25.2f)
{
targethorizontal = pagesHosi[1];
Point(1);
//2到1
if (time >= 25 && time < 25.2f)
{
targethorizontal = pagesHosi[1];
Point(1);
}
//1到0
if (time >= 30)
{
targethorizontal = pagesHosi[0];
Point(0);
time = 0;
//1到0
if (time >= 30)
{
targethorizontal = pagesHosi[0];
Point(0);
time = 0;
}
}
//判断滑动的方向,切换页面
public void judueFinger()
{
xMoveDistance = nowFingerPos.x - startFingerPos.x;
Debug.Log(xMoveDistance);
Debug.Log(lastHosi);
//右滑
if (xMoveDistance>0)
{
if (rect.horizontalNormalizedPosition!=0)
{
if (Mathf.Abs(lastHosi-pagesHosi[1])<0.1f)
{
targethorizontal = pagesHosi[0];
time = 0;
Point(0);
//判断滑动的方向,切换页面
public void judueFinger()
{
xMoveDistance = nowFingerPos.x - startFingerPos.x;
Debug.Log(xMoveDistance);
Debug.Log(lastHosi);
//右滑
if (xMoveDistance>0)
{
if (rect.horizontalNormalizedPosition!=0)
{
if (Mathf.Abs(lastHosi-pagesHosi[1])<0.1f)
{
targethorizontal = pagesHosi[0];
time = 0;
Point(0);
}
else if (Mathf.Abs(lastHosi - pagesHosi[2]) < 0.1f)
{
targethorizontal = pagesHosi[1];
if (saveTime>15.2)
{
time = 25.3f;
}
else
{
time = 5.3f;
}
Point(1);
}
else if (Mathf.Abs(lastHosi - pagesHosi[3]) < 0.1f)
{
targethorizontal = pagesHosi[2];
if (saveTime>15.2f)
{
time = 20.3f;
}
else
{
time = 10.3f;
}
Point(2);
}
else if (Mathf.Abs(lastHosi - pagesHosi[2]) < 0.1f)
{
targethorizontal = pagesHosi[1];
if (saveTime>15.2)
{
time = 25.3f;
}
else
{
time = 5.3f;
}
Point(1);
}
else if (Mathf.Abs(lastHosi - pagesHosi[3]) < 0.1f)
{
targethorizontal = pagesHosi[2];
if (saveTime>15.2f)
{
time = 20.3f;
}
else
{
time = 10.3f;
}
Point(2);
}
//targethorizontal = 0.333333f;
//Debug.Log(targethorizontal);
}
}
//左滑
else if (xMoveDistance<0)
{
if (rect.horizontalNormalizedPosition!=1)
{
if (Mathf.Abs(lastHosi - pagesHosi[2]) < 0.1f)
{
targethorizontal=pagesHosi[3];
time = 15.3f;
Point(3);
}
else if (Mathf.Abs(lastHosi - pagesHosi[1]) < 0.1f)
{
targethorizontal=pagesHosi[2];
time = 10.3f;
if (saveTime > 15.2)
{
time = 20.3f;
}
else
{
time = 10.3f;
}
Point(2);
}
else if (Mathf.Abs(lastHosi - pagesHosi[0]) < 0.1f)
{
targethorizontal = pagesHosi[1];
if (saveTime > 15.2)
{
time = 25.3f;
}
else
{
time = 5.3f;
}
Point(1);
}
}
}
//Debug.Log(targethorizontal);
}
}
//左滑
else if (xMoveDistance<0)
{
if (rect.horizontalNormalizedPosition!=1)
{
if (Mathf.Abs(lastHosi - pagesHosi[2]) < 0.1f)
{
targethorizontal=pagesHosi[3];
time = 15.3f;
Point(3);
}
else if (Mathf.Abs(lastHosi - pagesHosi[1]) < 0.1f)
{
targethorizontal=pagesHosi[2];
time = 10.3f;
if (saveTime > 15.2)
{
time = 20.3f;
}
else
{
time = 10.3f;
}
Point(2);
}
else if (Mathf.Abs(lastHosi - pagesHosi[0]) < 0.1f)
{
targethorizontal = pagesHosi[1];
if (saveTime > 15.2)
{
time = 25.3f;
}
else
{
time = 5.3f;
}
Point(1);
}
}
}
freePlay = false;
}
}