mygamedevtools / scene-loader

Enhance your scene loading experience with Unity.
MIT License
94 stars 5 forks source link

[Request] Add usage samples #36

Open JGameMaker92 opened 3 months ago

JGameMaker92 commented 3 months ago

None of the runtime scripts derive from Monobehaviour. Can't even attach them to my game objects? How about some example scenes to get us set up?

joaoborks commented 3 months ago

Hi @JGameMaker92. I understand your frustration and I'm sorry I didn't have a more hands-on example. Originally, this package was meant for more advanced usage, but given that it has reached a pretty mature state already, I should have included some plug-and-play functionality. This, however, is on my roadmap and I'll include better plug-and-play features right after #34 and #35.

For now, I can give you a simple example of how it's supposed to work:

public class SceneLoaderSingleton : MonoBehaviour
{
    public static SceneLoaderSingleton Instance { get; private set; }

    public ISceneLoader SceneLoader { get; private set; }

    public void Awake()
    {
        if (Instance)
        {
            Destroy(gameObject);
            return;
        }

        ISceneManager sceneManager = new AdvancedSceneManager(true);
        // If you want to wait for scenes to load, you can also use the SceneLoaderCoroutine or SceneLoaderUniTask
        SceneLoader = new SceneLoaderAsync(sceneManager);

        // Make sure this object does not get destroyed
        Instance = this;
        DontDestroyOnLoad(gameObject);
    }
}

Attach this behavior to a gameobject in your scene. Then, whenever you want to load a scene or transition, you can:

public class MyBehavior : MonoBehaviour
{
    // Call this function with the target scene name (make sure it's included in the Build Settings)
    public void TransitionToGameScene(string targetSceneName)
    {
        ISceneLoader sceneLoader = SceneLoaderSingleton.Instance.SceneLoader;
        sceneLoader.TransitionToScene(new LoadSceneInfoName(targetSceneName));
    }

    // Use this function when you want to transition with a loading scene
    public void TransitionToGameSceneWithLoading(string targetSceneName, string loadingSceneName)
    {
        ISceneLoader sceneLoader = SceneLoaderSingleton.Instance.SceneLoader;
        sceneLoader.TransitionToScene(new LoadSceneInfoName(targetSceneName), new LoadSceneInfoName(loadingSceneName));
    }
}

With this setup you should be able to take full advantage of this package. I haven't tested these scripts since I'm not at home at the moment, but let me know if you need any additional help with this.

EDIT: Updated script snippet to version 3.0.0 of the package.