Unity 3d: Move information from one scene to another

0

Dear, I did a search to find problem solution that I could not find in another source found in google. The idea is when a user fills the information in a scene identified as scene1 that contain UI object, such as access authentication screen case and between all things. When you click the button, move to another scene as scene2 containing the displayed information that was filled in a scene1. For example: I typed in "John" text field and when I click the confirm button it will move to scene2. And in scene 2 it displays John on a label or label. have some generic functions that do this?

    
asked by anonymous 26.12.2017 / 11:14

3 answers

2

You can use a static class for the persistence of data throughout the scenes. You can declare a UserData class, for example, and use static properties to store this data, for example:

class UserData{
    // Nome de usuário do jogador.
    public static string userName = "";

    // Password do jogador.
    public static string password = "";
}

It is not necessary to inherit MonoBehaviour in this case, nor to append the script to an object (in fact not inheriting MonoBehaviour ) nor can it be appended ).

So, at Scene1 you probably already have a script with a method that will be executed by clicking the button that switches to Scene2. Before loading Scene2 you can store your information, for example:

public void TrocaDeCena(){
    // Armazena os dados do usuário.
    UserData.userName = campoDeTextoUsername.text; // Aqui você já deve ter a referência do campo de texto onde o usuário digitou.
    UserData.password = campoDeTextoPassword.text; // Aqui você já deve ter a referência do campo de texto onde o usuário digitou.

    // Carrega a Cena2.
    SceneManager.LoadScene("Cena2");
}

To retrieve the data stored in the static class in the next scene in the Start method of some script attached to the UI element that will display the information you will have code similar to the follow:

public void Start(){
    // Aqui você já deve ter a referência do elemento text que exibirá o dado.
    // Exibe o nome do usuário armazenado.
    labelUserName.text = UserData.userName;
}

This is one of the simplest ways to traffic / persist data between scenes. How to get text field values and how to assign values in labels can change according to your version of Unity but the static class approach works from old versions.

    
12.07.2018 / 06:09
1

You can do this using the function:

PlayerPrefs.SetString("Nome", variavelNome);

To save in scene 1, and then in scene 2 use:

PlayerPrefs.GetString("Nome");

To manipulate information!

    
26.12.2017 / 12:05
1

Arthur,

To work around this problem you can use an empty object with a GameController script to save the information from the first scene. With the script you just need to add the DontDestroyOnLoad method so that when you load it into scene2, you still has the active% object in the scene. By adding the singleton design patterns, you can refer to the class methods of any script, without having to reference the scene.

Ex: GameController

public class GameController : MonoBehaviour
{
    private string nome = "";

    private static GameController instance = null;
    public static GameController Instance { get { return instance; } }

    void Start()
    {
        instance = this;
        DontDestroyOnLoad(this.gameObject);
    }

    public void GravarNome(string nome)
    {         
        this.nome = nome;
    }

    public string ConsultarNome(){
        return this.nome;
    }
}
    
27.12.2017 / 20:08