赞
踩
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 // 结束编译指令
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); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。