我想第一次显示一个文本,例如“嗨,我叫NAVI”。然后当玩家下次再次开始游戏时,它将显示另一个文字,例如“欢迎回来”。
使用播放器偏好设置可能是最简单的解决方案,尽管绝对不是唯一的解决方案*。
由于缺少布尔值playerpref,所以我通常选择使用SetInt/ GetInt,但这是个人喜好。您也可以使用字符串或浮点数来实现。
private void Start()
{
if(PlayerPrefs.GetInt("HasLaunched", 0) == 0)//Game hasn't launched before. 0 is the default value if the player pref doesn't exist yet.
{
//Code to display your first time text
}
else
{
//Code to show the returning user's text.
}
PlayerPrefs.SetInt("HasLaunched", 1); //Set to 1, so we know the user has been here before
}
Run Code Online (Sandbox Code Playgroud)
Playerpref在会话之间存储值,因此下一个会话仍将“ HasLaunched”设置为1。但是,这存储在本地设备上。因此用户可以根据需要手动重置它。
如果您想再次显示“首次启动”文本,只需将其设置HasLaunched回零,或将其与DeleteKey("HasLaunched");
*有很多替代解决方案,例如将其存储在本地配置文件中,或使用远程数据库(如果您不希望用户能够重置它,则需要这样做)。但是,它们将遵循相同的原则。将某个值设置为true或1,然后在启动时检查该值。
| 归档时间: |
|
| 查看次数: |
33 次 |
| 最近记录: |