如何在统一进行加载屏幕
发表于2016-03-29
在一些点或另一个,每一个游戏开发者必须创建一个加载屏幕。通常,你会看到一个加载屏幕在游戏开始时,或在进入一个新的游戏区。一个进度条或旋转的图标会弹出,你得等一会儿。在这篇文章中,您将学习如何创建一个加载屏幕闪烁的“加载中…”的文本看起来像这样。

如果这篇文章有点先进,你可能想从树的过程如何使一个视频游戏, and learn the basics of Unity and C# programming. When you’re done, you can come back to this post and add a loading screen to your newly created game!

一、当使用一个加载屏幕
加载屏幕时使用的运动模型,纹理,声音,和其他游戏的资产从一个更大的存储介质(如硬盘驱动器,光盘驱动器,或Web)到内存(RAM)在主板或显卡(显存)。这是一个必要的步骤,因为游戏一般渲染图形在60帧每秒或更高,而内存的主板和显卡提供了更快的访问数据,必须使每个框架和运行游戏的逻辑。
不幸的是,电脑的记忆容量有限,也有一个限定的快速数据可以加载和卸载不是一切都可以从硬盘读取或流从实时网络。因为这个原因,有些游戏需要显示屏幕给玩家指出游戏需要一定的时间才能赶上行动。
有许多方法来加载资产变成一场游戏,但在统一中,最常见的方法是从一个场景切换到另一个。
在某些情况下,游戏本身可能发生在一个单一的场景,而是一次“主菜单场景”可以让玩家配置选项和选择什么时候开始游戏。当玩家选择开始游戏,“游戏场景加载。这是有益的因为它允许比赛开始非常迅速,因为主菜单场景的资产可以加载几乎瞬间。
在其他情况下,一个游戏可能是如此之大,不同的场景需要打破游戏。例如,一个角色扮演游戏,可以供玩家探索大型户外区域,但一旦他们进入一个洞穴或建筑物,一个载入画面可以从室外场景切换到室内场景。
许多游戏使用加载屏幕作为一个机会来给予提示如何玩游戏或游戏的传说。例如,我最喜欢的一个统一的游戏,炉石:魔兽英雄 has loading screens that give a randomly chosen game tip and also contain amusing flavor text. These types of things make the wait feel shorter because they give the player something to do while the computer is working.
二、场景设置
连接两个场景共同团结相当简单。基本设置非常简单,然后你可以美化你的加载屏幕额外的动画或有趣的信息但是你会喜欢的。在一个较高的水平,一个场景只需要加载其他。当新场景加载开始,UI文本或加载杆可以在当前的场景让玩家知道计算机是工作。这是一个好主意,添加少量的动画加载UI让玩家知道计算机仍在工作(而不是冷冻)。
创建一个加载屏幕在这篇文章中相似的人,你需要带一个空的游戏对象将加载脚本UI文本游戏对象创建一个场景。我把我的空的游戏对象”现场装载机”,但它可以叫什么。这是我在现场看起来像前附加任何脚本(点击图片查看全分辨率截图)。

层次结构中的窗口,“现场人数”和“指令”的游戏对象都是游戏对象的用户界面文本组件连接到显示“第一幕”文本和它下面的说明文字。
其次,对“现场装载“游戏中的对象,我附加了一个脚本组件称为sceneloader.cs–看起来这在Inspector窗口:

现场是一个整数,商店的数量应该加载场景。在统一中,在构建每一个场景是编号。默认情况下,第一幕开始编号为零,然后,等等。加载文本字段存储文本组件使教学文本可以被交换与闪烁的“加载中…”文本。
这sceneloader.cs脚本包含下面的代码(说明):
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SceneLoader : MonoBehaviour {
private bool loadScene = false;
[SerializeField]
private int scene;
[SerializeField]
private Text loadingText;
// Updates once per frame
void Update() {
// If the player has pressed the space bar and a new scene is not loading yet...
if (Input.GetKeyUp(KeyCode.Space) && loadScene == false) {
// ...set the loadScene boolean to true to prevent loading a new scene more than once...
loadScene = true;
// ...change the instruction text to read
loadingText.text = ;
// ...and start a coroutine that will load the desired scene.
StartCoroutine(LoadNewScene());
}
// If the new scene has started loading...
if (loadScene == true) {
// ...then pulse the transparency of the loading text to let the player know that the computer is still working.
loadingText.color = new Color(loadingText.color.r, loadingText.color.g, loadingText.color.b, Mathf.PingPong(Time.time, 1));
}
}
// The coroutine runs on its own at the same time as Update() and takes an integer indicating which scene to load.
IEnumerator LoadNewScene() {
// This line waits for 3 seconds before executing the next line in the coroutine.
// This line is only necessary for this demo. The scenes are so simple that they load too fast to read the
yield return new WaitForSeconds(3);
// Start an asynchronous operation to load the scene that was passed to the LoadNewScene coroutine.
AsyncOperation async = Application.LoadLevelAsync(scene);
// While the asynchronous operation to load the new scene is not yet complete, continue waiting until it's done.
while (!async.isDone) {
yield return null;
}
}
}

