I continue to learn how to create mobile games and a question came up. How to implement a process in which the player clicks on several different buttons (by selecting “conditions”) and based on this selection an event is generated in a new scene (by clicking on the “Continue” button). For example, the Player presses the “Add trees” button, “add weather animation”, “add night time”, and then the “Continue” button and finds himself in a scene in which what the Player has selected is additionally present.
There no correct/wrong way to do this , i do however understand how people can get lost when then wanna switch to a new scene BUT keep data from the old scene.
One way i would do it is using a Coroutine , maybe something like this
public struct MyConditions
{
public List<Tree> trees;
public WeatherAnimation weather;
public bool isNightTime;
}
public class SceneTransitionBehaviour : MonoBehaviour
{
// this method should be triggered by your "continue" button
public void ContinueOnClickBtn()
{
StartCoroutine(CrtGoToScene());
}
private IEnumerator CrtGoToScene()
{
// get your conditions somehow from your UI or whatever
MyConditions conditions = GetConditions();
string scenePath = "YOUR_SCENE_PATH";
// Start scene loading
AsyncOperation asyncOp = SceneManager.LoadSceneAsync(scenePath);
// wait until it's done loading
yield return new WaitUntil(() => asyncOp.isDone);
// now by this point the scene should be loaded
Scene scene = SceneManager.GetSceneByPath(scenePath);
// get all the object of the new scene and find you object that you use to "apply" the conditions
GameObject[] newScenesObjects = scene.GetRootGameObjects();
ConditionsManager conditionsManager = null;
foreach(var go in newSceneObjects)
{
if(go.TryGetComponent<ConditionsManager>(out conditionsManager))
break;
}
if(conditionsManager == null)
{
// didn't find the maanger
return;
}
// apply your conditions using the object from the new scene
conditionsManager.Apply(conditions);
}
}
No Idea if there are better ways, but I would store all this information on an empty gameObject that has DontDestroyOnLoad(this); in it’s scripts start function, which makes it persist. and then in the new scene another script can read out all those settings and spawn those things you want to add.
You could also store this information in a static field of a class