I know I can inherit from MonoBehaviour
and add some handler functions to SceneManager.sceneLoaded
, as described in official document https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html.
My question is, do I need to create an empty GameObject in the scene, and attach a script to that GameObject to do the stuff? Or there is some other better practice?
CodePudding user response:
This is good practice. Create a SceneManager
empty GameObject
. Attach a script inheriting from MonoBehaviour
and handle all scene related actions in there.
Very lucid, straight to the point.
CodePudding user response:
It is one way to go, yes.
You do not necessarily require a GameObject
for this.
You can also e.g. use [RuntimeInitializeOnLoadMethod]
and attach the listener within there and then further process it.
public static class SomeClass
{
[RuntimeInitializeOnLoadMethod]
private static void Init()
{
SceneManager.sceneLoaded = OnSceneLoaded;
}
private static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log("OnSceneLoaded: " scene.name);
Debug.Log(mode);
}
}
What is "better" depends a lot on your use case and requirements of course