NGUI完成滑動翻頁後果實例代碼。本站提示廣大學習愛好者:(NGUI完成滑動翻頁後果實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是NGUI完成滑動翻頁後果實例代碼正文
空話不多說了,直接給年夜家上干貨了。
詳細代碼以下所示:
using UnityEngine;
using System.Collections;
public class PageView : MonoBehaviour
{
const int ITEM_NUM = 2; //總頁數
const int PAGE_WIDTH = 2048; //頁寬
const float DRAG_SPEED = 0.5f; //翻頁時光
const int DRAG_OFFECT = 30; //滑動的終點和起點的差需年夜於這個數能力觸發翻頁後果
float beganX = 0;
float beganY = 0; //鼠標按下的坐標
int curIndex = 1; //以後頁數,默許為第一頁
bool isPlay = false; //能否正在翻頁
bool isPress = false; //鼠標能否按下
bool isPageFoot = false; //以後能否處於頁尾
bool isHomePage = true; //以後能否處於首頁
string left = "left"; //左滑動畫的name
string right = "right"; //右滑動畫的name
GameObject[] Item_Objects;
// Use this for initialization
void 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;
}
}
//鼠標按下事宜監聽
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 ();
}
//動畫停止的回調函數
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;
}
}
}
代碼到此停止了,假如年夜家對代碼有疑問迎接給我留言,小編會實時和年夜家獲得接洽的。同時也異常感激年夜家對網站的支撐!