我将设计一个Unity3D游戏客户端框架的多场景管理系统,包含场景加载、进度显示和过渡动画等功能。
对惹,这里有一个游戏开发交流小组984358500,希望大家可以点击进来一起交流一下开发经验呀!
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class SceneTransitionManager : MonoBehaviour
{
public static SceneTransitionManager Instance { get; private set; }
[Header("UI References")]
public CanvasGroup loadingScreen;
public Slider progressBar;
public Text progressText;
public Text tipText;
public Image loadingIcon;
[Header("Settings")]
public float fadeDuration = 0.5f;
public float minLoadTime = 1.5f;
public List<string> loadingTips = new List<string>
{
"小提示:探索场景可以找到隐藏道具",
"小提示:按ESC键可以打开设置菜单",
"小提示:完成每日任务可获得额外奖励"
};
private AsyncOperation currentAsyncOperation;
private bool isLoading;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
// 加载单个场景(替换当前场景)
public void LoadScene(string sceneName)
{
if (isLoading) return;
StartCoroutine(LoadSceneCoroutine(sceneName, LoadSceneMode.Single));
}
// 叠加加载场景(不卸载当前场景)
public void LoadSceneAdditive(string sceneName)
{
if (isLoading) return;
StartCoroutine(LoadSceneCoroutine(sceneName, LoadSceneMode.Additive));
}
// 卸载指定场景
public void UnloadScene(string sceneName)
{
StartCoroutine(UnloadSceneCoroutine(sceneName));
}
private IEnumerator LoadSceneCoroutine(string sceneName, LoadSceneMode mode)
{
isLoading = true;
// 显示随机提示
tipText.text = loadingTips[Random.Range(0, loadingTips.Count)];
// 淡入加载界面
yield return StartCoroutine(FadeLoadingScreen(0, 1, fadeDuration));
float timer = 0f;
progressBar.value = 0;
progressText.text = "0%";
// 开始异步加载场景
currentAsyncOperation = SceneManager.LoadSceneAsync(sceneName, mode);
currentAsyncOperation.allowSceneActivation = false;
// 旋转加载图标
StartCoroutine(RotateLoadingIcon());
while (!currentAsyncOperation.isDone)
{
timer += Time.deltaTime;
// 计算加载进度(0-0.9)
float loadProgress = Mathf.Clamp01(currentAsyncOperation.progress / 0.9f);
float percent = Mathf.Clamp01((timer / minLoadTime) * loadProgress);
progressBar.value = percent;
progressText.text = Mathf.Round(percent * 100) + "%";
// 加载完成但等待最小加载时间
if (currentAsyncOperation.progress >= 0.9f && timer >= minLoadTime)
{
progressBar.value = 1;
progressText.text = "100%";
currentAsyncOperation.allowSceneActivation = true;
}
yield return null;
}
// 淡出加载界面
yield return StartCoroutine(FadeLoadingScreen(1, 0, fadeDuration));
isLoading = false;
}
private IEnumerator UnloadSceneCoroutine(string sceneName)
{
AsyncOperation unloadOperation = SceneManager.UnloadSceneAsync(sceneName);
while (!unloadOperation.isDone)
{
yield return null;
}
}
private IEnumerator FadeLoadingScreen(float startAlpha, float endAlpha, float duration)
{
float time = 0;
loadingScreen.blocksRaycasts = true;
loadingScreen.interactable = true;
while (time < duration)
{
loadingScreen.alpha = Mathf.Lerp(startAlpha, endAlpha, time / duration);
time += Time.deltaTime;
yield return null;
}
loadingScreen.alpha = endAlpha;
if (endAlpha == 0)
{
loadingScreen.blocksRaycasts = false;
loadingScreen.interactable = false;
}
}
private IEnumerator RotateLoadingIcon()
{
while (isLoading)
{
loadingIcon.transform.Rotate(0, 0, -180 * Time.deltaTime);
yield return null;
}
}
}
场景切换方法
SceneTransitionManager.Instance.LoadScene("MainScene");
叠加加载场景:
SceneTransitionManager.Instance.LoadSceneAdditive("InventoryScene");
卸载场景:
SceneTransitionManager.Instance.UnloadScene("InventoryScene");
编辑器设置
将组件拖拽到SceneTransitionManager脚本的对应字段
功能特点
最佳实践建议
资源管理:
场景通信:
性能优化:
这个多场景管理系统提供了游戏开发中常见的场景管理功能,可以作为游戏框架的核心组件之一。您可以根据项目需求扩展错误处理、场景依赖管理等功能。
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删