- Home /
Android 2D game how to create swipe left/right views
I want to make a simple 2D android game and i'm wondering, what is the easiest/correct way to implement different screen views ? My game will have a few different screen views, and i want to be able to switch between them by swapping left/right, i want it to be like android main menu, where you can swap between different pages full of icons. So how can i do that ? I thought about creating different scenes, but will i be able then swap between scenes without loading time, and how then main game script/logic will run ? Or should i create one scene, and in that one scene create all different views, one next to another ?
Answer by Abdulov · Nov 07, 2016 at 10:40 PM
You should create one scene and use ScrollView object in UI. Then fill your screens in the content and use that script. I have a bit corrected code of this script for my project. If there are problems, I can share.
To simplify your work, you can add Grid Layout Group and Content Size Fitter components to the content object.
Answer by Axtrainz · Nov 07, 2016 at 10:09 PM
Well if is simple show up of those views without sliding animations and stuff you can do Iteration. I explain: We place all those screens in order at the same position, Deactivate all screens except the first one, and we do script:
public class SwipeViews
{
public List<GameObject> MyViews;
public float maxTouchDistance = 50f; // how long the Swipe input has to be to enable the movement.
private Touch initialTouch; // Touch Class Object
private bool hasSwiped, swipedSideways; // Variables to condition my movement.
private float distance; // Distance from my current position and ended touch position
private float deltaX, deltaY; // Touch position
private float currView = 0;
void FixedUpdate()
{
if(MyViews.Count < 2)
{
Debug.LogError("Please add more than 1 view object.");
return;
}
onSwipeInput();
}
/**
* onSwipeInput() get Input and activate next View in mobile controllers.
*/
void onSwipeInput()
{
foreach (Touch t in Input.touches)
{
if (t.phase == TouchPhase.Began)
{
initialTouch = t;
}//began
else if (t.phase == TouchPhase.Moved && !hasSwiped)
{
//CALCULATE TOUCH POSITION, DISTANCE, SWIPE SIDEWAYS
deltaX = t.position.x - initialTouch.position.x;
deltaY = t.position.y - initialTouch.position.y;
distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
swipedSideways = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);
if (distance > maxTouchDistance)
{
//Start move action
setNextView();
hasSwiped = true;
}//if distance
}//Moved>if
else if (t.phase == TouchPhase.Ended)
{
//SET EVERYTHING TO DEFAULT VALUE AFTER TOUCH INPUT FINISH
initialTouch = new Touch();
hasSwiped = false;
}//Ended>if
}//foreach-loop
}//swipe Move method
/** If nested to activate next view and iterate over them if reach the end. */
void setNextView()
{
/*************** RIGHT ****************/
if (swipedSideways && deltaX >= 0) // swipe right
{
if(currView + 1 < MyViews.Count)
{
MyViews[currView].setActive(false);
currView++;
MyViews[currView].setActive(true);
}
else
{
MyViews[currView].setActive(false);
currView = 0;
MyViews[currView].setActive(true);
}
}
/****************** LEFT ******************/
else if (swipedSideways && deltaX < 0 ) // swipe left
{
if(currView - 1 >= 0)
{
MyViews[currView].setActive(false);
currView--;
MyViews[currView].setActive(true);
}
else
{
MyViews[currView].setActive(false);
currView = MyViews.Count - 1;
MyViews[currView].setActive(true);
}
}
}//Next view
}
}//class
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
object move in three lanes 1 Answer
Swipe Patterns to unlock efects 2 Answers
UI canvas or panel that only appears once per game session? 1 Answer