Как сделать экран загрузки unity
Перейти к содержимому

Как сделать экран загрузки unity

  • автор:

Splash Screen

The Unity Editor allows you to configure a Splash Screen for your project. The level to which you can customize the Unity Splash Screen depends on your Unity subscription; depending on whehether you have Unity Personal, Unity Plus or Unity Pro, you can disable the Unity Splash Screen entirely, disable the Unity logo, and add your own logos, among other options.

You can also make your own introductory screens or animations to introduce your Project in your first Scene, using the Unity UI System. These can be in addition to or instead of using the Unity Splash Screen, depending on your subscription.

The Unity Splash Screen is uniform across all platforms. It displays promptly, displaying while the first Scene loads asynchronously in the background. This is different to your own introductory screens or animations which can take time to appear; this is due to Unity having to load the entire engine and first Scene before displaying them.

Note: Unity copies these options directly to the Package.appxmanifest file.

Subscription limitations

The Unity Pro and Plus subscriptions have no limitations to customization of the Unity Splash Screen.

The Unity Personal subscription has the following limitations:

  • The Unity Splash Screen cannot be disabled.
  • The Unity logo cannot be disabled.
  • The opacity level can be set to a minimum value of 0.5.

Unity Splash Screen settings

To access the Unity Splash Screen settings, open Edit > Project Settings from the main menu in Unity and select the Player category. Then expand the Splash Image section and scroll down to the Splash Screen property.

Splash Screen settingsSplash Screen settings

The screen contains the following sections:

APreview

BStyle

CAnimation

DLogos

EBackground

Preview

Enable the Show Splash Screen option to display the Splash Screen at the start of the game. If you are using the Unity Personal subscription you cannot disable this option.

Splash Screen and Preview settingsSplash Screen and Preview settings

Use the Preview button to see a preview of the Splash Screen in the Game view. The preview reflects the resolution and aspect ratio of the Game view. Use multiple Game views to preview multiple different resolutions and aspect ratios simultaneously. This is particularly useful for simulating the Splash Screen’s appearance on multiple different devices.

Image A: Preview - Multiple previewsImage A: Preview — Multiple previews

Style

Use the Splash Style setting to control the style of the Unity branding.

Style settingStyle setting

Choose either Light on Dark (the default) or Dark on White from the Splash Style drop-down menu.

Image B: Splash Style — On the left, Light on Dark style. On the right, Dark on Light style.

Animation

Use the Animation setting to define how the Splash Screen appears and disappears from the screen.

Animation settingsAnimation settings

Choose one of the following values from the Animation drop-down menu:

Value Функция:
Static Do not apply animation.
Dolly The logo and background zoom to create a visual dolly effect. This is the default.
Custom Configure the background and logo zoom amounts to allow for a modified dolly effect.

Logos

Use the settings in the Logos section to customize your application’s logos.

Logos settings

Your application is co-branded with the Unity logo by default. If you are using the Unity Personal subscription you cannot disable this option. If you are using Unity Pro or Plus subscriptions, you can disable the Show Unity Logo option to remove the Unity logo from your application.

If you are using Unity co-branding, you can control how it appears by choosing one of the following values from the Draw Mode drop-down box:

Value Функция:
Unity Logo Below Draw the co-branding Unity logo beneath all logos that are shown.
All Sequential Insert the co-branding Unity logo as a logo into the Logos list.

You can customize the list of logos to appear on the Splash Screen:

Image C: Logos — The Logo list and Logo Duration

Each logo must be a Sprite Asset. Set the Sprite Mode to Single to use the entire sprite as the logo. If you want to change the logo’s aspect ratio, set the Sprite Mode to Multiple and edit the dimensions with the Sprite Editor.

To add and remove logos, use the plus (+) and minus (-) buttons.

To reorder logos, drag and drop them in the list.

You can control the length of time each Sprite Asset appears on the screen by setting its Logo Duration setting. Use a value between 2 and 10 seconds.

If an entry in the Logos list has no logo Sprite Asset assigned, no logo is shown for the duration of that entry. You can use this to create delays between logos.

The entire duration of the Splash Screen is the total of all logos plus 0.5 seconds for fading out. This might be longer if the first Scene is not ready to play, in which case the Splash Screen shows only the background image or color and then fades out when the first Scene is ready to play.

Background

Use the settings in the Background section to customize the background of your application’s Splash Screen.

Logos settingsLogos settings

Overlay Opacity

Adjust the strength of the Overlay Opacity setting to make the logos stand out. This affects the background color and image color, based on how you set up your Splash Style.

You can set the opacity to a lower number to reduce this effect. You can also completely disable the effect by setting it to 0. For example, if the Splash Screen style is Light on Dark, with a white background, the background becomes gray when Overlay Opacity is set to 1, and white when Overlay Opacity is set to 0. In the Unity Personal subscription, this option has a minimum value of 0.5.

