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.如何有效的 ...
随机推荐
- dbcp2和dbcp 1.4在API层面的差异
近期处于某种原因,打算把所有系统的数据库连接统一升级到dbcp2.发现有几处与dbcp 1在API层面发生了变化,主要如下所示: dbcp 2:org.apache.commons.dbcp2.Bas ...
- 最短路径—大话Dijkstra算法和Floyd算法
Dijkstra算法 算法描述 1)算法思想:设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合(用S表示,初始时S中只有一个源点,以后每求得一条最短路径 , ...
- Jaxb解析xml准换为javabean
先说下这个的背景吧,前些日子,有个以前的小同事说刚接触webservice,想解析下xml,记得我学的时候还是dom4j,sax的解析方式,最近看别人的代码用的jaxb的方式,觉得注解起来很简练,所以 ...
- lambda 个人学习理解
lambda是简化代码量的写用更简单的方法来写匿名方法 lambda左边是参数,右边是代码块(方法执行语句). 整体运算结果是根据左边参数,执行右边语句,返回右边执行的结果: 匿名方法是简化方法 1. ...
- ubuntu定时执行脚本(crond)
如果发现您的系统里没有这个命令,请安装下面两个软件包. vixie-cron crontabs crontab 是用来让使用者在固定时间或固定间隔执行程序之用,换句话说,也就是类似使用者的时程表.-u ...
- 【基础】PHP变量及变量作用域
新学PHP,比较有意思的语法,记录下. 1. 变量的作用域 作用域只分两个Global和Local,Global相对于整个.php文件来讲,Local是本地最小范围,是距离变量最近的范围,如:在函数中 ...
- Underscore学习笔记1
项目用了很久underscore.每次都是临时查手册,没有系统的研究过,最近有空正好看看 github地址:https://github.com/lily1010/underscore_learn 一 ...
- 初学Node(一)国际惯例HelloWorld
简介 没有用过Node,记的这些只是学习的笔记,有什么错的地方,望各位前辈指正. Node是一个服务器端Javascript解释器,依赖于Chrome v8引擎进行代码编译,事件驱动.非阻塞I/O都是 ...
- 关于C#中泛型类型参数约束(where T : class)
.NET支持的类型参数约束有以下五种:where T : struct | T必须是一个结构类型where T : class ...
- iOS开发~UI布局(一)初探Size Class
随着iOS8系统的发布,一个全新的页面UI布局概念出现,这个新特性将颠覆包括iOS7及之前版本的UI布局方式,这个新特性就是Size Class.Size Class配合Auto Layout可以解决 ...