NGUI Implementation Sliding Page Turning Effect Example Code

  • 2021-09-16 07:49:31
  • OfStack

I won't talk too much nonsense, so I'll give you dry goods directly.

The specific code is as follows:


using UnityEngine;
using System.Collections;
public class PageView : MonoBehaviour
{
const int ITEM_NUM = 2; // Total pages 
const int PAGE_WIDTH = 2048; // Page width 
const float DRAG_SPEED = 0.5f; // Page turning time 
const int DRAG_OFFECT = 30; // The difference between the starting point and the ending point of sliding needs to be greater than this number to trigger the page turning effect 
float beganX = 0; 
float beganY = 0; // Coordinates of mouse press 
int curIndex = 1; // Current number of pages , Default to the 1 Page 
bool isPlay = false; // Are you turning pages 
bool isPress = false; // Does the mouse press down 
bool isPageFoot = false; // Is it currently at the end of the page 
bool isHomePage = true; // Is it currently on the home page 
string left = "left"; // Left slide animated name
string right = "right"; // Slide right animated 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;
}
}
// Press the mouse to monitor events 
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;
}
}
// Slide to the left 
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 ();
}
// Slide to the right 
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 ();
}
// Callback function at the end of animation 
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;
}
}
}

This is the end of the code, if you have any questions about the code, please leave me a message, this site will get in touch with you in time. At the same time, thank you very much for your support to this site!


Related articles: