63 lines
1.7 KiB
C#
63 lines
1.7 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
public class LevelSwitch : MonoBehaviour
|
|
{
|
|
public LevelSwitchData switchData;
|
|
public SpriteRenderer iconRenderer;
|
|
private Interactable interactable;
|
|
|
|
void Awake()
|
|
{
|
|
if (iconRenderer == null)
|
|
iconRenderer = GetComponent<SpriteRenderer>();
|
|
|
|
interactable = GetComponent<Interactable>();
|
|
if (interactable != null)
|
|
{
|
|
interactable.Interacted += OnInteracted;
|
|
}
|
|
ApplySwitchData();
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
if (interactable != null)
|
|
{
|
|
interactable.Interacted -= OnInteracted;
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
void OnValidate()
|
|
{
|
|
if (iconRenderer == null)
|
|
iconRenderer = GetComponent<SpriteRenderer>();
|
|
ApplySwitchData();
|
|
}
|
|
#endif
|
|
|
|
public void ApplySwitchData()
|
|
{
|
|
if (switchData != null)
|
|
{
|
|
if (iconRenderer != null)
|
|
iconRenderer.sprite = switchData.mapSprite;
|
|
gameObject.name = switchData.targetLevelSceneName;
|
|
// Optionally update other fields, e.g. description
|
|
}
|
|
}
|
|
|
|
private async void OnInteracted()
|
|
{
|
|
Debug.Log($"LevelSwitch.OnInteracted: Switching to level {switchData?.targetLevelSceneName}");
|
|
if (switchData != null && !string.IsNullOrEmpty(switchData.targetLevelSceneName))
|
|
{
|
|
// Optionally: show loading UI here
|
|
var progress = new Progress<float>(p => Debug.Log($"Loading progress: {p * 100:F0}%"));
|
|
await SceneManagerService.Instance.SwitchSceneAsync(switchData.targetLevelSceneName, progress);
|
|
// Optionally: hide loading UI here
|
|
}
|
|
}
|
|
}
|