189 8069 5689

Unity实现场景加载渐入渐出效果-创新互联

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

创新互联是一家成都网站制作、网站建设,提供网页设计,网站设计,网站制作,建网站,按需求定制设计,网站开发公司,从2013年创立是互联行业建设者,服务者。以提升客户品牌价值为核心业务,全程参与项目的网站策划设计制作,前端开发,后台程序制作以及后期项目运营并提出专业建议和思路。

首先,创建两个场景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  ().BeginFade (1);
        yield return new WaitForSeconds (time);
        SceneManager.LoadSceneAsync ("Game");
    }

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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享文章:Unity实现场景加载渐入渐出效果-创新互联
文章出自:http://cdxtjz.cn/article/icjsc.html

其他资讯