To allow selecting or dragging/dropping a scene into the LevelSO Scriptable Object, you can use Unity's SceneAsset type. This will enable you to assign a scene directly in the Unity Editor. Here's how you can modify the LevelSO to include a scene field:
1. Modify LevelSO to Include SceneAsset
using UnityEngine;
using UnityEngine.SceneManagement;
[CreateAssetMenu(fileName = "Level_", menuName = "Level System/Level Data")]
public class LevelSO : ScriptableObject, ILevelInfo
{
[SerializeField] private string levelId;
[SerializeField] private string displayName;
[SerializeField] private SceneAsset sceneAsset; // Change from string to SceneAsset
[SerializeField] private LevelDifficulty difficulty;
public string LevelId => levelId;
public string DisplayName => displayName;
public string SceneName => sceneAsset ? sceneAsset.name : null; // Get the scene name from the SceneAsset
public enum LevelDifficulty
{
Easy,
Medium,
Hard
}
}
2. Update LevelLoader to Handle SceneAsset
Modify the LevelLoader to handle the SceneAsset type:
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class LevelLoader : MonoBehaviour, ILevelLoader
{
[SerializeField] private LevelProgressSO progressData;
private Dictionary<string, AsyncOperation> loadingOperations = new();
public void LoadLevel(string levelId)
{
StartCoroutine(LoadLevelAsync(levelId));
}
public void UnloadLevel(string levelId)
{
SceneManager.UnloadSceneAsync(GetSceneName(levelId));
}
public bool IsLevelLoaded(string levelId) =>
SceneManager.GetSceneByPath(GetSceneName(levelId)).isLoaded;
private IEnumerator LoadLevelAsync(string levelId)
{
var levelData = GetLevelData(levelId);
if (levelData == null) yield break;
var asyncLoad = SceneManager.LoadSceneAsync(levelData.SceneName, LoadSceneMode.Additive);
loadingOperations[levelId] = asyncLoad;
while (!asyncLoad.isDone)
{
yield return null;
}
progressData.CurrentLevelId = levelId;
loadingOperations.Remove(levelId);
}
private LevelSO GetLevelData(string levelId)
{
// Implement your level data lookup logic here
// Could be a SO array, addressables, or other system
return Resources.Load<LevelSO>($"Levels/{levelId}");
}
private string GetSceneName(string levelId)
{
var data = GetLevelData(levelId);
return data != null ? data.SceneName : null;
}
}
3. Using SceneAsset in the Unity Editor
-
Create a SceneAsset:
- In the Unity Editor, right-click in the Project window and select
Create > Scene.
- Name your scene (e.g.,
Level_01).
- Save the scene.
-
Assign the SceneAsset to LevelSO:
- Create a new
LevelSO Scriptable Object.
- In the Inspector, you will see a field for
SceneAsset.
- Drag and drop the scene you created into this field.
4. Example Usage
Here's how you can use the LevelSO and LevelLoader in your game:
public class GameController : MonoBehaviour
{
[SerializeField] private LevelLoader loader;
[SerializeField] private LevelProgressSO progress;
void Start()
{
LoadInitialLevel();
}
public void OnLevelComplete()
{
progress.CompleteLevel(progress.CurrentLevelId);
loader.UnloadLevel(progress.CurrentLevelId);
LoadNextLevel();
}
private void LoadInitialLevel()
{
loader.LoadLevel("Level_01");
}
private void LoadNextLevel()
{
// Implement your level selection logic
loader.LoadLevel("Level_02");
}
}
Summary
By using SceneAsset, you can easily drag and drop scenes into the LevelSO in the Unity Editor. This approach provides a more intuitive and error-free way to manage scene assignments for your levels. The LevelLoader and other components remain largely unchanged, ensuring a smooth integration with the rest of your level loading system.