In the first scene or maybe the Main Menu scene of your game Create an Empty Gameobject. Call it whatever you like, for reference, I would call it as GO_LoadingScreen. Attach the script below to it:

using UnityEngine;
using System.Collections; public class LoadingScreen : MonoBehaviour
{
//The static loading loading screen texture to be assigned
public Texture2D texture;
//We make a static variable to our LoadingScreen instance
static LoadingScreen instance; //When the object awakens, we assign the static variable if its a new instance and
void Awake()
{
//destroy the already existing instance, if any
if (instance)
{
Destroy(gameObject);
hide(); //call hide function to hide the 'loading texture'
return;
} instance = this;
gameObject.AddComponent<GUITexture>().enabled = false; //disable the texture on start of the scene
guiTexture.texture = texture; //assign the texture
transform.position = new Vector3(0.5f, 0.5f, 1f); //position the texture to the center of the screen
DontDestroyOnLoad(this); //make this object persistent between scenes
} void Update()
{
//hide the loading screen if the scene is loaded
if(!Application.isLoadingLevel)
hide();
} //function to enable the loading screen
public static void show()
{
//if instance does not exists return from this function
if (!InstanceExists())
{
return;
}
//enable the loading texture
instance.guiTexture.enabled = true;
} //function to hide the loading screen
public static void hide()
{
if (!InstanceExists())
{
return;
}
instance.guiTexture.enabled = false;
} //function to check if the persistent instance exists
static bool InstanceExists()
{
if (!instance)
{
return false;
}
return true; } }

I have commented the script for you guys and I don't think I should explain it in detail as it is quite simple.

But, I would brief the logic though:
- The script is basically a singleton class (A singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance.) which expects a Texture2D input (this Texture2D is the static image which you want to display when the screen is loading)
- We have made the GO_LoadingScreen persistent throughout our game, that is, after the scene where it is created, by attaching the script above (If you are wondering which line achieve this objective, well, the line DontDestroyOnLoad(this) of the Awake function does this)
- Functions showhide and InstanceExists have been defined to display the texture, hidethe texture and check if the instance of the singleton already exists, respectively.

Save the script and switch back to unity. You will see that the script attached toGO_LoadingScreen is expecting a Texture2D component. Set the texture which you want to use for the static loading screen of your game.

Once this is done you can use this script by calling the show function of the LoadingScreen singleton from another script where in you switch between the levels.

For instance, check out the method below:

public static void loadLevel(string sceneName)
{
LoadingScreen.show();
Application.LoadLevel(sceneName);
}

See you around.

Follow the simple steps below to achieve this effect in Unity 4.6:

1. In the first scene of your game create an Empty GameObject and name it whatever you like, for reference I am going to name it as Splash Screen Holder, or perhaps, you can instead add it to the Main Menu scene as well.

2. Add a Canvas component inside this empty gameobject i.e., create a Canvas component and make it the child of  Splash Screen Holder object.

3. Inside the Canvas component, add a Image component, name it as ImageLS or any name you wish to.
Once you are done with the steps above your Splash Screen Holder object in the hierarchy will look like something in the image below:

Loading Scene / Splash Screen Hierarchy View

4.  Select the Render Mode of the Canvas to Screen Space - Camera. Check the Pixel Perfect checkbox. Drag and drop the Main Camera in the Render Camera field. Set theOrder in Layer to 1 so as to make the components of this Canvas appear on top of any other Canvas components, if any.

Canvas Component Properties

5. Drag and drop the Sprite which you want to display as the Loading Scene Image / Splash Screen Image in the Source Image field of the Image component.

Image Component Properties

6. You will see the Static Image on your Game and Scene view, which based on it's size, occupies a part of the screen. Resize the Image to occupy the entire Canvas and also set the Anchor points to the edges to make it dynamic.

 
Static Image Before Resizing 
Static Image After Resizing 

7. Create a new C# script named LoadingScreen46, attach it to the Splash Screen Holderobject and add the below code to it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using UnityEngine;
using System.Collections;
 
public class LoadingScreen46 : MonoBehaviour
{
 //We make a static variable to our LoadingScreen instance
 static LoadingScreen46 instance;
 //reference to gameobject with the static image
 GameObject loadingScreenImage;
 
 //function which executes on scene awake before the start function
 void Awake()
 {
  //find the ImageLS gameobject from the Hierarchy
  loadingScreenImage = GameObject.Find("ImageLS");
  //destroy the already existing instance, if any
  if (instance)
  {
   Destroy(gameObject);
   hide();     //call hide function to hide the 'loading Screen Sprite'
   return;
  }
  instance = this;   
  instance.loadingScreenImage.SetActive(false);
  DontDestroyOnLoad(this);  //make this object persistent between scenes
 }
 
 void Update()
 {
  //hide the loading screen if the scene is loaded
  if(!Application.isLoadingLevel)
   hide();
 }
 //function to enable the loading screen
 public static void show()
 {
  //if instance does not exists return from this function
  if (!InstanceExists())
  {
   return;
  }
  //enable the loading image object
  instance.loadingScreenImage.SetActive(true);
 }
 //function to hide the loading screen
 public static void hide()
 {
  if (!InstanceExists())
  {
   return;
  }
  instance.loadingScreenImage.SetActive(false);
 }
 //function to check if the persistent instance exists
 static bool InstanceExists()
 {
  if (!instance)
  {
   return false;
  }
  return true;
   
 }
  
}

Save the script and return to Unity.

