Unity实现场景加载渐入渐出效果

发布时间:2020-04-03 03:59:23 作者:速度速度撒
来源:网络 阅读:784

项目中要用到加载场景的时候有个渐入渐出的效果,做了一下,跟大家分享

首先,创建两个场景Main和Game场景;

其次,在Main场景中创建FandeScene.cs脚本,创建Fade空对象,挂载,给一张黑色的图片,拖成预设体,同样也拖到Game场景中。

using UnityEngine;
using System.Collections;


public class FadeScene : MonoBehaviour {

    public Texture blackTexture;
    private float alpha = 1.0f;
    public float fadespeed = 0.2f;
    private int fadeDir = -1;
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }

    void OnGUI()
    {
            alpha += fadeDir * fadespeed * Time.deltaTime;
            GUI.color = new Color (GUI.color .r ,GUI.color .g ,GUI.color .b,alpha);
            GUI.DrawTexture (new Rect (0,0,Screen .width ,Screen .height), blackTexture);
    }

    public float BeginFade(int direction)
    {
        fadeDir = direction;
        return 1 / fadespeed;
    }

    void OnLevelWasLoaded()
    {
        Debug.Log ("场景加载完毕!");
        BeginFade (-1);  
    }

}

再次,加载场景

协程加载
    IEnumerator FadeLoadScene()
    {
        float time = GameObject.Find ("Fade").GetComponent <FadeScene> ().BeginFade (1);
        yield return new WaitForSeconds (time);
        SceneManager.LoadSceneAsync ("Game");
    }

这样运行,就会出现渐入渐出的效果。

推荐阅读:
  1. Unity场景打AssetBundle包,加载后天空盒材质丢
  2. 关于Unity异步加载场景WebGl端需注意的

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

private frame public

上一篇:python学习笔记4:变量作用域

下一篇:ProgressDialog+Thread实现弹出进度条数据异步加载(配有效果图)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》