主界面添加,许多bug修复。祝贺黑盒1如期开始
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
using System.Collections;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
using Spine.Unity;
|
||||
|
||||
/// <summary>
|
||||
/// Throttled preload: uses queues and concurrency limits to load scenes and Resources in small time slices
|
||||
@@ -16,21 +17,23 @@ public class preload_allScene : MonoBehaviour
|
||||
public string[] resourcesToPreload = new string[0];
|
||||
|
||||
[Header("UI")]
|
||||
public GameObject spineObject1;
|
||||
public GameObject spineObject2;
|
||||
public CanvasGroup spineObject1CanvasGroup;
|
||||
public float spineObject1DestroyDelay = 6.7f;
|
||||
public float spineObject1FadeDelay = 5.7f;
|
||||
public float spineObject2PlayDelay = 0f;
|
||||
public Text progressText;
|
||||
[Tooltip("Optional Image whose fillAmount will be driven by the overall progress (0..1)")]
|
||||
public Image progressFillImage;
|
||||
[Tooltip("Optional Image to disable after 1 second")]
|
||||
public Image startDisableImage;
|
||||
[Tooltip("Documentation text normalized.")]
|
||||
public Text loadingInfoText;
|
||||
|
||||
[Header("Behavior")]
|
||||
public bool startOnAwake = true;
|
||||
|
||||
[Header("Fade-out on complete")]
|
||||
[Tooltip("GameObject to disable after fade completes")] public GameObject fadeOutObject;
|
||||
[Tooltip("CanvasGroup to fade (if null and fadeOutObject assigned, will search for one on it)")] public CanvasGroup fadeCanvasGroup;
|
||||
[Tooltip("Duration of fade-out in seconds")] public float fadeOutDuration = 0.5f;
|
||||
[Tooltip("Delay before starting fade-out after completion")] public float fadeOutDelay = 0f;
|
||||
|
||||
[Header("Throttle")]
|
||||
public int maxConcurrentResourceLoads = 2;
|
||||
[Tooltip("Max milliseconds spent per frame on driver work (approx)")]
|
||||
@@ -50,15 +53,12 @@ public class preload_allScene : MonoBehaviour
|
||||
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)
|
||||
// 确保物体 1 初始处于关闭状态,以便 1 秒后延迟开启
|
||||
if (spineObject1 != null)
|
||||
{
|
||||
fadeOutObject.SetActive(true);
|
||||
spineObject1.SetActive(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("[preload_allScene] fadeOutObject is not assigned!");
|
||||
}
|
||||
|
||||
|
||||
// if UI not assigned, try to auto-find Text components
|
||||
if (progressText == null)
|
||||
{
|
||||
@@ -78,21 +78,139 @@ public class preload_allScene : MonoBehaviour
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If a fade object is set but no CanvasGroup assigned, try to find one on the object
|
||||
if (fadeOutObject != null && fadeCanvasGroup == null)
|
||||
{
|
||||
fadeCanvasGroup = fadeOutObject.GetComponent<CanvasGroup>() ?? fadeOutObject.GetComponentInChildren<CanvasGroup>(true);
|
||||
}
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
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
|
||||
|
||||
// 同时启用并初始化两个 Spine 物体
|
||||
if (spineObject1 != null)
|
||||
{
|
||||
// Start 时显式禁用物体 1,确保初始状态正确
|
||||
spineObject1.SetActive(false);
|
||||
// 等待 1 秒后再播放 Spine 1
|
||||
StartCoroutine(DelayedPlaySpine1());
|
||||
}
|
||||
|
||||
if (spineObject2 != null)
|
||||
{
|
||||
// Start 时显式禁用物体 2,确保初始状态正确
|
||||
spineObject2.SetActive(false);
|
||||
// 延迟播放物体 2 的动画
|
||||
StartCoroutine(DelayedPlaySpine2Logic());
|
||||
}
|
||||
|
||||
// 1 秒后禁用指定的 Image
|
||||
if (startDisableImage != null)
|
||||
{
|
||||
StartCoroutine(DelayedDisableImage());
|
||||
}
|
||||
|
||||
StartPreload();
|
||||
}
|
||||
|
||||
private IEnumerator DelayedDisableImage()
|
||||
{
|
||||
yield return new WaitForSeconds(1f);
|
||||
if (startDisableImage != null)
|
||||
{
|
||||
startDisableImage.gameObject.SetActive(false);
|
||||
Debug.Log("[preload_allScene] startDisableImage has been disabled after 1 second.");
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator DelayedPlaySpine1()
|
||||
{
|
||||
// 等待 1 秒
|
||||
yield return new WaitForSeconds(1f);
|
||||
|
||||
if (spineObject1 != null)
|
||||
{
|
||||
spineObject1.SetActive(true);
|
||||
var sg1 = spineObject1.GetComponentInChildren<SkeletonGraphic>(true);
|
||||
if (sg1 != null)
|
||||
{
|
||||
sg1.allowMultipleCanvasRenderers = true;
|
||||
sg1.Initialize(true);
|
||||
var state1 = (sg1.Animation as IAnimationStateComponent)?.AnimationState;
|
||||
if (state1 != null)
|
||||
{
|
||||
state1.SetAnimation(0, "intro", false);
|
||||
}
|
||||
|
||||
// 改为延迟切换到 loop 动画
|
||||
StartCoroutine(DelayedPlaySpine1Loop(sg1));
|
||||
|
||||
// 新增渐隐逻辑
|
||||
StartCoroutine(FadeOutSpine1());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator DelayedPlaySpine1Loop(SkeletonGraphic sg)
|
||||
{
|
||||
if (spineObject1DestroyDelay > 0)
|
||||
yield return new WaitForSeconds(spineObject1DestroyDelay);
|
||||
|
||||
var state1 = (sg.Animation as IAnimationStateComponent)?.AnimationState;
|
||||
if (state1 != null)
|
||||
{
|
||||
state1.SetAnimation(0, "loop", true);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator FadeOutSpine1()
|
||||
{
|
||||
if (spineObject1CanvasGroup == null) yield break;
|
||||
|
||||
if (spineObject1FadeDelay > 0)
|
||||
yield return new WaitForSeconds(spineObject1FadeDelay);
|
||||
|
||||
float duration = 1f;
|
||||
float elapsed = 0f;
|
||||
float startAlpha = spineObject1CanvasGroup.alpha;
|
||||
|
||||
while (elapsed < duration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
spineObject1CanvasGroup.alpha = Mathf.Lerp(startAlpha, 0f, elapsed / duration);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
spineObject1CanvasGroup.alpha = 0f;
|
||||
|
||||
// 渐隐结束后销毁物体 1
|
||||
if (spineObject1 != null)
|
||||
{
|
||||
Debug.Log("SpineObject1 fade out completed. Destroying object.");
|
||||
Destroy(spineObject1);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator DelayedPlaySpine2Logic()
|
||||
{
|
||||
if (spineObject2 == null) yield break;
|
||||
|
||||
// 等待指定的播放延迟
|
||||
if (spineObject2PlayDelay > 0)
|
||||
yield return new WaitForSeconds(spineObject2PlayDelay);
|
||||
|
||||
// 启用物体并初始化动画
|
||||
spineObject2.SetActive(true);
|
||||
var sg2 = spineObject2.GetComponentInChildren<SkeletonGraphic>(true);
|
||||
if (sg2 != null)
|
||||
{
|
||||
sg2.allowMultipleCanvasRenderers = true;
|
||||
sg2.Initialize(true);
|
||||
var state2 = (sg2.Animation as IAnimationStateComponent)?.AnimationState;
|
||||
if (state2 != null)
|
||||
{
|
||||
state2.SetAnimation(0, "loop", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void StartPreload()
|
||||
{
|
||||
if (preloadCoroutine != null) return;
|
||||
@@ -117,10 +235,6 @@ public class preload_allScene : MonoBehaviour
|
||||
{
|
||||
UpdateProgressText(1f);
|
||||
UpdateLoadingInfo("Loading Complete");
|
||||
if (fadeOutObject != null)
|
||||
{
|
||||
StartCoroutine(FadeAndDisableCoroutine());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -197,12 +311,6 @@ public class preload_allScene : MonoBehaviour
|
||||
preloadCoroutine = null;
|
||||
Debug.Log("Preload complete (throttled). Resources cached: " + loadedResources.Count);
|
||||
|
||||
// start fade+disable if configured
|
||||
if (fadeOutObject != null)
|
||||
{
|
||||
StartCoroutine(FadeAndDisableCoroutine());
|
||||
}
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
@@ -214,10 +322,6 @@ public class preload_allScene : MonoBehaviour
|
||||
UpdateProgressText(1f);
|
||||
UpdateLoadingInfo("Loading Complete");
|
||||
preloadCoroutine = null;
|
||||
if (fadeOutObject != null)
|
||||
{
|
||||
StartCoroutine(FadeAndDisableCoroutine());
|
||||
}
|
||||
}
|
||||
|
||||
private float CalculateOverallProgress()
|
||||
@@ -284,44 +388,4 @@ public class preload_allScene : MonoBehaviour
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator FadeAndDisableCoroutine()
|
||||
{
|
||||
// ensure we have a CanvasGroup reference
|
||||
if (fadeOutObject == null) yield break;
|
||||
|
||||
if (fadeCanvasGroup == null)
|
||||
{
|
||||
fadeCanvasGroup = fadeOutObject.GetComponent<CanvasGroup>() ?? fadeOutObject.GetComponentInChildren<CanvasGroup>(true);
|
||||
}
|
||||
|
||||
// if there's no CanvasGroup, just disable after delay
|
||||
if (fadeCanvasGroup == null)
|
||||
{
|
||||
if (fadeOutDelay > 0f) yield return new WaitForSecondsRealtime(fadeOutDelay);
|
||||
fadeOutObject.SetActive(false);
|
||||
yield break;
|
||||
}
|
||||
|
||||
// optionally wait before fading
|
||||
if (fadeOutDelay > 0f) yield return new WaitForSecondsRealtime(fadeOutDelay);
|
||||
|
||||
float startAlpha = fadeCanvasGroup.alpha;
|
||||
float elapsed = 0f;
|
||||
while (elapsed < fadeOutDuration)
|
||||
{
|
||||
elapsed += Time.unscaledDeltaTime;
|
||||
float frac = Mathf.Clamp01(elapsed / Mathf.Max(0.0001f, fadeOutDuration));
|
||||
fadeCanvasGroup.alpha = Mathf.Lerp(startAlpha, 0f, frac);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
fadeCanvasGroup.alpha = 0f;
|
||||
// disable raycasts and interaction
|
||||
fadeCanvasGroup.blocksRaycasts = false;
|
||||
fadeCanvasGroup.interactable = false;
|
||||
|
||||
// finally disable the GameObject
|
||||
fadeOutObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user