控制切换场景淡入淡出的教程
来源:互联网 发布:唯歌生民病 愿得天子知 编辑:程序博客网 时间:2024/06/05 19:23
using UnityEngine;
using System.Collections;
/*
* 使用方法:将该脚本附在镜头下面,然后切换场景时镜头不销毁
* 要调用的时候获得该脚本,然后调用StartSplash()
*/
//@author joi
public class SceneLoad : MonoBehaviour {
public int guiDepth = 0;
public string levelToLoad = "";
//将要加载的场景序号
public int levelToLoadInt;
//切换场景的纹理
public Texture2D splashLogo;
//淡入淡出的速度
float fadeSpeed = 0.8f;
//保持纹理最高透明度的时间
float waitTime = 0f;
//是否要等待输入
public bool waitForInput = false;
public bool startAutomatically = false;
public bool IsDealPlayer=false;
private float timeFadingInFinished = 0.0f;
//处理切换场景后需要实现的事件
public delegate void EventHandler();
public event EventHandler trigger;
public delegate void EventHandler2nd();
public event EventHandler2nd triggerAtLoading;
//淡入淡出方式
public enum SplashType
{
LoadNextLevelThenFadeOut,
FadeOutThenLoadNextLevel
}
public SplashType splashType;
private float alpha = 0.0f;
//纹理的状态
private enum FadeStatus
{
Paused,
FadeIn,
FadeWaiting,
FadeOut
}
private FadeStatus status = FadeStatus.Paused;
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;
// }
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);
}
if(Application.levelCount <= 1)
{
Debug.LogWarning("Invalid levelToLoad value.");
}
}
//开始切换,要跳转的场景和设置对应的切换速率
public void StartSplash (int level,int i)
{
status = FadeStatus.FadeIn;
levelToLoadInt=level;
SetValue(i);
}
public void setIsDealPlayer(bool isDealPlayer)
{
IsDealPlayer=isDealPlayer;
}
public void SetValue(int i)
{
switch(i)
{
case 1:
fadeSpeed=0.8f;
waitTime=1.0f;
break;
case 2:
fadeSpeed=0.8f;
waitTime=1.0f;
break;
case 3:
fadeSpeed=0.8f;
waitTime=1.0f;
break;
}
}
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*2;
if(alpha<=0.0f)
{
if(trigger!=null){
trigger();
}
status=FadeStatus.Paused;
}
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)
{
loadingNextLevel = true;
if((Application.levelCount) >= 1)
{
// print(levelToLoadInt);
Application.LoadLevel(levelToLoadInt);
if(triggerAtLoading!=null)
triggerAtLoading();
}
}
}
if(alpha < 0.0f)
{
if(splashType == SplashType.FadeOutThenLoadNextLevel)
{
if(Application.levelCount >= 1)
{
Application.LoadLevel(levelToLoadInt);
}
}
else
{
}
}
}
}
using System.Collections;
/*
* 使用方法:将该脚本附在镜头下面,然后切换场景时镜头不销毁
* 要调用的时候获得该脚本,然后调用StartSplash()
*/
//@author joi
public class SceneLoad : MonoBehaviour {
public int guiDepth = 0;
public string levelToLoad = "";
//将要加载的场景序号
public int levelToLoadInt;
//切换场景的纹理
public Texture2D splashLogo;
//淡入淡出的速度
float fadeSpeed = 0.8f;
//保持纹理最高透明度的时间
float waitTime = 0f;
//是否要等待输入
public bool waitForInput = false;
public bool startAutomatically = false;
public bool IsDealPlayer=false;
private float timeFadingInFinished = 0.0f;
//处理切换场景后需要实现的事件
public delegate void EventHandler();
public event EventHandler trigger;
public delegate void EventHandler2nd();
public event EventHandler2nd triggerAtLoading;
//淡入淡出方式
public enum SplashType
{
LoadNextLevelThenFadeOut,
FadeOutThenLoadNextLevel
}
public SplashType splashType;
private float alpha = 0.0f;
//纹理的状态
private enum FadeStatus
{
Paused,
FadeIn,
FadeWaiting,
FadeOut
}
private FadeStatus status = FadeStatus.Paused;
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;
// }
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);
}
if(Application.levelCount <= 1)
{
Debug.LogWarning("Invalid levelToLoad value.");
}
}
//开始切换,要跳转的场景和设置对应的切换速率
public void StartSplash (int level,int i)
{
status = FadeStatus.FadeIn;
levelToLoadInt=level;
SetValue(i);
}
public void setIsDealPlayer(bool isDealPlayer)
{
IsDealPlayer=isDealPlayer;
}
public void SetValue(int i)
{
switch(i)
{
case 1:
fadeSpeed=0.8f;
waitTime=1.0f;
break;
case 2:
fadeSpeed=0.8f;
waitTime=1.0f;
break;
case 3:
fadeSpeed=0.8f;
waitTime=1.0f;
break;
}
}
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*2;
if(alpha<=0.0f)
{
if(trigger!=null){
trigger();
}
status=FadeStatus.Paused;
}
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)
{
loadingNextLevel = true;
if((Application.levelCount) >= 1)
{
// print(levelToLoadInt);
Application.LoadLevel(levelToLoadInt);
if(triggerAtLoading!=null)
triggerAtLoading();
}
}
}
if(alpha < 0.0f)
{
if(splashType == SplashType.FadeOutThenLoadNextLevel)
{
if(Application.levelCount >= 1)
{
Application.LoadLevel(levelToLoadInt);
}
}
else
{
}
}
}
}
0 0
- 控制切换场景淡入淡出的教程
- Unity切换场景淡入淡出
- Unity切换场景淡入淡出效果
- 淡入淡出切换图片的实现
- jq简单的淡入淡出图片切换
- Activity 的切换效果 淡入淡出
- js控制淡入淡出
- Unity3D淡入淡出场景跳转
- Unity3D特效-场景淡入淡出
- Unity3D特效-场景淡入淡出
- Unity3D特效-场景淡入淡出
- Viewpager切换的淡入淡出切换(页面不移动)
- cocos2d-x 场景淡出淡入效果的具体运用
- javascript淡入淡出窗口切换
- 图片淡入淡出与切换
- 图片淡入淡出切换效果
- iOS view淡入淡出切换
- Activity切换淡入淡出效果
- Head First 设计模式(1):策略模式
- Java如何连接Access数据库(两种方式实例代码)
- Irrlicht 3D Engine 笔记系列 之 教程6- 2D Graphics
- 两种用于派生的Singleton模式(以TextureMgr为例)
- 黑马程序员-----IO流
- 控制切换场景淡入淡出的教程
- Adaboost 算法的原理与推导(强烈推荐)
- JSON 之JAVA 解析
- 友元函数、类和运算符重载
- OC基础4:类和方法
- WWDC 2012 Session笔记——219 Advanced Collection Views and Building Custom Layouts
- Android版本与过高的JDK版本不兼容问题
- 显示网络中的图片
- 从今天开始正式向软件开发进军