Unity实现场景加载进度条

1、效果展示

进度条

2、场景搭建

搭建场景

3、代码

1、SceneNameDrawer.cs:实现可以选择你想要切换的场景,避免手动输入错误

using UnityEngine; // 引入Unity引擎命名空间
using UnityEditor; // 引入Unity编辑器命名空间
using System.Linq; // 引入LINQ命名空间,用于处理集合

public class SceneNameAttribute : PropertyAttribute { } // 场景名称属性,继承自PropertyAttribute

#if UNITY_EDITOR // 编译指令,如果是在Unity编辑器中编译则进入下面的代码块
[CustomPropertyDrawer(typeof(SceneNameAttribute))] // 自定义属性绘制器,指定处理SceneNameAttribute属性
public class SceneNameDrawer : PropertyDrawer // 场景名称绘制器,继承自PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) // 重写PropertyDrawer类的OnGUI方法
    {
        // 获取Build Settings中的所有场景,过滤掉路径为空的场景并提取场景名称,生成场景名称数组
        var scenes = EditorBuildSettings.scenes
            .Where(s => !string.IsNullOrEmpty(s.path))
            .Select(s => System.IO.Path.GetFileNameWithoutExtension(s.path))
            .ToArray();

        // 获取当前序列化属性的值,查找其在场景名称数组中的索引
        var index = Mathf.Max(0, System.Array.IndexOf(scenes, property.stringValue));

        // 在编辑器中绘制一个弹出式下拉菜单,用于选择场景
        EditorGUI.BeginChangeCheck(); // 开始检查编辑器UI是否发生了变化
        index = EditorGUI.Popup(position, label.text, index, scenes); // 绘制弹出式下拉菜单
        if (EditorGUI.EndChangeCheck()) // 如果编辑器UI发生了变化
        {
            property.stringValue = scenes[index]; // 更新序列化属性的值
        }
    }
}
#endif // 结束编译指令

2、进度条.cs:实现进度条滚动

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

public class 进度条 : MonoBehaviour
{
    public Slider slider;

    public GameObject startButton;

    public bool really = true;//是否是真实的加载

    [SceneName]
    public string sceneName; //你想要切换的场景

    private float currentProgress;//当前进度

    private float loadingTime = 2;//虚假的加载时间

    private AsyncOperation operation;

    // Start is called before the first frame update
    void Start()
    {
        startButton.SetActive(false);

        startButton.GetComponent<Button>().onClick.AddListener(OnStartButtonClick);

        currentProgress = 0;

        slider.value = currentProgress;

        if (really)
        {
            operation = SceneManager.LoadSceneAsync(sceneName);
            operation.allowSceneActivation = false;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (!really)
        {
            currentProgress += Time.deltaTime / loadingTime;
            if (currentProgress > 1)
            {
                currentProgress = 1;
            }
        }
        else
        {
            currentProgress = Mathf.Clamp01(operation.progress / 0.9f);
        }
        OnSliderValueChange(currentProgress);
    }

    private void OnStartButtonClick()
    {
        if (!really)
        {
            SceneManager.LoadScene(sceneName);
        }
        else
        {
            operation.allowSceneActivation = true;
        }
    }

    private void OnSliderValueChange(float value)//改变滑动条的值
    {
        slider.value = value;
        if (value >= 1)
        {
            startButton.SetActive(true);
        }
    }
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值