廢話不多說了,直接給大家上干貨了。
具體代碼如下所示:
using UnityEngine;using System.Collections;public class PageView : MonoBehaviour{const int ITEM_NUM = 2; //總頁數(shù)const int PAGE_WIDTH = 2048; //頁寬const float DRAG_SPEED = 0.5f; //翻頁時間const int DRAG_OFFECT = 30; //滑動的起點和終點的差需大于這個數(shù)才能觸發(fā)翻頁效果float beganX = 0; float beganY = 0; //鼠標按下的坐標int curIndex = 1; //當前頁數(shù),默認為第一頁bool isPlay = false; //是否正在翻頁bool isPress = false; //鼠標是否按下bool isPageFoot = false; //當前是否處于頁尾bool isHomePage = true; //當前是否處于首頁string left = "left"; //左滑動畫的namestring right = "right"; //右滑動畫的nameGameObject[] Item_Objects;// Use this for initializationvoid Start (){this.Init ();}void Init(){Item_Objects = new GameObject[ITEM_NUM];for(int i = 1; i <= ITEM_NUM; ++i){Transform trans = this.transform.FindChild("item" + i);if(trans){GameObject spr = trans.transform.FindChild("Background").gameObject;spr.AddComponent<UIEventListener>();UIEventListener.Get(spr.gameObject).onPress = OnPressEvent;}Item_Objects[i - 1] = trans.gameObject;}}//鼠標按下事件監(jiān)聽void OnPressEvent(GameObject obj,bool isDown){float endX;float endY;if (isDown) {beganX = UICamera.lastTouchPosition.x;beganY = UICamera.lastTouchPosition.y;isPress = true;} else {endX = UICamera.lastTouchPosition.x;endY = UICamera.lastTouchPosition.y;if (isPress) {if(isPlay == false){if(endX - beganX > DRAG_OFFECT){if(isHomePage == false){RightDrag();}}else if(endX - beganX < DRAG_OFFECT){if(isPageFoot == false){LeftDrag();}}}}isPress = false;}}//向左滑void LeftDrag(){isPlay = true;float x = this.transform.localPosition.x - PAGE_WIDTH;TweenPosition leftTween = TweenPosition.Begin (this.gameObject,DRAG_SPEED,new Vector3(x,0,0));leftTween.method = UITweener.Method.EaseInOut;leftTween.callWhenFinished = "callback";leftTween.name = left;leftTween.Reset ();}//向右滑void RightDrag(){isPlay = true;float x = this.transform.localPosition.x + PAGE_WIDTH;TweenPosition rightTween = TweenPosition.Begin (this.gameObject,DRAG_SPEED,new Vector3(x,0,0));rightTween.method = UITweener.Method.EaseInOut;rightTween.callWhenFinished = "callback";rightTween.name = right;rightTween.Reset ();}//動畫結(jié)束的回調(diào)函數(shù)void callback(UITweener tween){isPlay = false;if (tween.name == left) {curIndex ++;} else if (tween.name == right) {curIndex --;}if (curIndex == 1) {isHomePage = true;}else{isHomePage = false;}if(curIndex == ITEM_NUM){isPageFoot = true;}else{isPageFoot = false;}}}代碼到此結(jié)束了,如果大家對代碼有疑問歡迎給我留言,小編會及時和大家取得聯(lián)系的。同時也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!
新聞熱點
疑難解答