代码的注释,但我们通过主要街区散步。
三、变量
在上课前,三个变量的声明。
私人布尔loadscene–这个布尔值是用来检查是否当前的场景应该是加载一个新的场景。默认情况下,它设置为false,因为现场等待玩家的输入。
私有场景–这个整数是暴露在Inspector窗口通过serializefield [ ]属性和存储所需的场景数量应加载。
loadingtext私人文本 – This variable is of type Text, and it’s also exposed in the Inspector window via the serializefield [ ]属性。这个变量存储一个参考的“教学文本”的游戏对象的文本组件。
1、update()
任何代码里面的update()方法运行每帧。在这个脚本,update()是指由两个if语句。
第一如果语句检查玩家是否按下空格键和是否loadscene布尔是真实的。如果玩家按下空格键,然后loadscene布尔值设置为true。设置这个布尔真并检查它的如果声明意味着这一块只会跑一旦. This is important, because otherwise, the new scene would just load over and over again. In addition, the text inside of the loadingtext variable is set to “Loading…” to give the player some feedback based on their input. Finally, the if block starts a coroutine method named loadnewscene()这样,一旦被调用,将运行正常的外update()环。
下一个如果表内update() flashes the “Loading…” text by bouncing the alpha channel of the text’s color from zero to one and then back again. This statement executes every frame (and thus, flashes the color) if the loadscene boolean has been set to true. You could change the functionality inside of this if statement to do any number of things, such as display a spinning icon or wobble the rotation of the text.
2、loadnewscene()
对脚本的底部块是什么被称为一个过程,它可以返回任何实现C #接口迭代器。这是稍微超出了本文的范围,但总的来说,协程是一种特殊类型的方法,在统一中,可以继续在自己的并行执行update()方法。作为update()方法继续运行,程序同时运行也。重要的是要注意,不能保证是否协同程序将运行之前或之后update()在任何特定的帧的方法。它运行在它自己的时间,所以它需要精心设计,以便它不依赖于其他变量被设置在任何特定的顺序。
里面的loadnewscene()方法的第一行收益的新waitforseconds(3) simply waits for 3 seconds while the loading text flashes. These two scenes are so simple that they load instantly, so if this line were removed, the “Loading…” text would never be visible because the scene would switch immediately. When you implement a loading screen in your game, this line should be removed completely because it will just slow down the loading process; it’s only meant for the purposes of this example.
其次是线AsyncOperation async = Application.LoadLevelAsync(scene);这条线是最重要的,因为它的线实际加载新的场景。它使用应用类在统一调用方法loadlevelasync(),以场景的数量(在这种情况下,来自同一个)。你可以阅读更多关于loadlevelasync()在统一的文档,但本质上,它加载一个新的场景的背景和返回类型为异步操作。这是存储在异步变量。
异步操作有一个布尔变量结束,这是方便的,因为它可以用来检查是否有新的场景完成加载。进一步在loadnewscene()方法,结束用于输入回路,这基本上可以让程序继续运行直到新的场景已加载。
四、连接两个场景
一旦这个脚本已连接,填写督察领域“加载文本“拖动”说明“游戏中的对象从层次窗口检查员现场。然后复制当前的场景,给它一个不同的名字。您可以通过将文件菜单,选择“另存为场景…”这个场景的名字无关紧要,只要它对你意味着什么。
下一步,转到文件菜单并选择“生成设置…”打开设置窗口的建立。然后将两个副本场景从项目窗口的建立设置窗口。这将增加场景的构建。对每个正确的名字里面的场景构建构建部分,你也应该看到一个小的数。现场零将永远载入第一,所以如果你的第一个场景是不是编号为零,你可以拖动场景排序。

这一幕数,应该在检查现场的“场景”在现场加载脚本组件。现场数零应加载场景数量一,反之亦然。你可能需要重新打开以前的场景设置其“景”领域适当。
最后,运行游戏模式游戏。你可以按空格键去从零到场景中的一个场景,然后按空格键,再加载场景零次。
五、载入画面的想法
这里的链接到我的完整统一的项目下载包含的场景和sceneloader.cs脚本连接在一起。如果你有任何的加载屏幕的想法,你想尝试,随时修改该项目,然后在评论自己的作品展示。当然,如果你有任何问题,请随时让我知道在评论以及!