I'm using [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
to initialize a prefab that contains all the components required for the game to work propertly.
public class FrameworkInitializer : MonoBehaviour
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void InitializeFramework()
{
Instantiate(Resources.Load("_GameEngine")) as GameObject;
}
}
It works fine.
Now I'm creating some scenes to try out things before implementing them in the final game, and I don't really need the default prefab to initialize. I tried adapting the script to this:
public class FrameworkInitializer : MonoBehaviour
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void InitializeFramework()
{
if (GameObject.FindWithTag("Framework Support") != null) return;
if (GameObject.Find("NoFrameworkSupportRequired") != null)
{
Debug.LogWarning("Carla Framework initializer skipped because of NoFrameworkSupportRequired gameobject in scene.");
return;
}
var framework = Instantiate(Resources.Load("_GameEngine")) as GameObject;
}
}
But it's not working, because the scene is not loaded yet so it can't find any gameobject.
And if I change BeforeSceneLoad
to AfterSceneLoad
, some of the scripts that require the prefab in the Awake
and onEnable
cycle fails.
How can I make this script to run only on some of the scenes?
FrameworkInitializer
should run? Can I read PlayerPrefs there? \$\endgroup\$_GameEngine
lazily, so it's there as soon as someone asks for it? \$\endgroup\$_GameEngine
prefab is just a wrapper withDontDestroyOnLoad
. It contains other child gameobjects that really do the work and acts like singleton. For example, if someone needs to call the DialogueSystem, it'll just do:DialogueSystemManager.Instance
._GameEngine
doesn't really know when a script needs something inside it. I don't know how to make_GameEngine
aware of this, to make it load lazily \$\endgroup\$