修复UI系统BUG
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace GameFramework
|
||||
{
|
||||
/// <summary>
|
||||
/// 挂到 MainMenu 场景的 EventSystem 上(或和它同一个空物体上),
|
||||
/// 让 EventSystem 通过 DontDestroyOnLoad 跨场景存活。
|
||||
/// 这样 Gameplay、Scoring 等场景的按钮都能正常响应点击。
|
||||
///
|
||||
/// 如果场景里没有 EventSystem,也可以挂到任意空物体上,
|
||||
/// 脚本会自动创建一个。
|
||||
/// </summary>
|
||||
public class PersistentEventSystem : MonoBehaviour
|
||||
{
|
||||
void Awake()
|
||||
{
|
||||
// 如果场景里没有 EventSystem,自动创建
|
||||
if (FindObjectOfType<EventSystem>() == null)
|
||||
{
|
||||
var go = new GameObject("EventSystem");
|
||||
go.AddComponent<EventSystem>();
|
||||
go.AddComponent<StandaloneInputModule>();
|
||||
}
|
||||
|
||||
// 让 EventSystem 跨场景存活
|
||||
var eventSystem = FindObjectOfType<EventSystem>();
|
||||
if (eventSystem != null)
|
||||
DontDestroyOnLoad(eventSystem.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: DngctSioBnphPjHzVSJy8JJFUED7SJpCvstbMN5AHriyvx/FqtTNVmE=
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -11,7 +11,7 @@ namespace GameFramework
|
||||
///
|
||||
/// 搭建:把 SceneLoader 挂到一个空物体上(放第一个场景 MainMenu 里),
|
||||
/// 它会 DontDestroyOnLoad。transitionImage 是它子物体 Canvas 上的 Image(全屏黑),
|
||||
/// Canvas 设为 Screen Space - Overlay、sortingOrder 设很高(如 1000)。
|
||||
/// Canvas 设为 Screen Space - Overlay、sortingOrder 设很高(如 9999)。
|
||||
/// </summary>
|
||||
public class SceneLoader : PersistentSingleton<SceneLoader>
|
||||
{
|
||||
@@ -25,18 +25,28 @@ namespace GameFramework
|
||||
[SerializeField] private string scoringScene = "Scoring";
|
||||
|
||||
private Color color;
|
||||
private bool isLoading = false;
|
||||
|
||||
public void LoadGameplayScene() => Load(gameplayScene);
|
||||
public void LoadMainMenuScene() => Load(mainMenuScene);
|
||||
public void LoadScoringScene() => Load(scoringScene);
|
||||
public void Load(string sceneName)
|
||||
{
|
||||
if (isLoading) return;
|
||||
isLoading = true;
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(LoadingCoroutine(sceneName));
|
||||
}
|
||||
|
||||
private IEnumerator LoadingCoroutine(string sceneName)
|
||||
{
|
||||
if (transitionImage == null)
|
||||
{
|
||||
Debug.LogWarning("[SceneLoader] transitionImage 未赋值! " +
|
||||
"请在 SceneLoader 子物体上创建 Canvas (Overlay, SortOrder=9999) + 全屏黑色 Image," +
|
||||
"拖到 SceneLoader 的 transitionImage 字段。");
|
||||
}
|
||||
|
||||
var loadingOperation = SceneManager.LoadSceneAsync(sceneName);
|
||||
loadingOperation.allowSceneActivation = false;
|
||||
|
||||
@@ -46,20 +56,32 @@ namespace GameFramework
|
||||
color = transitionImage.color;
|
||||
color.a = 0f;
|
||||
|
||||
// 淡入到黑
|
||||
// 淡入到黑(LoadSceneAsync 在后台并行加载)
|
||||
while (color.a < 1f)
|
||||
{
|
||||
color.a = Mathf.Clamp01(color.a + Time.unscaledDeltaTime / fadeTime);
|
||||
transitionImage.color = color;
|
||||
yield return null;
|
||||
}
|
||||
color.a = 1f;
|
||||
transitionImage.color = color;
|
||||
|
||||
// 淡入完成后,确保场景已加载就绪(大多数情况下已经加载好了)
|
||||
yield return new WaitUntil(() => loadingOperation.progress >= 0.9f);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 没有过渡图,直接等加载
|
||||
yield return new WaitUntil(() => loadingOperation.progress >= 0.9f);
|
||||
}
|
||||
|
||||
// 等异步加载到 90%
|
||||
yield return new WaitUntil(() => loadingOperation.progress >= 0.9f);
|
||||
|
||||
loadingOperation.allowSceneActivation = true;
|
||||
|
||||
// 等待场景切换完全完成(旧场景卸载 + 新场景渲染就绪)
|
||||
// 没有这一步会在场景交接时闪一帧旧场景的内容
|
||||
yield return null;
|
||||
yield return null;
|
||||
|
||||
if (transitionImage != null)
|
||||
{
|
||||
// 淡出到透明
|
||||
|
||||
@@ -18,11 +18,13 @@ namespace GameFramework
|
||||
|
||||
Collider pickupCollider;
|
||||
MeshRenderer meshRenderer;
|
||||
SpriteRenderer spriteRenderer;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
pickupCollider = GetComponent<Collider>();
|
||||
meshRenderer = GetComponentInChildren<MeshRenderer>();
|
||||
spriteRenderer = GetComponentInChildren<SpriteRenderer>();
|
||||
// 确保有触发器
|
||||
if (pickupCollider != null)
|
||||
pickupCollider.isTrigger = true;
|
||||
@@ -30,11 +32,16 @@ namespace GameFramework
|
||||
|
||||
void OnTriggerEnter(Collider other)
|
||||
{
|
||||
Debug.Log($"[ScorePickup] 触发碰撞: {other.gameObject.name}, Tag={other.tag}");
|
||||
if (!other.CompareTag("Player")) return;
|
||||
|
||||
Debug.Log($"[ScorePickup] 玩家碰撞! ScoreManager={(ScoreManager.Instance != null ? "存在" : "NULL")}");
|
||||
|
||||
// 加分
|
||||
if (ScoreManager.Instance != null)
|
||||
ScoreManager.Instance.AddScore(scoreValue);
|
||||
else
|
||||
Debug.LogWarning("[ScorePickup] ScoreManager 未挂载! 请在 MainMenu 场景的空 GameObject 上挂载 ScoreManager 组件。");
|
||||
|
||||
// 音效
|
||||
if (AudioManager.Instance != null && pickUpSFX != null)
|
||||
@@ -58,6 +65,8 @@ namespace GameFramework
|
||||
pickupCollider.enabled = visible;
|
||||
if (meshRenderer != null)
|
||||
meshRenderer.enabled = visible;
|
||||
if (spriteRenderer != null)
|
||||
spriteRenderer.enabled = visible;
|
||||
}
|
||||
|
||||
void Respawn()
|
||||
|
||||
Reference in New Issue
Block a user