1. Effect display
2. Scene construction
3. Code
1. SceneNameDrawer.cs: You can choose the scene you want to switch to avoid manual input errors
using UnityEngine;
using UnityEditor;
using System.Linq;
public class SceneNameAttribute : PropertyAttribute {
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(SceneNameAttribute))]
public class SceneNameDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
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();
index = EditorGUI.Popup(position, label.text, index, scenes);
if (EditorGUI.EndChangeCheck())
{
property.stringValue = scenes[index];
}
}
}
#endif
2. Progress bar.cs: Realize the progress bar scrolling
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;
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;
}
}
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);
}
}
}