Unity-WIKI 之 SplashScreen
组件功能
在屏幕上的一个启动画面消失,等待几秒钟(或等待用户输入),然后淡出,下一个场景加载。
组件源码
using UnityEngine;
using System.Collections; //
// SplashScreen Script
//
// Version 0.1 by Martijn Dekker
// martijn.pixelstudio@gmail.com
//
// Version 0.2 by Ferdinand Joseph Fernandez, 2010Sep7 16:45 GMT + 8
// Changes:
// * changed levelToLoad to a string, for easier usage
// * added waitTime, which adds a pause after fade in, and before fade
// out (during fade waiting)
// * added option to either automatically fade out after waitTime
// seconds (default), or wait for user input (press any key to continue)
// * added option to wait until fade out is complete before loading next
// level, instead of the default, which is to load the next level
// before fade out
//
// Version 0.3 by Ferdinand Joseph Fernandez, 2010Sep8 01:13 GMT + 8
// Changes:
// * splash screen itself is now fading without the need for a solid
// background color
// * optimized some code
//
// Version 0.4 by Ferdinand Joseph Fernandez, 2010Sep14 14:09 GMT + 8
// Changes:
// * splash screen picture can now be either centered (default) or
// stretched on the screen
//
// Version 0.5 by Ferdinand Joseph Fernandez, 2010Sep15 18:27 GMT + 8
// Changes:
// * now has option to start automatically or not. if not started
// automatically, the splash screen can be started by calling
// the StartSplash function
// * code acknowledges if the levelToLoad is blank, in that case,
// the code simply does not attempt to load a level
//
// Version 0.6 by Ferdinand Joseph Fernandez, 2010Sep29 13:43 GMT + 8
// Changes:
// * added the property "gui depth" so you can control at which depth the
// splash screen shows in
// public class SplashScreen : MonoBehaviour
{
public int guiDepth = 0;
public string levelToLoad = ""; // this has to correspond to a level (file>build settings)
public Texture2D splashLogo; // the logo to splash;
public float fadeSpeed = 0.3f;
public float waitTime = 0.5f; // seconds to wait before fading out
public bool waitForInput = false; // if true, this acts as a "press any key to continue"
public bool startAutomatically = true;
private float timeFadingInFinished = 0.0f; public enum SplashType
{
LoadNextLevelThenFadeOut,
FadeOutThenLoadNextLevel
}
public SplashType splashType; private float alpha = 0.0f; private enum FadeStatus
{
Paused,
FadeIn,
FadeWaiting,
FadeOut
}
private FadeStatus status = FadeStatus.FadeIn; private Camera oldCam;
private GameObject oldCamGO; private Rect splashLogoPos = new Rect();
public enum LogoPositioning
{
Centered,
Stretched
}
public LogoPositioning logoPositioning; private bool loadingNextLevel = false; void Start()
{
if (startAutomatically)
{
status = FadeStatus.FadeIn;
}
else
{
status = FadeStatus.Paused;
}
oldCam = Camera.main;
oldCamGO = Camera.main.gameObject; if (logoPositioning == LogoPositioning.Centered)
{
splashLogoPos.x = (Screen.width * 0.5f) - (splashLogo.width * 0.5f);
splashLogoPos.y = (Screen.height * 0.5f) - (splashLogo.height * 0.5f); splashLogoPos.width = splashLogo.width;
splashLogoPos.height = splashLogo.height;
}
else
{
splashLogoPos.x = 0;
splashLogoPos.y = 0; splashLogoPos.width = Screen.width;
splashLogoPos.height = Screen.height;
} if (splashType == SplashType.LoadNextLevelThenFadeOut)
{
DontDestroyOnLoad(this);
DontDestroyOnLoad(Camera.main);
}
if ((Application.levelCount <= 1) || (levelToLoad == ""))
{
Debug.LogWarning("Invalid levelToLoad value.");
}
} public void StartSplash()
{
status = FadeStatus.FadeIn;
} void Update()
{
switch (status)
{
case FadeStatus.FadeIn:
alpha += fadeSpeed * Time.deltaTime;
break;
case FadeStatus.FadeWaiting:
if ((!waitForInput && Time.time >= timeFadingInFinished + waitTime) || (waitForInput && Input.anyKey))
{
status = FadeStatus.FadeOut;
}
break;
case FadeStatus.FadeOut:
alpha += -fadeSpeed * Time.deltaTime;
break;
}
} void OnGUI()
{
GUI.depth = guiDepth;
if (splashLogo != null)
{
GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, Mathf.Clamp01(alpha));
GUI.DrawTexture(splashLogoPos, splashLogo);
if (alpha > 1.0f)
{
status = FadeStatus.FadeWaiting;
timeFadingInFinished = Time.time;
alpha = 1.0f;
if (splashType == SplashType.LoadNextLevelThenFadeOut)
{
oldCam.depth = -1000;
loadingNextLevel = true;
if ((Application.levelCount >= 1) && (levelToLoad != ""))
{
Application.LoadLevel(levelToLoad);
}
}
}
if (alpha < 0.0f)
{
if (splashType == SplashType.FadeOutThenLoadNextLevel)
{
if ((Application.levelCount >= 1) && (levelToLoad != ""))
{
Application.LoadLevel(levelToLoad);
}
}
else
{
Destroy(oldCamGO); // somehow this doesn't work
Destroy(this);
}
}
}
} void OnLevelWasLoaded(int lvlIdx)
{
if (loadingNextLevel)
{
Destroy(oldCam.GetComponent<AudioListener>());
Destroy(oldCam.GetComponent<GUILayer>());
}
} void OnDrawGizmos()
{
Gizmos.color = new Color(1f, 0f, 0f, .5f);
Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1));
}
}
Unity-WIKI 之 SplashScreen的更多相关文章
- 基于Unity有限状态机框架
这个框架是Unity wiki上的框架.网址:http://wiki.unity3d.com/index.php/Finite_State_Machine 这就相当于是“模板”吧,自己写的代码,写啥都 ...
- Unity 相关经典博客资源总结(持续更新)
就作为一个记录吧,把平时看过的Unity相关的一些好的Blog记录并分享. 好的论坛: Unity官方脚本 点评:这个不用说了,最核心的内容,理解整个Unity引擎的方方面面,梳理结构. Unity ...
- 【Unity3D基础教程】给初学者看的Unity教程(零):如何学习Unity3D
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点推荐.谢谢! Unity3D有什么优势 Unity3D是一个跨 ...
- 【转】Unity 相关经典博客资源总结(持续更新)
原文:http://blog.csdn.net/prothi/article/details/20123319 就作为一个记录吧,把平时看过的Unity相关的一些好的Blog记录并分享. 好的论坛: ...
- 【Unity Shader】2D动态云彩
写在前面 赶在年前写一篇文章.之前翻看2015年的SIGGRAPH Course(关于渲染的可以去selfshadow的博客里找到,很全)的时候看到了关于体积云的渲染.这个课程讲述了开发者为游戏< ...
- Unity Shaderlab: Object Outlines 转
转 https://willweissman.wordpress.com/tutorials/shaders/unity-shaderlab-object-outlines/ Unity Shader ...
- 学习Unity的步骤
作者:王选易链接:https://www.zhihu.com/question/23790314/answer/46815232来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- Unity FSM 有限状态机
翻译了一下unity wiki上对于有限状态机的案例,等有空时在详细写一下.在场景中添加两个游戏物体,一个为玩家并修改其Tag为Player,另一个为NPC为其添加NPCControl脚本,并为其将玩 ...
- unity 3d开发的大型网络游戏
unity 3d开发的大型网络游戏 一.总结 1.unity的官网上面应该有游戏列表 2.unity3D是很好的3d游戏引擎,也支持2d,也能做很多画面精良的3A级游戏 3.范围:电脑游戏,手机游戏, ...
- 2019年Unity学习资源指南[精心整理]
前言 进入一个领域,最直接有效的方法就是,寻找相关综述性文章,首先你需要对你入门的领域有个概括性的了解,这些包括: 1.主流的学习社区与网站. 2.该领域的知名大牛与热心分享的从业者. 3.如何有效的 ...
随机推荐
- html button自动提交表单问题
在ie中,button默认的type是button,而其他浏览器和W3C标准中button默认的属性都是submit,所以在chrome中,需要使用<button type="butt ...
- mysql 5.7.15发布
2016-09-06,mysql发布了5.7更新5.7.15,修复的bug数项目之前的版本已经大大减少,说明越来越稳定了.估计再过三四个版本,就会有很多公司开始考虑生产中使用了.
- 总结一下SQL的全局变量
SQL Server 2008中的全局变量及其用法 T-SQL程序中的变量分为全局变量和局部变量两类,全局变量是由SQL Server系统定义和使用的变量.DBA和用户可以使用全局变量的值,但不能自己 ...
- CSS类选择器和ID选择器
CSS类选择器和ID选择器皆允许以一种独立于文档元素的方式来指定样式,同时二者皆区分大小写. 区别如下: 第一:在同一个页面内,不允许有相同名字的id对象出现,但是允许相同名字的class 第二:当页 ...
- C#中的Mutex对象认识
我们知道,有些应用程序可以重复打开,有些只能打开一个,我以前写的程序为了防止用户打开多个程序,都是去遍历Process 查找进程的方式,现在看起来真是不专业,今天看大神的破解分析文章时,认识了mute ...
- andriod CheckBox
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:orientatio ...
- 如何在 在SharePoint 2013/2010 解决方案中添加 ashx (HttpHandler)
本文讲述如何在 在SharePoint 2013/2010 解决方案中添加 ashx (HttpHandler). 一般处理程序(HttpHandler)是·NET众多web组件的一种,ashx是其扩 ...
- 通过sftp实现文件分发功能
1 环境: 分发服务器:ubuntu server 64bit,192.168.56.22 接受服务器:windows server 2008,192.168.56.102 2 ...
- Android使用layer-list实现三面边框
layer-list可以将多个图片或形状按照顺序层叠起来 <?xml version="1.0" encoding="utf-8"?> <la ...
- 【读书笔记】iOS-属性列表
一,在Cocoa中,有一类名为属性列表的对象,常简写为plist.这些列表包含Cocoa知道如何操作的一组对象.具体来讲,Cocoa如何知道将这们保存在文件中并进行加载.属性列表类包括NSArray, ...