Unity场景的异步加载

版权声明:他人成果,为我所用。留下足迹,美名杰成。 https://blog.csdn.net/weixin_41814169/article/details/87934907

第一步:创建三个场景,分别为 A B C

第二步:shift+ctrl+b 分别把三个场景加载进去

第三步:开始干活

场景 A 是开始场景

场景B 是中间过渡场景

场景C 是最终需要到达(加载)的场景

准备好脚本:

我没有用button,而是用的gameobject作为点击事件 所以就有一点不同

AsyncLoadScene(此脚本是核心内容)

CubeEventItemBind(此脚本是cube的点击事件)

AddEeventMonoTest(此脚本是cube添加点击事件)

直接上


using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Globe
{
    public static string nextSceneName ;
}

public class AsyncLoadScene : MonoBehaviour
{
    public Slider loadingSlider;

    public Text loadingText;

    private float loadingSpeed = 1;

    private float targetValue;

    private AsyncOperation operation;

    // Use this for initialization
    void Start()
    {
        loadingSlider.value = 0.0f;

        if (SceneManager.GetActiveScene().name == "B")
        {
            //启动协程
            StartCoroutine(AsyncLoading());
        }
    }

    IEnumerator AsyncLoading()
    {
        operation = SceneManager.LoadSceneAsync("Scenen");
        //阻止当加载完成自动切换
        operation.allowSceneActivation = false;

        yield return operation;
    }

    // Update is called once per frame
    void Update()
    {
        targetValue = operation.progress;

        if (operation.progress >= 0.9f)
        {
            //operation.progress的值最大为0.9
            targetValue = 1.0f;
        }

        if (targetValue != loadingSlider.value)
        {
            //插值运算
            loadingSlider.value = Mathf.Lerp(loadingSlider.value, targetValue, Time.deltaTime * loadingSpeed);
            if (Mathf.Abs(loadingSlider.value - targetValue) < 0.01f)
            {
                loadingSlider.value = targetValue;
            }
        }

        loadingText.text = ((int)(loadingSlider.value * 100)).ToString() + "%";

        if ((int)(loadingSlider.value * 100) == 100)
        {
            //允许异步加载完毕后自动切换场景
            operation.allowSceneActivation = true;
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
/// <summary>
/// function:具体  cube上 的点击响应事件
/// </summary>
public class CubeEventItemBind : MonoBehaviour
{

    public string paramStr = "";

    public void OnClickCubeItem(UnityEngine.EventSystems.BaseEventData data = null)
    {
        //SceneManager.LoadScene("");


        //保存需要加载的目标场景

        //Globe.nextSceneName = "Scene";

        SceneManager.LoadScene("B");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

/// <summary>
/// 给指定的BoxCollider添加点击事件
/// </summary>
public class AddEeventMonoTest : MonoBehaviour
{

    public GameObject targetGameObject;

    // Use this for initialization
    void Start()
    {
        targetGameObject = GameObject.Find("Cube");

        //Camera上必须要有 PhysicsRaycaster,EventSystem,StandaloneInputModule 组件
        if (this.gameObject.GetComponent<PhysicsRaycaster>() == null)
        {
            gameObject.AddComponent<PhysicsRaycaster>();
        }

        if (this.gameObject.GetComponent<EventSystem>() == null)
        {
            this.gameObject.AddComponent<EventSystem>();
        }

        if (this.gameObject.GetComponent<StandaloneInputModule>() == null)
        {
            gameObject.AddComponent<StandaloneInputModule>();
        }
    }

    private void FixedUpdate()
    {
        AddObjectClickEvent(targetGameObject);
    }

    /// <summary>
    /// 添加组件,BoxCollider,CubeEventItemBind,EventTrigger 和事件函数
    /// </summary>
    /// <param name="itemObject"></param>
    public void AddObjectClickEvent(GameObject itemObject)
    {
        var box = itemObject.GetComponent<BoxCollider>();
        if (box == null)
        {
            box = itemObject.AddComponent<BoxCollider>();
        }

        var item = itemObject.GetComponent<CubeEventItemBind>();
        if (item == null)
            item = itemObject.AddComponent<CubeEventItemBind>();
        item.paramStr = "哈哈";

        EventTrigger trigger = itemObject.GetComponent<EventTrigger>();
        if (trigger == null)
            trigger = itemObject.AddComponent<EventTrigger>();

        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.PointerClick;
        UnityEngine.Events.UnityAction<BaseEventData> click = new UnityEngine.Events.UnityAction<BaseEventData>(item.OnClickCubeItem);
        entry.callback.AddListener(click);

        trigger.triggers.Clear();
        trigger.triggers.Add(entry);

        
    }

    //private void OnGUI()
    //{
    //    if (GUILayout.Button("给 GameObject=" + targetGameObject + ",添加点击事件!!"))
    //    {
    //        Debug.Log("给 " + targetGameObject + ",添加事件");
    //        AddObjectClickEvent(targetGameObject);
    //    }
    //}
}

②返回unity

在A场景创建一个 Cube

在B场景创建一个Slider 和一个Text

在C场景。。。无所谓了,都可以,最后到的场景你认识就行

A场景

Camera添加以下组件和代码

cube添加脚本:

B场景

给Camera添加组件

slider的位置调整好,Text也调整好

在slider调整位置的时候,怎么样才能让进度条牢牢的在屏幕最下方呢?

解决方案:

选中画布,如下操作

然后在选中slider,切记要同时按下Alt键,然后在调整大小,应该就没问题了

C场景:............空空如也

结果:点击A场景的cube,跳转到B场景加载进度条,待进度条加载完成,跳转到C场景

猜你喜欢

转载自blog.csdn.net/weixin_41814169/article/details/87934907