之前写了一篇关于切换场景的黑变白的跳转,但是想更好一点,就做了下面的效果:唯一不足的地方,就是掉帧比较严重,跳转掉了20+帧,有大神如果能优化,麻烦留言告诉下怎么做
image是一个遮挡全屏的2DUI,初始Color的a为0;
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class JumpScreenAnimator : MonoBehaviour {
public Image _image;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
}
IEnumerator GetPictureNow()
{
yield return new WaitForEndOfFrame();
Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); //创建一个新的Texture2D
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); //读取屏幕像素,并保存
tex.Apply();
Sprite sp = Sprite.Create(tex, new Rect(0, 0, Screen.width, Screen.height), Vector2.zero); //Texture2D转换为Sprite
_image.sprite = sp;
print(sp);
StartCoroutine("spriteAnimator");
}
IEnumerator spriteAnimator()
{
_image.color = new Color(_image.color.r, _image.color.g, _image.color.b, 1);
float a = 1;
while (_image.color.a > 0)
{
a -= 0.1f;
_image.color = new Color(_image.color.r, _image.color.g, _image.color.b, a);
yield return new WaitForSeconds(0.05f);
}
}
public void JumpTestScreen()
{
SceneManager.LoadSceneAsync("Testscene");
DontDestroyOnLoad(this.gameObject);
StartCoroutine(GetPictureNow());
}
public void JumpFiveScreen()
{
SceneManager.LoadSceneAsync("FiveScene");
DontDestroyOnLoad(this.gameObject);
StartCoroutine(GetPictureNow());
}
}