美文网首页
Unity两种技巧之场景叠加及场景淡入淡出

Unity两种技巧之场景叠加及场景淡入淡出

作者: 银河的精神家园 | 来源:发表于2018-05-19 21:56 被阅读0次

在Unity中使用场景叠加的方法也许会帮助实现一些想要的效果,场景叠加用到的方法为Application.LoadLevelAdditive("SceneName");

示例

void Update()

{

if(Input.GetKey(KeyCode.Space))//按下空格

{ Application.LoadLevelAdditive("SceneName");//实现场景叠加,SceneName为要叠加的场景名 }

}

场景淡入淡出

在游戏中创建一个空物体,添加GUITexture组件,添加C#脚本名为FadeInOut

添加组件

如图给GUI Texture组件Texture与Color赋值

FadeInOut脚本:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class FadeInOut : MonoBehaviour {

public float fadeSpeed;//渐隐速度

private bool sceneStarting = true;//场景是否开始

private GUITexture tex;//添加GUITexture组件

void Start()

 {

 tex = this.GetComponent();

 tex.pixelInset = new Rect(0, 0, Screen.width, Screen.height);//初始化,位置0,0,大小屏幕大小

 }

void Update()

{

if(sceneStarting)//场景开始

{

StartScene();//调用方法

}

}

private void FadeToClear()//渐显方法

{

tex.color = Color.Lerp(tex.color, Color.clear, fadeSpeed * Time.deltaTime);

}

private void FadeToBlack()//渐隐方法

{

tex.color = Color.Lerp(tex.color, Color.black, fadeSpeed * Time.deltaTime);

}

private void StartScene()//场景开始

{

FadeToClear();

if (tex.color.a <= 0.05f)//当GUI阿尔法通道小于0.05时

{

tex.color = Color.clear;//颜色变为彻底透明

tex.enabled = false;//GUITexture组件禁用

sceneStarting = false;//标识位设置为false

}

}

public void EndScene()//场景结束

{

tex.enabled = true;//激动GUITexture

FadeToBlack();//渐显变为黑色

if (tex.color.a >= 0.95f)//当大于0.95时

{

UnityEngine.SceneManagement.SceneManager.LoadScene("SceneName");//重新加载

}

}

}

相关文章

网友评论

      本文标题:Unity两种技巧之场景叠加及场景淡入淡出

      本文链接:https://www.haomeiwen.com/subject/bywadftx.html