8. You have now successfully added the loading screen / splash screen to your game using the Unity 4.6 GUI, however, you got to call the show function of the LoadingScreen46 script before calling the LoadLevel function to load any level. The below function demonstrates a simple usage of this Singleton script

1
2
3
4
5
public static void loadLevel(string sceneName)
{
        LoadingScreen46.show();
        Application.LoadLevel(sceneName);
}

The above function is a function from a script within which there would be a need to switch to a different scene during run time (Note that this is not the same script as LoadingScreen46)

See you around.

CREATE A LOADING SCENE / SPLASH SCREEN - UNITY的更多相关文章

  1. Create side-by-side stereo pairs in the Unity game engine

    Create side-by-side stereo pairs in the Unity game engine Paul BourkeDecember 2008 Sample Island pro ...

  2. iphone dev 入门实例7:How to Add Splash Screen in Your iOS App

    http://www.appcoda.com/how-to-add-splash-screen-in-your-ios-app/ What’s Splash Screen? For those who ...

  3. Windows Phone Splash Screen

    Why to use splash screen? Typically, you should use a splash screen in your app only if your app is ...

  4. Customize Netbeans Platform Splash Screen and About Dialog

    原帖一直打不开,通过谷歌翻译找到的 http://blogs.kiyut.com/tonny/2007/10/18/customize-netbeans-platform-splash-screen- ...

  5. Xamarin Android -创建Splash Screen (一)

    ......(空话少说) Xamarin 开发的技术资料很少,通过学习,把自己的学习过程及其中碰到的问题与大家分享. Splash Screen 就是在应用程序启动时,出现的一张图片,一般App的Sp ...

  6. Splash Screen开场屏在Android中的实现

    很多网友可能发现近期Tencent推出的手机QQ Android版包含了一个开场屏Splash Screen载入效果,通常游戏或大型软件打开时可能需要一个释放解析资源的过程,需要一个前台的动画播放和后 ...

  7. 实现在Android开发中的Splash Screen开场屏的效果

    很多网友可能发现近期Tencent推出的手机QQ Android版包含了一个开场屏Splash Screen载入效果,通常游戏或大型软件打开时可能需要一个释放解析资源的过程,需要一个前台的动画播放和后 ...

  8. Flutter - 给App增加启动屏幕(Splash Screen)并且设置背景颜色

    先看一下效果图,启动图最好设置为png格式的透明图,以防图片填充不满的时候背景图会非常的煞白(Flutter 默认背景色是白色). 打开android\app\src\main\res\drawabl ...

  9. 第十八章:自定义splash screen、app icon和tab icon

    本文介绍如何自定义splash screen.app icon和tab icon. 1. 自定义splash screen和app icon 1.1 产生resources文件夹,启动命令行: ion ...

随机推荐

  1. C#学习之Linq to Xml

    前言 我相信很多从事.NET开发的,在.NET 3.5之前操作XML会比较麻烦,但是在此之后出现了Linq to Xml,而今天的主人公就是Linq to Xml,废话不多说,直接进入主题. 题外:最 ...

  2. 39行代码实现JS HTML模板(轻量+高效+易用)

    otmpl 是一个轻量级前端模版(仅有39行无压缩代码,支持缓存),所用指令仅需[#...#]和{#...#},前者包含需要输出html语句,后者包含js变量. 支持javascript完整语法,你可 ...

  3. 一页纸商业计划书 (Business Plan) 模板(转载)

    本文转载自:https://blog.eood.cn/business-plan 假如你也有一个 idea ,但是还处于想法阶段,这个商业计划书模板能够帮你理清思路. 这个一页 BP 模板简单实用,分 ...

  4. Windows Phone编程中Dispatcher.BeginInvoke的使用

    在学习Windows Phone 程序开发时经常会使用到Dispatcher.BeginInvoke()的编程方法,可能许多初学者并不熟悉Dispatcher.BeginInvoke的使用方法以及为什 ...

  5. asp.net 生成 excel导出保存时, 解决迅雷下载aspx页面问题

    网络上搜索,一大堆废话,以下为简单的导出生成Excel代码: string excelFile = Server.MapPath("~/SB/UpFile/20151104111008/Bo ...

  6. freshcodecolor纯正则实现的在线代码着色(高亮)

    小菜最新完成的一款在线代码着色工具-freshcodecolor,该工具采用Javascript编写,着色识别策略完全采用正则表达式,无奈正则表达式在Javascript中有很大局限性,导致某些场合识 ...

  7. ciancd开源

     github 开源项目:ciandcd : https://github.com/ciandcdjenkins:https://github.com/ciandcd/jenkins-awesomec ...

  8. C# VS JAVA 差异 (未完待续)

    1. 静态构造函数 C#中有静态构造函数, Java中没有静态构造函数.其实Java中有一个类似静态构造函数的东东,称作静态初始化,或者静态代码块,可以通过这样的代码实现相同的功能: 但是Java中静 ...

  9. Atitit.javascript 实现类的方式原理大总结

    Atitit.javascript 实现类的方式原理大总结 1. 实现类的式::构造方法方式:原型方式:构造方法+原型的混合方式 1 2. 原型方式(function mode)经典式..实现属性推荐 ...

  10. fir.im Weekly - 我回来了

    Hey, 大家好,距离 fir.im 新版上线已匆忙过去一周多的时间,新版的fir正在慢慢稳定优化中,感谢大家的反馈与支持!后续我们将上线 FAQ 帮助中心,如还有疑问请邮件至 help@fir.im ...