Background Color

Set the Background Color when no background image is set. Note that the actual background color may be affected by the Overlay Opacity setting, and might not match the assigned color.

Background Image

Specify a reference to a Sprite image in the Background Image setting instead of using a color background. Unity adjusts the background image so that it fills the screen. The image is uniformly scaled until it fits both the width and height of the screen. This means that parts of the image might extend beyond the screen edges in some aspect ratios. To adjust the background image’s response to aspect ratio, change the Sprite’s Position values in the Sprite Editor.

Alternate Portrait Image

Set an Alternate Portrait Image with portrait aspect ratios (for example, a mobile device in portrait mode). If there is no Alternate Portrait Image Sprite assigned, the Unity Editor uses the Sprite assigned as the Background Image for both portrait and landscape mode.

You can adjust the Position and dimensions of the Sprite in the Sprite Editor to control the aspect ratio and position of the background image on the Splash Screen. In this example, the same image is being used for both landscape and portrait; however, the portrait position has been adjusted.

Image D: Background Image - The same is being used here for both landscape and portraitImage D: Background Image — The same is being used here for both landscape and portrait

Simple loading screen in Unity

Most games will have a loading screen between scenes to show the progress before revealing the next scene. Let’s go over how to implement this:

First, add a UI Image that will act as the progress bar. Select the source image and then set image type to filled. Set fill method to horizontal:

Position the progress bar. You can see what it will look like by adjusting the fill amount:

Create a script to handle the progress bar and also load the next scene. You can add this script to any other game object:

Loading screen tutorial

Chico Unity3D

It’s a common enough feature request: add a loading screen between levels. This isn’t quite as easy as you might expect, but it’s pretty quick once you’re familiar with Unity’s scripting features.

Because Unity allows us to control the way levels are loaded, we can put up a very simple “transition” scene while we wait for a bigger level to load. The transition scene could be just about anything, but here are some ideas:

  • Completely empty, except for an OnGUI hook showing some text.
  • An orthographic camera staring directly at a textured quad.

If you have Unity Pro, you could even take advantage of async level loading to create a quick animation or mini-game; otherwise, I’d recommend you stick with something that renders one frame (think “splash screen”).

Point is, the particular contents of the transition scene don’t matter very much. This tutorial just gets you as far as having one.

Creating a transition scene

We’re going to need a transition scene. I strongly recommend keeping it simple, so that it can load quickly and won’t slow down the more expensive target scene load.

  • Create and save an empty scene, name it “Loading”.
  • Add the scene to your build settings (File > Build settings…).
  • Populate the scene with some simple stuff.

As I mentioned above, I’m a fan of the ortho camera staring into one or more textured quads. You could whip up some scripts to show a semi-randomized texture, show some helpful tips, or whatever you want.

For the purpose of this tutorial, we’ll get up and running with the simplest possible thing:

Attach that script to something in your scene and press play. You should see the loading message. Nothing else will happen (yet).

Creating a script hook

Most people switch levels by calling Application.LoadLevel . Since we want to control the process, we need to provide a single hook for other scripts to call. We’ll make a new class called LevelManager that’s just simple enough to keep working:

This is just a wrapper, but it gives you a single access point: all other level switching should be done by calling LevelManager.Load , so that you can swap it out later without breaking the project.

Here’s a very simple test script:

The above will wait two seconds before calling LevelManager.Load . You should probably replace «SomeOtherLevel» with the name of an actual level that exists in your project. Or, if you’re in a hurry, use Application.loadedLevelName to reload the current level.

As an aside, remember that Application.LoadLevel can only load levels which are configured in your build settings.

A single-frame loading screen

This next step will get us to render one frame in the transition scene, then load the target scene. While performing a blocking load, Unity displays the last rendered frame; in our case, this is an advantage.

We’re going to start mixing static and instance members of the LevelManager class. If you don’t understand the difference, you might want to look it up.

We’re also going to use a coroutine. They enable us to create asynchronous functions which can execute across multiple frames.

Let’s flesh out the LevelManager class a bit more:

Reminder: unless you have access to Pro’s async loading, the transition level will freeze during the load. Keep it simple.

Try your LevelManagerTest script again. You should notice that your loading scene gets used!

