我试图能够重新生成,并在我走进我的" Gold"宾语。 至于现在,在尝试实施" Score-Stuff"之前,我甚至都无法重生。 (首先是" FoundGold"脚本只用于能够重生)。此外,我试图将最低分数作为高分。 请注意,我是C#的新手,我有点把所有东西都放在我需要的教程中,所以回答一些实际的代码/陈述出错的地方将非常感激。
//GoldFound Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GoldFound : MonoBehaviour
{
private ScoreManager theScoreManager;
public Transform target;
[SerializeField] private Transform player;
[SerializeField] private Transform respawnpoint;
private void Start()
{
theScoreManager = FindObjectOfType<ScoreManager>();
}
private void OnTriggerEnter(Collider other)
{
theScoreManager.scoreIncreasing = false;
player.transform.position = respawnpoint.transform.position;
theScoreManager.scoreCount = 0;
theScoreManager.scoreIncreasing = true;
}
}
其他代码
//ScoreManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
public Text scoreText;
public Text hiScoreText;
public float scoreCount;
public float hiScoreCount;
public float pointPerSecond;
public bool scoreIncreasing;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (scoreIncreasing)
{
scoreCount += pointPerSecond * Time.deltaTime;
}
if(scoreCount > hiScoreCount)
{
hiScoreCount = scoreCount;
}
scoreText.text = "Score: " + Mathf.Round (scoreCount);
hiScoreText.text = "High Score: " + Mathf.Round (hiScoreCount);
}
}
答案 0 :(得分:3)
如果您想在中间播放会话中保存您的高分,那么最简单的方法是将值保存到PlayerPrefs。如果你想开始保存更多/更复杂的东西,你真的应该把它保存在你自己生成的文件中。但在你的情况下,PlayerPrefs没问题。
这是关于这个主题的Unity教程: https://unity3d.com/learn/tutorials/topics/scripting/high-score-playerprefs
否则,您可以这样做:
public void SetHighscore (float currentScore)
{
if (PlayerPrefs.HasKey("highscore"))
{
float highscore = PlayerPrefs.GetFloat("highscore");
if (highscore > currentScore)
{
PlayerPrefs.SetFloat("highscore", currentScore);
PlayerPrefs.Save();
}
}
else
{
PlayerPrefs.SetFloat("highscore", currentScore);
PlayerPrefs.Save();
}
}
然后只需在需要时编写PlayerPrefs.GetKey(&#34; highscore&#34;)。 (虽然我也建议您使用PlayerPrefs.HasKey检查它是否存在(&#34;高分&#34;)) https://docs.unity3d.com/ScriptReference/PlayerPrefs.html