简体   繁体   English

如何通过不同的场景传递字符串变量? 统一C#

[英]How do I Pass a string variable Through Different Scenes? Unity C#

I Have 2 scripts.我有 2 个脚本。 In one, you have a function where you can sign in. It needs a username and I want that username on a TMP Text that is located on the main menu scene.一方面,您有一个 function ,您可以在其中登录。它需要一个用户名,我希望该用户名出现在位于主菜单场景的 TMP 文本上。

    public string Username;

It IS a public string.它是一个公共字符串。 Other script:其他脚本:

    public TextMeshProUGUI playerName;

And that is the TMP Text on which the player's username should be displayed.这就是应该显示玩家用户名的 TMP 文本。

I know how I could pass variables between scripts, but I don't know how to pass them through whole scenes.我知道如何在脚本之间传递变量,但我不知道如何在整个场景中传递它们。

Kinda rushed sorry.有点着急抱歉。

You can try using PlayerPrefs .您可以尝试使用PlayerPrefs

Set players name on PlayerPrefs after login:登录后在 PlayerPrefs 上设置玩家名称:

PlayerPrefs.SetString("Name", m_PlayerName);

Later load it on another scene from PlayerPrefs:稍后从 PlayerPrefs 将其加载到另一个场景中:

m_PlayerName = PlayerPrefs.GetString("Name", "No Name");

And if you would like to keep it persist between scenes PlayerPrefs would work as well.如果你想让它在场景之间持续存在,PlayerPrefs 也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM