How make SWIPE LEVEL MENU in Unity?

Rehope Games
7 Jul 202313:32

Summary

TLDRThis Unity tutorial walks you through creating a Level Menu with navigation buttons and a bar. It teaches how to implement swipe detection and button click animations using the LeanTween package for smooth transitions. The process involves setting up a canvas, scroll view, and pages, then adding buttons with Grid Layout Group for alignment. Masking and updating UI elements like navigation buttons and bars are also covered, ensuring an interactive user experience.

Takeaways

  • 🎮 Start by adding a canvas to the Unity scene for the level menu.
  • 🔗 Create a scroll view and level pages for organizing content.
  • 🖼️ Use images to represent different levels and adjust their colors.
  • 🔄 Implement horizontal scrolling with a Scroll Rect component.
  • 🔲 Add level buttons to the pages using a Grid Layout Group for alignment.
  • 🖼️ Customize button images using assets created in Adobe XD.
  • 🎭 Add a mask component to the scroll view for clean UI transitions.
  • ➡️ Create navigation buttons and assign functions for page movement.
  • 📦 Install the LeanTween package for smooth animations and transitions.
  • 🔄 Implement swipe detection with a custom script and the EventSystem library.
  • 🔑 Update the navigation bar dynamically to reflect the current active page.

Q & A

  • What is the main focus of this Unity tutorial?

    -The main focus of this Unity tutorial is to create a Level Menu with navigation buttons, a navigation bar, swipe detection, and button click animations.

  • How do you start setting up the scene for the Level Menu?

    -You start by adding a canvas to the scene and making adjustments to its settings.

  • What is the purpose of creating an empty object called 'ScrollView'?

    -The 'ScrollView' object is created to serve as a container for the level pages that can be scrolled horizontally.

  • How do you adjust the size of the 'Page 1' object?

    -You resize the 'Page 1' object by changing its width and height.

  • What component is added to the 'Level Pages' object to align the pages horizontally?

    -A Horizontal Layout Group component is added to align the pages horizontally.

  • Why is the Scroll Rect component important in this tutorial?

    -The Scroll Rect component is crucial as it allows the level pages to be scrolled horizontally within the game.

  • How do you ensure that the level pages only move horizontally?

    -You disable the vertical scroll feature and the inertia feature in the Scroll Rect component.

  • What is the purpose of using the Grid Layout Group component?

    -The Grid Layout Group component is used to properly align the level buttons on each page.

  • Why is the LeanTween package installed in this tutorial?

    -The LeanTween package is installed to provide a variety of functions for creating smooth and controlled animations for GameObjects and UI elements.

  • How do you implement swipe detection in the tutorial?

    -Swipe detection is implemented by adding the EndDragHandler interface to the SwipeController script and creating a variable named dragThreshold.

  • What is the function of the 'UpdateBar' function in the SwipeController script?

    -The 'UpdateBar' function updates the navigation bar by setting all bar images to 'barClosed' and changing the bar image of the current page to 'barOpen'.

  • How do you add button click animations using the LeanTween library?

    -You create a new script named 'ButtonAnimation', add it to all buttons, and call the 'Anim' function when the button is clicked.

  • How do you handle the interactivity of the navigation buttons based on the current page?

    -You create a function named 'UpdateArrowButton' that disables the interactable feature of the Previous button on the first page and the Next button's interactable feature on the last page.

Outlines

00:00

🎮 Unity Level Menu Creation

This paragraph introduces a Unity tutorial focused on creating a Level Menu. The tutorial covers the creation of navigation buttons and a navigation bar, swipe detection implementation, and button click animations. The process begins with setting up the scene by adding a canvas and configuring it. An empty object named 'scroll view' is created, followed by another named 'Level Pages'. An image object 'page 1' is added and resized, then duplicated to create multiple pages. Horizontal layout and content size filter components are used to manage the layout. The tutorial then proceeds to add a Scroll Rect component to enable horizontal scrolling. Level buttons are added and customized with GridLayout Group for alignment. The tutorial also includes changing button images, adding a mask for clean edges, and setting up navigation buttons with assigned functions. The process concludes with a brief mention of using the LeanTween package for animations.

05:01

🔗 Implementing Swipe Detection and Navigation Buttons

The second paragraph delves into the installation and utilization of the LeanTween package, a tweening library that facilitates smooth animations for GameObjects and UI elements. The tutorial demonstrates how to import LeanTween and create a 'Swipe Controller' script with necessary variables and functions like 'Next', 'Previous', and 'MovePage'. The script is used to manage page transitions with specified tween times and types. The tutorial then explains how to set up swipe detection by implementing the EndDragHandler interface and using a 'dragThreshold' variable to determine when a page change should occur. Additionally, the creation of a navigation bar is detailed, including the setup of an empty object 'level bar', adding images, and using Horizontal Layout Group. The 'Swipe Controller' script is updated to include a function 'UpdateBar' that manages the visual state of the navigation bar, reflecting the active page.

10:04

🎨 Customizing Navigation Bar and Button Click Animations

The final paragraph focuses on refining the navigation bar and adding button click animations. It starts by detailing how to update the navigation bar's images to reflect the active page, using the 'UpdateBar' function called from 'Awake' and 'MovePage'. The tutorial then addresses the need to disable the 'Previous' button on the first page and the 'Next' button on the last page, introducing a function 'UpdateArrowButton' to manage button interactivity. The process involves creating button variables and calling the function from 'Awake' and 'MovePage'. The paragraph concludes with instructions on adding button click animations using the LeanTween library. A new script 'ButtonAnimation' is created and added to buttons, triggering an 'anim' function upon button clicks. The tutorial ends with a prompt to like the video and a farewell note, indicating the next tutorial will be forthcoming.

Mindmap

Keywords

💡Level Menu

A Level Menu in gaming refers to the interface that allows players to select different levels or stages within a game. In the context of the video, the Level Menu is being created using Unity, a popular game development engine. The script describes the process of setting up this menu, which includes navigation buttons and a navigation bar, making it a central concept for the tutorial.

💡Navigation Buttons

Navigation Buttons are UI elements that allow users to move between different sections or levels. In the video, the creator guides viewers on how to create these buttons in Unity and implement swipe detection, showing how they can be used to navigate through level pages, which is a fundamental part of creating an interactive Level Menu.

💡Swipe Detection

Swipe Detection is a feature that enables a game to respond to a player's swipe gestures on a touch screen. The video explains how to implement this feature using Unity, allowing for a more intuitive user experience. It's a key concept in mobile game development and is demonstrated through the script by adding functionality to the Level Menu.

💡Canvas

In Unity, a Canvas is a UI component that serves as a container for all UI elements. The script mentions adding a Canvas to the scene as the first step in creating the Level Menu, highlighting its importance as the foundational element for UI design within the game.

💡Scroll View

A Scroll View is a UI component that allows for scrolling through content that exceeds the visible area. The video script describes creating a Scroll View and setting it up to only scroll horizontally, which is crucial for the functionality of the Level Menu, enabling users to swipe through levels.

💡Horizontal Layout Group

The Horizontal Layout Group is a component in Unity used to automatically arrange child objects horizontally. In the script, it's added to the Level Pages object to align the buttons neatly, showcasing its use in organizing UI elements for better aesthetics and functionality.

💡Scroll Rect

Scroll Rect is a component in Unity that enables scrolling functionality for UI elements. The script describes adding this component to the Scroll View object and setting it up to only allow horizontal scrolling, which is essential for the navigation of level pages in the menu.

💡LeanTween

LeanTween is a tweening library for Unity that simplifies the process of creating smooth animations. The video script includes instructions on installing LeanTween and using it to create animations for button clicks, demonstrating its utility in enhancing the visual appeal and interactivity of the game's UI.

💡Tween Time

Tween Time refers to the duration over which an animation or transition occurs. In the context of the video, the creator discusses setting the tween time for transitioning between level pages, which is a key aspect of creating smooth and visually appealing navigation in the Level Menu.

💡Mask Component

The Mask Component in Unity is used to restrict the visible region of a UI element. The script mentions adding a mask to the Scroll View object to ensure that only the current level page is visible, which is important for maintaining a clean and focused UI design.

💡Button Click Animation

Button Click Animation refers to the visual feedback provided when a button is pressed. The video script includes instructions on creating and adding animations to buttons using LeanTween, which is a way to enhance user interaction and provide a more dynamic and engaging experience.

Highlights

Introduction to creating a Level Menu in Unity

Learn to create navigation buttons and a navigation bar

Implement swipe detection for a more interactive UI

Add button click animations for enhanced user experience

Adding a canvas to the scene for UI elements

Creating a scroll view object for the level menu

Setting up the width and height of the scroll view

Adding a Horizontal Layout Group for organized UI elements

Enabling scroll functionality with the Scroll Rect component

Disabling vertical scroll and inertia for horizontal-only navigation

Adding level buttons to the pages with proper resizing

Using Grid Layout Group for button alignment

Customizing button images with Adobe XD designs

Implementing a mask component for clean UI transitions

Adding page navigation buttons with proper positioning

Assigning functions to navigation buttons for page transitions

Installing and using the LeanTween package for smooth animations

Creating a SwipeController script for swipe detection

Adjusting tween time and types for customized animations

Adding swipe detection feature for intuitive UI navigation

Creating a navigation bar with dynamic image changes

Disabling interactable features for navigation buttons based on page

Adding button click animations using LeanTween for visual feedback

Encouraging viewers to like the tutorial for future content

Transcripts

play00:00

Hi dear friends. 🖐

play00:00

Welcome back to Rehope Games.

play00:02

In this Unity tutorial

play00:03

we’re gonna make a Level Menu.

play00:05

Also in this video you will learn

play00:07

how to create a navigation buttons

play00:09

and a navigation bar,

play00:10

implement swipe detection,

play00:11

and add button click animations.

play00:14

Ok! Let’s get started.

play00:17

As you can see,

play00:18

currently there is only a background object on our scene.

play00:21

Let's start by adding a canvas to the scene. 

play00:24

Right click on the hierarchy panel

play00:25

and create a canvas.

play00:30

I will make a small adjustment

play00:31

in the canvas settings.

play00:36

That's it.

play00:37

I will right-click on the canvas

play00:39

and create an empty object

play00:41

called scroll view.

play00:43

And I will change the width  and height of this object. 

play00:46

And then right click on the scroll View object

play00:48

and create an empty object called Level Pages.

play00:52

I will create an image under  the level pages object. 

play00:56

And I'll call it page 1.

play00:58

Resize the page 1 object.

play01:01

Duplicate page 1.

play01:08

I want to change the colors of the pages. 

play01:17

Select the level Pages object

play01:18

and add

play01:19

Horizontal layout Group component.

play01:23

Set the position of the level page like this.

play01:27

I'm going to set the spacing to 100.

play01:31

I'm going to add a

play01:32

content size filter component

play01:33

to the level pages.

play01:42

Start the game. 

play01:44

As you can see the level pages  are currently not scrolling. 

play01:47

I want to be able to

play01:48

scroll the level pages

play01:49

in the game like this.

play01:53

So select the scroll view object

play01:55

and add the Scroll Rect component.

play01:58

I will drag and drop the  object I want to scroll here.

play02:06

Then we determine which area

play02:08

will detect the scrolling motion.

play02:10

Drag and drop the scroll view object to the slot. 

play02:14

Start the game again.

play02:15

As you can see

play02:16

we can scroll the level pages object as we want.

play02:19

But I only want it to move horizontally.

play02:22

So I'm going to disable the  vertical scroll feature.

play02:29

And I'm going to disable the inertia feature. 

play02:45

Now I'm going to add the  level buttons to the pages. 

play02:51

Delete the text object of the button. 

play02:54

And I will resize the button.

play02:57

duplicate the button.

play02:59

I will use the Grid Layout Group component

play03:01

to properly align the buttons

play03:03

Select page 1 and add a  grid layout group component. 

play03:18

Start the game.

play03:25

I will rename the buttons.

play03:28

I want to change the images of the buttons.

play03:30

I created button images with Adobe XD before.

play03:33

I will Drag and drop

play03:34

the images to the Source image of the buttons.

play03:54

I will delete page 2 and page 3

play03:57

and then duplicate page 1.

play04:02

I will rename the buttons of page 2. 

play04:08

And change the button images of page 2.

play04:16

I will do the same process for page 3 as well. 

play04:24

Select scroll view object and add mask component.

play04:28

To perform the masking operation,

play04:30

we need an image component.

play04:34

Start the game. 

play04:41

Select all pages and remove the image component.

play04:44

Because we don't need it.

play04:51

Now I will add the page navigation buttons.

play04:54

Right click on the canvas and create a button.

play05:00

Resize the button.

play05:04

I’m gonna change the image of the button. 

play05:11

Set the position.

play05:14

Duplicate the button.

play05:19

And set the position.

play05:22

Change the image of the button.

play05:26

Start the game.

play05:31

I will assign the function

play05:32

of the navigation buttons.

play05:34

Before creating the SwipeController script,

play05:36

I would like to install the LeanTween package.

play05:39

The LeanTween package is a

play05:40

popular tweening library

play05:41

that provides a variety of functions for creating

play05:44

smooth and controlled animations.

play05:46

It allows you to animate GameObjects,

play05:48

UI elements,

play05:49

and properties over time with ease.

play05:52

Click on the window and click on the asset store. 

play05:55

Click on the Search Online button.

play05:57

Find the LeanTween Asset.

play06:04

Click on the Add to my Assets button.

play06:07

After that,

play06:08

open the package manager.

play06:14

Select my assets.

play06:15

You can see LeanTween here.

play06:18

I downloaded it already.

play06:20

You can first download it

play06:21

and then import it.

play06:30

The LeanTween asset imported.

play06:33

Select the scroll view object

play06:34

and then add a script named Swipe Controller.

play06:42

Open the script. 

play06:46

First, I will create the

play06:47

necessary variables that I need.

play06:50

I will create public functions

play06:51

named 'Next' and 'Previous'.

play06:54

And I’ve created a function

play06:55

named MovePage.

play06:57

If you'd like, I can create a detailed

play06:58

video on how to use LeanTween.

play07:11

You can access this script

play07:12

in the description.

play07:21

ok save the script

play07:22

and go back to unity.

play07:24

We have 3 pages.

play07:26

Drag and drop the Level Pages object here.

play07:31

We can set the duration for transitioning

play07:33

to the next page using Tween time.

play07:36

And set the tween type.

play07:39

Let's determine the page step.

play07:40

Select level Pages. 

play07:43

What should be the amount of position

play07:44

change to transition to the next page?

play07:50

In this example, a position change

play07:51

of -800 units on the x-axis is

play07:54

required to navigate to the next level page.

play07:56

This number may be different in your project.

play07:59

Let’s add an event function

play08:00

to the navigate buttons.

play08:02

Drag and drop the scroll view object here

play08:04

because it has the swipe controller script.

play08:07

Select the next Function.

play08:12

Start the game and test it. 

play08:19

As you can see navigation Buttons work properly.

play08:22

You can change the tween time as you want.

play08:29

And you can set the page step as you want. 

play08:36

Let's take a look at the tween types. 

play08:43

You can find the best tween time

play08:45

and tween type for you by trying.

play08:58

Let's add the

play08:59

swipe detection feature.

play09:00

Open the swipe controller script.

play09:02

Add EventSystem library.

play09:04

And I will add the

play09:05

EndDragHandler Interface.

play09:08

Implement the interface.

play09:11

I will create a variable named dragThershold.

play09:15

And set the variable .

play09:20

If we swipe the page

play09:21

more than the dragThershold value,

play09:23

the page will change.

play09:25

Save and go back to unity.

play09:28

Start the game.

play09:30

As you can see,

play09:31

when we scroll more than the value we set,

play09:33

it moves to the next page.

play09:37

Ok swipe detection system is ready.

play09:41

Let's add the navigation bar .

play09:43

Right click on the canvas and

play09:44

create an empty object named level bar.

play09:54

Right click on the level bar

play09:55

and create an image.

play10:03

Drag and drop the bar closed image

play10:05

to the source image of the bar.

play10:09

Duplicate the object.

play10:13

Select level bar object

play10:14

and add horizontal layout group component.

play10:28

I will replace the image

play10:29

of the active page's bar

play10:30

with the Open bar.

play10:32

Open the swipe controller script.

play10:35

First I will add the UI library.

play10:37

Then, create an array to keep

play10:39

the image components of the bars.

play10:41

Then create a sprite variable

play10:43

named barClosed and barOpen.

play10:46

I’m gonna create a function

play10:48

named UpdateBar.

play10:50

I will set all bar images

play10:51

to barClosed with a foreach loop.

play10:55

After that I will change the

play10:56

bar image of the current page to barOpen.

play10:59

I will call the UpdateBar function

play11:01

from Awake and

play11:03

the MovePage function.

play11:06

Go back to unity. 

play11:12

Set the barImage array size.

play11:18

And drag and the navigation bars here. 

play11:25

Drag and drop

play11:25

the images to the slots.

play11:31

Let’s test it. 

play11:37

Nice Navigation bar works properly.

play11:43

When on the first page,

play11:44

I want to disable the interactable feature

play11:46

of the Previous button.

play11:48

And when on the last page ,

play11:49

I want to disable the interactable feature of the Next button.

play11:52

I will create a button variable

play11:53

to keep navigation buttons.

play11:56

I'm going to create a function

play11:57

named UpdateArrowButton.

play12:02

That's it.

play12:03

I’m gonna call the function from

play12:05

awake function and MovePage function.

play12:07

Save and go back to unity.

play12:09

Drag and drop the

play12:10

navigation buttons to the slots.

play12:16

Start the game.

play12:18

As you can see,

play12:19

on the first page,

play12:20

the previous button's interactable feature is disabled.

play12:23

And on the last page,

play12:25

the next button's  interactable feature is disabled.

play12:30

You can add button click animation

play12:31

using the LeanTween library.

play12:35

I'm going to create a new script

play12:36

named button animation.

play12:54

I will add this script to all buttons.

play12:56

And when the button is clicked,

play12:58

the anim function will be called.

play13:00

save the script

play13:01

and go back to unity.

play13:05

Select the buttons to which you want to add animation,

play13:07

and add the ButtonAnimation script.

play13:14

Start the game. 

play13:21

Looks cool. 

play13:24

I hope the video was helpful to you.

play13:25

Please don’t forget to

play13:26

hit the like button. 👍

play13:27

Thanks for watching. 🙏

play13:28

And see you in the next unity tutorial.

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
Unity TutorialLevel MenuNavigation ButtonsSwipe DetectionButton AnimationsLeanTween PackageUI DesignScroll RectGridLayoutGroupHorizontal LayoutGame Development
Benötigen Sie eine Zusammenfassung auf Englisch?