Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bafelmauk · Nov 05, 2016 at 06:01 PM · c#javascriptscene-switchingscenesswipe

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 ?alt text

47520-untitled.png (38.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

236 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges