技能主要更新,修复卡顿并加入动画,以及各种其他更新。
This commit is contained in:
@@ -13,14 +13,13 @@ using UnityEngine.UI;
|
||||
public class preload_allScene : MonoBehaviour
|
||||
{
|
||||
[Header("Preload settings")]
|
||||
public string[] scenesToPreload = new string[0];
|
||||
public string[] resourcesToPreload = new string[0];
|
||||
|
||||
[Header("UI")]
|
||||
public Text progressText;
|
||||
[Tooltip("Optional Image whose fillAmount will be driven by the overall progress (0..1)")]
|
||||
public Image progressFillImage;
|
||||
[Tooltip("Optional Text to show current loading item: '正在加载:xxx'")]
|
||||
[Tooltip("Optional Text to show current loading item: '���ڼ��أ�xxx'")]
|
||||
public Text loadingInfoText;
|
||||
|
||||
[Header("Behavior")]
|
||||
@@ -33,22 +32,13 @@ public class preload_allScene : MonoBehaviour
|
||||
[Tooltip("Delay before starting fade-out after completion")] public float fadeOutDelay = 0f;
|
||||
|
||||
[Header("Throttle")]
|
||||
[Tooltip("Max concurrent resource loads")]
|
||||
public int maxConcurrentResourceLoads = 2;
|
||||
[Tooltip("Max concurrent scene loads")]
|
||||
public int maxConcurrentSceneLoads = 1;
|
||||
[Tooltip("Max milliseconds spent per frame on driver work (approx)")]
|
||||
public int maxMilliSecondsPerFrame = 4;
|
||||
|
||||
// internal queues and trackers
|
||||
private Queue<string> sceneQueue = new Queue<string>();
|
||||
private Queue<string> resourceQueue = new Queue<string>();
|
||||
|
||||
// scenes currently loading (progress < ~0.9)
|
||||
private List<(string sceneName, AsyncOperation op)> loadingSceneOps = new List<(string, AsyncOperation)>();
|
||||
// scenes loaded to 0.9 and waiting activation
|
||||
private List<(string sceneName, AsyncOperation op)> readySceneOps = new List<(string, AsyncOperation)>();
|
||||
|
||||
private List<(string path, ResourceRequest rr)> activeResourceReqs = new List<(string, ResourceRequest)>();
|
||||
|
||||
private List<Object> loadedResources = new List<Object>();
|
||||
@@ -57,7 +47,18 @@ public class preload_allScene : MonoBehaviour
|
||||
|
||||
void Awake()
|
||||
{
|
||||
fadeOutObject.SetActive(true);
|
||||
Debug.Log("[preload_allScene] Awake called. Forcing Time.timeScale = 1.");
|
||||
Time.timeScale = 1f; // Ensure time is running so fades and DOTween work
|
||||
|
||||
if (fadeOutObject != null)
|
||||
{
|
||||
fadeOutObject.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("[preload_allScene] fadeOutObject is not assigned!");
|
||||
}
|
||||
|
||||
// if UI not assigned, try to auto-find Text components
|
||||
if (progressText == null)
|
||||
{
|
||||
@@ -87,12 +88,9 @@ public class preload_allScene : MonoBehaviour
|
||||
|
||||
void Start()
|
||||
{
|
||||
// fadeOutObject.SetActive(true);
|
||||
// auto-start if configured OR there are items to preload
|
||||
if (startOnAwake || (scenesToPreload != null && scenesToPreload.Length > 0) || (resourcesToPreload != null && resourcesToPreload.Length > 0))
|
||||
{
|
||||
StartPreload();
|
||||
}
|
||||
Debug.Log("[preload_allScene] Start called. resourcesToPreload count: " + (resourcesToPreload?.Length ?? 0));
|
||||
// Always call StartPreload to ensure the screen fades out even if no resources are listed
|
||||
StartPreload();
|
||||
}
|
||||
|
||||
public void StartPreload()
|
||||
@@ -100,26 +98,32 @@ public class preload_allScene : MonoBehaviour
|
||||
if (preloadCoroutine != null) return;
|
||||
|
||||
// fill queues
|
||||
sceneQueue.Clear();
|
||||
resourceQueue.Clear();
|
||||
|
||||
if (scenesToPreload != null)
|
||||
{
|
||||
foreach (var s in scenesToPreload)
|
||||
if (!string.IsNullOrWhiteSpace(s)) sceneQueue.Enqueue(s);
|
||||
}
|
||||
if (resourcesToPreload != null)
|
||||
{
|
||||
foreach (var r in resourcesToPreload)
|
||||
if (!string.IsNullOrWhiteSpace(r)) resourceQueue.Enqueue(r);
|
||||
}
|
||||
|
||||
Debug.Log($"Preload starting. Scenes queued: {sceneQueue.Count}, Resources queued: {resourceQueue.Count}");
|
||||
Debug.Log($"Preload starting. Resources queued: {resourceQueue.Count}");
|
||||
|
||||
// ensure UI shows 0%
|
||||
UpdateProgressText(0f);
|
||||
UpdateLoadingInfo("");
|
||||
|
||||
// If nothing to preload, just show 100% and fade out immediately
|
||||
if (resourceQueue.Count == 0)
|
||||
{
|
||||
UpdateProgressText(1f);
|
||||
UpdateLoadingInfo("Loading Complete");
|
||||
if (fadeOutObject != null)
|
||||
{
|
||||
StartCoroutine(FadeAndDisableCoroutine());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
preloadCoroutine = StartCoroutine(PreloadRoutine());
|
||||
}
|
||||
|
||||
@@ -135,40 +139,15 @@ public class preload_allScene : MonoBehaviour
|
||||
private IEnumerator PreloadRoutine()
|
||||
{
|
||||
// Start initial batch
|
||||
while (sceneQueue.Count > 0 || resourceQueue.Count > 0 || loadingSceneOps.Count > 0 || activeResourceReqs.Count > 0)
|
||||
while (resourceQueue.Count > 0 || activeResourceReqs.Count > 0)
|
||||
{
|
||||
float frameStart = Time.realtimeSinceStartup;
|
||||
|
||||
// start scene ops up to concurrency
|
||||
while (loadingSceneOps.Count < maxConcurrentSceneLoads && sceneQueue.Count > 0)
|
||||
{
|
||||
string sceneName = sceneQueue.Dequeue();
|
||||
UpdateLoadingInfo($"正在加载:{sceneName}");
|
||||
AsyncOperation op = null;
|
||||
try
|
||||
{
|
||||
op = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Debug.LogWarning($"Preload: failed to start loading scene '{sceneName}': {ex}");
|
||||
}
|
||||
|
||||
if (op != null)
|
||||
{
|
||||
op.allowSceneActivation = false;
|
||||
loadingSceneOps.Add((sceneName, op));
|
||||
Debug.Log($"Preload: started scene load '{sceneName}'");
|
||||
}
|
||||
|
||||
if ((Time.realtimeSinceStartup - frameStart) * 1000f > maxMilliSecondsPerFrame) break;
|
||||
}
|
||||
|
||||
// start resource reqs up to concurrency
|
||||
while (activeResourceReqs.Count < maxConcurrentResourceLoads && resourceQueue.Count > 0)
|
||||
{
|
||||
string path = resourceQueue.Dequeue();
|
||||
UpdateLoadingInfo($"正在加载:{path}");
|
||||
UpdateLoadingInfo($"Loading: {path}");
|
||||
ResourceRequest rr = null;
|
||||
try
|
||||
{
|
||||
@@ -204,36 +183,19 @@ public class preload_allScene : MonoBehaviour
|
||||
if ((Time.realtimeSinceStartup - frameStart) * 1000f > maxMilliSecondsPerFrame) break;
|
||||
}
|
||||
|
||||
// Poll loading scenes and move to ready list when reach ~0.9
|
||||
for (int i = loadingSceneOps.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var (sceneName, op) = loadingSceneOps[i];
|
||||
if (op.progress >= 0.89f)
|
||||
{
|
||||
// move to ready list so we free a slot for next scene
|
||||
readySceneOps.Add((sceneName, op));
|
||||
loadingSceneOps.RemoveAt(i);
|
||||
Debug.Log($"Preload: scene '{sceneName}' reached 0.9 and moved to ready list");
|
||||
}
|
||||
|
||||
if ((Time.realtimeSinceStartup - frameStart) * 1000f > maxMilliSecondsPerFrame) break;
|
||||
}
|
||||
|
||||
// Update progress calculation
|
||||
float overall = CalculateOverallProgress();
|
||||
UpdateProgressText(overall);
|
||||
|
||||
// If there are no active resource requests, no loading scenes, no queued scenes/resources, and ready scenes are present (they are considered loaded), we're done
|
||||
bool anyLoadingScenes = loadingSceneOps.Count > 0 || sceneQueue.Count > 0;
|
||||
// If there are no active resource requests and no queued resources, we're done
|
||||
bool anyActiveResources = activeResourceReqs.Count > 0 || resourceQueue.Count > 0;
|
||||
|
||||
if (!anyLoadingScenes && !anyActiveResources)
|
||||
if (!anyActiveResources)
|
||||
{
|
||||
// All loading work started and finished; readySceneOps contains scenes at 0.9 waiting activation
|
||||
UpdateProgressText(1f);
|
||||
UpdateLoadingInfo("加载完成");
|
||||
UpdateLoadingInfo("Loading Complete");
|
||||
preloadCoroutine = null;
|
||||
Debug.Log("Preload complete (throttled). Scenes ready: " + readySceneOps.Count + ", resources cached: " + loadedResources.Count);
|
||||
Debug.Log("Preload complete (throttled). Resources cached: " + loadedResources.Count);
|
||||
|
||||
// start fade+disable if configured
|
||||
if (fadeOutObject != null)
|
||||
@@ -250,7 +212,7 @@ public class preload_allScene : MonoBehaviour
|
||||
|
||||
// if we exit loop, also mark complete
|
||||
UpdateProgressText(1f);
|
||||
UpdateLoadingInfo("加载完成");
|
||||
UpdateLoadingInfo("Loading Complete");
|
||||
preloadCoroutine = null;
|
||||
if (fadeOutObject != null)
|
||||
{
|
||||
@@ -263,26 +225,10 @@ public class preload_allScene : MonoBehaviour
|
||||
float sum = 0f;
|
||||
int items = 0;
|
||||
|
||||
// queued scenes count as 0
|
||||
items += sceneQueue.Count;
|
||||
// loading scenes contribute normalized progress
|
||||
items += loadingSceneOps.Count;
|
||||
// ready scenes count as completed (1)
|
||||
items += readySceneOps.Count;
|
||||
|
||||
items += resourceQueue.Count + activeResourceReqs.Count;
|
||||
|
||||
if (items == 0) return 1f;
|
||||
|
||||
foreach (var (sceneName, op) in loadingSceneOps)
|
||||
{
|
||||
float p = Mathf.Clamp01(op.progress / 0.9f);
|
||||
sum += p;
|
||||
}
|
||||
|
||||
// ready scenes are counted as full
|
||||
sum += readySceneOps.Count * 1f;
|
||||
|
||||
foreach (var (path, rr) in activeResourceReqs)
|
||||
{
|
||||
sum += rr.progress; // 0..1
|
||||
@@ -352,19 +298,19 @@ public class preload_allScene : MonoBehaviour
|
||||
// if there's no CanvasGroup, just disable after delay
|
||||
if (fadeCanvasGroup == null)
|
||||
{
|
||||
if (fadeOutDelay > 0f) yield return new WaitForSeconds(fadeOutDelay);
|
||||
if (fadeOutDelay > 0f) yield return new WaitForSecondsRealtime(fadeOutDelay);
|
||||
fadeOutObject.SetActive(false);
|
||||
yield break;
|
||||
}
|
||||
|
||||
// optionally wait before fading
|
||||
if (fadeOutDelay > 0f) yield return new WaitForSeconds(fadeOutDelay);
|
||||
if (fadeOutDelay > 0f) yield return new WaitForSecondsRealtime(fadeOutDelay);
|
||||
|
||||
float startAlpha = fadeCanvasGroup.alpha;
|
||||
float elapsed = 0f;
|
||||
while (elapsed < fadeOutDuration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
elapsed += Time.unscaledDeltaTime;
|
||||
float frac = Mathf.Clamp01(elapsed / Mathf.Max(0.0001f, fadeOutDuration));
|
||||
fadeCanvasGroup.alpha = Mathf.Lerp(startAlpha, 0f, frac);
|
||||
yield return null;
|
||||
@@ -378,24 +324,4 @@ public class preload_allScene : MonoBehaviour
|
||||
// finally disable the GameObject
|
||||
fadeOutObject.SetActive(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Activate a previously preloaded additive scene by name (sets allowSceneActivation=true)
|
||||
/// </summary>
|
||||
public void ActivatePreloadedScene(string sceneName)
|
||||
{
|
||||
for (int i = 0; i < readySceneOps.Count; i++)
|
||||
{
|
||||
if (readySceneOps[i].sceneName == sceneName)
|
||||
{
|
||||
readySceneOps[i].op.allowSceneActivation = true;
|
||||
// remove from ready list so further activations don't re-trigger
|
||||
readySceneOps.RemoveAt(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// fallback: if scene loaded and already in SceneManager, nothing to do
|
||||
Debug.LogWarning($"ActivatePreloadedScene: scene '{sceneName}' not found among ready preloaded ops.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user