Unity-WIKI 之 SplashScreen
本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhaoqingqing/p/3649331.html
组件功能#
在屏幕上的一个启动画面消失,等待几秒钟(或等待用户输入),然后淡出,下一个场景加载。
组件源码#
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)); } }
作者:赵青青 一名在【网易游戏】做游戏开发的程序员,擅长Unity3D,游戏开发,.NET等领域。
本文版权归作者和博客园共有,欢迎转载,转载之后请务必在文章明显位置标出原文链接和作者,谢谢。
如果本文对您有帮助,请点击【推荐】您的赞赏将鼓励我继续创作!想跟我一起进步么?那就【关注】我吧。
本文版权归作者和博客园共有,欢迎转载,转载之后请务必在文章明显位置标出原文链接和作者,谢谢。
如果本文对您有帮助,请点击【推荐】您的赞赏将鼓励我继续创作!想跟我一起进步么?那就【关注】我吧。
分类:
Unity 插件日志
标签:
插件
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 易语言 —— 开山篇
· Trae初体验