Skip to content

Commit

Permalink
fix: scene linking for scenes loaded by path (#41)
Browse files Browse the repository at this point in the history
* fix loading scenes by path

* improve loadsceneinfoname debug name
  • Loading branch information
joaoborks authored Jul 31, 2024
1 parent b8167bd commit 7dc5641
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ namespace MyGameDevTools.SceneLoading
{
public readonly LoadSceneInfoType Type => LoadSceneInfoType.Name;

public readonly object Reference => _sceneName;
public readonly object Reference => _sceneNameOrPath;

readonly string _sceneName;
readonly string _sceneNameOrPath;

/// <summary>
/// Creates a new <see cref="ILoadSceneInfo"/> based on the scene's name.
/// Creates a new <see cref="ILoadSceneInfo"/> based on the scene's name or path.
/// The scene must be added to the Build Settings in order to be loaded.
/// </summary>
/// <param name="sceneName">`The scene's asset name, as displayed in the Build Settings window.</param>
public LoadSceneInfoName(string sceneName)
/// <param name="sceneNameOrPath">`The scene's asset name or path, as displayed in the Build Settings window.</param>
public LoadSceneInfoName(string sceneNameOrPath)
{
if (string.IsNullOrWhiteSpace(sceneName))
throw new ArgumentException("Cannot create a LoadSceneInfoName from an empty string.", nameof(sceneName));
_sceneName = sceneName;
if (string.IsNullOrWhiteSpace(sceneNameOrPath))
throw new ArgumentException("Cannot create a LoadSceneInfoName from an empty string.", nameof(sceneNameOrPath));
_sceneNameOrPath = sceneNameOrPath;
}

public bool CanBeReferenceToScene(Scene scene) => scene.name == _sceneName;
public bool CanBeReferenceToScene(Scene scene) => scene.name == _sceneNameOrPath || scene.path == _sceneNameOrPath;

public override string ToString()
{
return $"Scene with name '{_sceneName}'";
return $"Scene with name/path '{_sceneNameOrPath}'";
}

public bool Equals(ILoadSceneInfo other)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnitySceneManager = UnityEngine.SceneManagement.SceneManager;
Expand Down Expand Up @@ -69,7 +70,7 @@ public static void LinkLoadedScenesWithSceneDataArray(ISceneData[] sceneDataArra

if (unmatchedSceneDatas.Count > 0)
{
Debug.LogError($"Unable to link all scene datas to loaded scenes. Linked {sceneDataCount - unmatchedSceneDatas.Count}/{sceneDataCount}.");
throw new Exception($"Unable to link all scene datas to loaded scenes. Linked {sceneDataCount - unmatchedSceneDatas.Count}/{sceneDataCount}.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ namespace MyGameDevTools.SceneLoading.Tests
public static class SceneBuilder
{
public static readonly string[] SceneNames = new string[] { "loading", "sceneA", "sceneB", "sceneC" };
public static readonly string[] ScenePaths = new string[]
{
SceneTestEnvironment.ScenePathBase + "/" + SceneNames[0] + ".unity",
SceneTestEnvironment.ScenePathBase + "/" + SceneNames[1] + ".unity",
SceneTestEnvironment.ScenePathBase + "/" + SceneNames[2] + ".unity",
SceneTestEnvironment.ScenePathBase + "/" + SceneNames[3] + ".unity",
};

#if UNITY_EDITOR
static readonly string _scenePathFormat = "/{0}.unity";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace MyGameDevTools.SceneLoading.Tests
{
public class SceneTestEnvironment : IPrebuildSetup, IPostBuildCleanup
{
public const string ScenePathBase = "Assets/_test";
public const int DefaultTimeout = 3000;

public static readonly ILoadSceneInfo[][] MultipleLoadSceneInfoList = new ILoadSceneInfo[][]
Expand All @@ -31,13 +32,15 @@ public class SceneTestEnvironment : IPrebuildSetup, IPostBuildCleanup
new LoadSceneInfoAddress(SceneBuilder.SceneNames[2]),
new LoadSceneInfoAddress(SceneBuilder.SceneNames[3]),
#endif
new LoadSceneInfoName(SceneBuilder.ScenePaths[3])
},
// This list of scenes expects two load scene infos that point to the same source scene,
// and validates whether that causes any issues when linking to the target loaded scene.
new ILoadSceneInfo[]
{
new LoadSceneInfoIndex(1),
new LoadSceneInfoName(SceneBuilder.SceneNames[1]),
new LoadSceneInfoName(SceneBuilder.ScenePaths[1]),
#if ENABLE_ADDRESSABLES
// Since we can't test statically with AssetReference, we should at least validate
// that two AsyncOperations with the same addressable source do not cause issues.
Expand All @@ -50,6 +53,7 @@ public class SceneTestEnvironment : IPrebuildSetup, IPostBuildCleanup
public static readonly ILoadSceneInfo[] SingleLoadSceneInfoList = new ILoadSceneInfo[]
{
new LoadSceneInfoName(SceneBuilder.SceneNames[1]),
new LoadSceneInfoName(SceneBuilder.ScenePaths[1]),
new LoadSceneInfoIndex(1),
#if ENABLE_ADDRESSABLES
new LoadSceneInfoAddress(SceneBuilder.SceneNames[1]),
Expand All @@ -62,7 +66,6 @@ public class SceneTestEnvironment : IPrebuildSetup, IPostBuildCleanup
};

#if UNITY_EDITOR
const string _scenePathBase = "Assets/_test";
#if ENABLE_ADDRESSABLES
const string _addressableScenePathBase = "Assets/_addressables-test";
const string _sceneReferencePath = _addressableScenePathBase + "/sceneReference.asset";
Expand All @@ -75,7 +78,7 @@ public void Setup()
int sceneCount = SceneBuilder.SceneNames.Length;
List<EditorBuildSettingsScene> buildScenes = new(sceneCount);

if (!SceneBuilder.TryBuildScenes(_scenePathBase, (i, s, p) => buildScenes.Add(new EditorBuildSettingsScene(p, true))))
if (!SceneBuilder.TryBuildScenes(ScenePathBase, (i, s, p) => buildScenes.Add(new EditorBuildSettingsScene(p, true))))
return;

Debug.Log("Adding test scenes to build settings:\n" + string.Join("\n", buildScenes.Select(scene => scene.path)));
Expand Down Expand Up @@ -104,12 +107,12 @@ public void Setup()
public void Cleanup()
{
#if UNITY_EDITOR
EditorBuildSettings.scenes = EditorBuildSettings.scenes.Where(scene => !scene.path.StartsWith(_scenePathBase)).ToArray();
EditorBuildSettings.scenes = EditorBuildSettings.scenes.Where(scene => !scene.path.StartsWith(ScenePathBase)).ToArray();

if (!Directory.Exists(_scenePathBase))
if (!Directory.Exists(ScenePathBase))
return;

AssetDatabase.DeleteAsset(_scenePathBase);
AssetDatabase.DeleteAsset(ScenePathBase);
AssetDatabase.Refresh();

#if ENABLE_ADDRESSABLES
Expand Down

0 comments on commit 7dc5641

Please sign in to comment.