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.如何有效的 ...
随机推荐
- Visual Studio 2013下JSON可视化工具
Visual Studio 2013现在我们有个小工具可以实现JSON可视化,这样给我们调试JSON提供了便利. JSON这种数据格式已经比较流行,在WEB前端随处可见. 在你需要安装VS ...
- SQL Server SQL语句执行顺序
执行顺序: 1.FROM:对FROM子句中前两个表执行笛卡尔积生成虚拟表vt1 2.ON:对vt1表应用ON筛选器只有满足 为真的行才被插入vt2 3.OUTER(join):如果指定了 OUTER ...
- C++模板元编程
ABC
- JavaScript If...Else、Switch、For、While、Break、Continue语句
一,JavaScript If...Else 语句 条件语句 通常在写代码时,您总是需要为不同的决定来执行不同的动作.您可以在代码中使用条件语句来完成该任务. 在 JavaScript 中,我们可使用 ...
- ADODB.Connection 错误 ‘800a0e7a’ 未找到提供程序
问题表现:做网站ASP页面提示:ADODB.Connection 错误 '800a0e7a' 未找到提供程序.该程序可能未正确安装. 解决方案:一般都是64位系统的原因,把IIS切换为32Bit模式运 ...
- javascript中静态方法、实例方法、内部方法和原型的一点见解
1.静态方法的定义 var BaseClass = function() {}; // var BaseClass=new Function(); BaseClass.f1 = function(){ ...
- How-to: disable the web-security-check in Chrome for Mac
When I try to test one web app in coperate intranet, there is always some error like "Failed to ...
- linux中fork()函数详解(原创!!实例讲解) (转载)
一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程, 也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不 ...
- IOS异常日志记录与展现功能
在平常的APP开发过程中经常碰到程序遇到异常闪退的问题,通过日志可以把相关的详细错误信息进行记录,本实例要记录不管在哪个页面出错都要进行记录,这边使用到的日志记录插件CocoaLumberjack,以 ...
- Java使用正则表达式获取文本的章节名称
获取文本的章节,首先要确定章节的开始标准,一般中文的章节都是以“第”开头,第一章.第二章等.所以使用“^”字符来确定首位,但是很多时候章节前面会有空白字符,所有以“第”作为章节的开始,进行以下的匹配 ...