How To Make a Loading Bar Screen in Unity (C# Tutorial)

After all these years, Unity hasn’t been able to provide a feature where you can simply add a loading screen between your game scenes. Despite that fact, implementing a loading bar in Unity is one of the easiest things.

But before we go right into making a loading bar, let’s understand first what loading a scene/level actually means in Unity.

To be able to load a scene from another scene, you will have to use a function from the Scene Manager known as SceneManager.LoadScene (which is also known back in the day as Application.LoadLevel ).

When the said function is used, Unity will perform the following process:

  • Loading – In this process, Unity will load the assets from the memory to make the upcoming level. This is also where your game will stay longer especially if your scene is big or uses a large set of game objects.
  • Activation– After the assets are loaded, Unity will start removing the previous game objects and starts loading the new assets to draw the upcoming level.

Without a loading screen, gamers will experience lag and glitches in the game and so it is highly suggested to implement a loading screen between levels.

In this tutorial, we’ll be teaching you how to create a loading screen with a loading bar and percentage to load a game scene whenever you trigger a LoadLevel function.

Video Tutorial

If you prefer watching video tutorials instead, you can play the following video:

Getting Started

Create a new project or open your existing project in Unity. Any projects will do as long as there are two or more scenes to switch between.
.
We’ll be using UI for this tutorial to be able to switch from one scene to another. So, to start, right-click into your hierarchy and select UI then Button.

Creating a Button with UI in Unity

Rename the Button to something like LoadLevelButton.

Next, inside the LoadLevelButton game object, select the Text game object.

Then go to its Text component and change the text from Button to Load Level.

Changing the text of Buttons in Unity UI

Next, go back to your hierarchy, right-click, create an empty game object and name it SceneManager.

Then, go to your Project window and create a new C# script, name the file LevelManager.cs .

Want to learn how to create a flappy bird game in less than 10 minutes?
How To Make Your Own Flappy Bird Game in 10 Minutes (Unity Tutorial)

Level Manager Script

Open the C# script with visual studio and copy the following code.

Save your script and go back to Unity.

Creating Scenes

Save your current scene and name it to anything you want. After that, create a new scene and save that scene and name it to anything you want again.

As for the reference, I will have two scenes named Level1 and Level2.

Once you have your scenes created, go to your build settings and add the two scenes.

Unity 3d build settings

Keep in mind though that since our levels don’t have that many objects, responses in the console will most likely show nothing.

Because if you remember in our code, we have used the class AsyncOperation. This class will allow us to see all the process that is happening whenever you load a scene.

With all that being said, let’s go back to our Level1 scene and add the LoadLevel function to our Button game object.

Select the LoadLevelButton game object, all the way down in the inspector, select Add Component and look for the script Level Manager.

Next, navigate to the Button component, just right underneath the On Click () section, click on the plus button.

Then, drag the LoadLevelButton game object to the box underneath the Runtime Only dropdown.

assigning functions in unity UI button on click

Next, click on the dropdown menu and open the LevelManager and select the LoadLevel (string).

Level Manager in Unity on click button

Then, type in the input box the name of the scene that you want to load. In our case, we’ll type Level2.

loading levels with unity 3d on click

Save your scene and try to run your scene.

You should be able to load your scene by clicking on the button and at the same time, you should be able to see a response in your console, like the following.

unity 3d console logs

The Loading Bar

.Now that we got our loading script working, let’s start adding a loading bar to our scene by going to the hierarchy once again then right-click on the Canvas game object, select UI and then Panel.

creating a UI panel in unity 3d

Next, select the Panel game object and go to its inspector and navigate to its Image component, change the source image to none and its color alpha to 255.

changing the color of the panel in unity 3d UI

Let’s rename our Panel game object to LoadingPanel.

Next, right-click to your LoadingPanel game object and navigate to UI, and select Slider.

creating a slider in unity 3d

Rename your new slider to LoadingBar.

With your LoadingBar game object selected, go to the inspector and change the following value for the Slider component.

  • Interactable : unchecked
  • Transition : none
  • Navigation : none

Next, inside your LoadingBar game object, delete the Handle Slide Area object.

removing the handle slide area in unity 3d

And also, I have realized that we didn’t actually use the SceneManager game object for the Level Manager script… So we’ll just delete that as well. (haha)

Alright, going back to the tutorial.

If you want to scale up your loading bar, you may by selecting the LoadingBar game object and adjusting its width and height in the Rect Transform.

changing the width and height of unity 3d UI slider

If you want to change the color of your loading bar, you may, by going to the Fill game object inside your Loading bar game object and inside the Fill Area game object.

changing the color of the slider in unity 3d UI

If you want to add a percentage text in your loading bar, you may as well, by right-clicking on your LoadingBar game object and select UI and then Text.

adding progress text percentage for loading bar

Then rename it to LoadingPercentageText.

Then position the text to wherever you want it to be. And, change the text from New Text to something like a percentage value for preview purposes.

If you want to change the font styles you may do it as well.

Using the Loading Bar

To start using the loading bar, first, we have to hide the loader first since we only need this to show up when we load another scene.

So go to your hierarchy, select LoadingPanel game object and uncheck the checkbox just beside the name of the game object.

disabling a game object in unity 3d

Next, we’ll be referencing this LoadingPanel in our script so we’ll be able to enable the game object when the game starts loading another scene.

Open your Level Manager script once again and update your script to the following.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *