SoFunction
Updated on 2025-03-07

NGUI implements the example code of sliding page turn effect

I won’t say much nonsense, I will just give you some real information.

The specific code is as follows:

using UnityEngine;
using ;
public class PageView : MonoBehaviour
{
const int ITEM_NUM = 2; //Total page countconst int PAGE_WIDTH = 2048; //Page widthconst float DRAG_SPEED = 0.5f; //Page turn timeconst int DRAG_OFFECT = 30; //The difference between the starting point and end point of the sliding must be greater than this number to trigger the page turn effectfloat beganX = 0; 
float beganY = 0; //The coordinates pressed by the mouseint curIndex = 1; //The current number of pages is the first page by defaultbool isPlay = false; //Is it turning the page?bool isPress = false; //Whether the mouse is pressedbool isPageFoot = false; //Is it currently at the end of the pagebool isHomePage = true; //Is it currently on the homepagestring left = "left"; //The name of the animation swipe leftstring right = "right"; //Swipe right animation nameGameObject[] Item_Objects;
// Use this for initialization
void Start ()
{
 ();
}
void Init()
{
Item_Objects = new GameObject[ITEM_NUM];
for(int i = 1; i <= ITEM_NUM; ++i)
{
Transform trans = ("item" + i);
if(trans)
{
GameObject spr = ("Background").gameObject;
<UIEventListener>();
().onPress = OnPressEvent;
}
Item_Objects[i - 1] = ;
}
}
//Mouse press event listeningvoid OnPressEvent(GameObject obj,bool isDown)
{
float endX;
float endY;
if (isDown) 
{
beganX = ;
beganY = ;
isPress = true;
} else 
{
endX = ;
endY = ;
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;
}
}
//Swipe leftvoid LeftDrag()
{
isPlay = true;
float x =  - PAGE_WIDTH;
TweenPosition leftTween =  (,DRAG_SPEED,new Vector3(x,0,0));
 = ;
 = "callback";
 = left;
 ();
}
//Swipe rightvoid RightDrag()
{
isPlay = true;
float x =  + PAGE_WIDTH;
TweenPosition rightTween =  (,DRAG_SPEED,new Vector3(x,0,0));
 = ;
 = "callback";
 = right;
 ();
}
//The callback function at the end of the animationvoid callback(UITweener tween)
{
isPlay = false;
if ( == left) 
{
curIndex ++;
} else if ( == right) 
{
curIndex --;
}
if (curIndex == 1) 
{
isHomePage = true;
}else
{
isHomePage = false;
}
if(curIndex == ITEM_NUM){
isPageFoot = true;
}else
{
isPageFoot = false;
}
}
}

The code ends here. If you have any questions about the code, please leave me a message. The editor will contact you in time. Thank you very much for your support for my website!