分数重置脚本

时间:2019-08-27 15:29:13

标签: c# unity3d

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ResetButton : MonoBehaviour
{
    public static ResetButton Instance;
    public bool isGameOver = false;
    public Score score;
 
    public Text scoreText;
    public Button yourButton;
    internal static object instance;

    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else if (Instance != this)
        {
            Destroy(gameObject);
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        Button btn = yourButton.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);

    }

    public void TaskOnClick()
    {
        ScorePanelUpdater.Score = 0;
    }

    // Update is called once per frame
    void Update()
    {
        if (isGameOver && Input.GetMouseButtonDown(0))
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

        }
       
    }

}

我做一个问答游戏,当您触摸按钮重置分数时,我希望分数重置。测验中有20个问题的分数脚本(完成前20个问题后,接下来的20个问题中,您以不同的分数开始另一个测验)我想要一个适用于特定分数脚本的代码(不要从一场比赛)

ScorePanel16:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class ScorePanel16 : MonoBehaviour
{
    // better if you can already reference this via Inspector
    [SerializeField] Text text;

    void Awake()
    {
        if (!text) text = GetComponent<Text>();

        // it's always save to remove listeners before adding them
        // makes sure it is added only once
        GameInstance16.OnScoreChanged -= OnScoreChanged;
        GameInstance16.OnScoreChanged += OnScoreChanged;

        // invoke once now with current value
        OnScoreChanged(GameInstance16.Score);
    }

    private void OnDestroy()
    {
        GameInstance16.OnScoreChanged -= OnScoreChanged;
    }

    private void OnScoreChanged(int newValue)
    {
        text.text = "Score: " + newValue;
    }
}

GameInstance15:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public static class GameInstance15
{
    // Start is called before the first frame update
    private static int _score = 0;

    // have an event to register listeners
    public static event Action<int> OnScoreChanged;

    // public property
    public static int Score
    {
        get { return _score; }
        set
        {
            _score = value;

            // invoke an event to inform all registered listeners
            OnScoreChanged?.Invoke(_score);
        }
    }
}

得分16:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Score16 : MonoBehaviour
{
    public Text scoreText;
    public Button button;

    // Use this for initialization
    void Start()
    {
        DontDestroyOnLoad(gameObject);

        if (!button) button = GetComponent<Button>();
        button.onClick.AddListener(() => { GameInstance16.Score++; });

        GameInstance16.OnScoreChanged -= OnScoreChanged;
        GameInstance16.OnScoreChanged += OnScoreChanged;

        // invoke once now with current value
        Debug.LogFormat("Current Score: {0}", GameInstance16.Score);
        OnScoreChanged(GameInstance16.Score);

    }

    private void OnDestroy()
    {
        GameInstance16.OnScoreChanged -= OnScoreChanged;
    }

    private void OnScoreChanged(int newValue)
    {
        scoreText.text = "Score: " + newValue;
    }


    // These two make not much sense .. since the value already was public
    // you wouldn't need methods for this
    // anyway now you could directly do it on GameInformation instead
    public void AddScore(int s)
    {
        GameInstance16.Score += s;
    }

    public int GetScore()
    {
        return GameInstance16.Score;
    }
}

1 个答案:

答案 0 :(得分:0)

完成测验(含20个问题)后。使用PlayerPrefPlayerPrefs.SetInt("totalScore",score);中保存并添加分数,如果您想在完成之前重置,只需清除currentScore。否则,如果您完成游戏,则选择score=PlayerPrefs.GetInt("totalScore")+currentScore;。在下一行中再次设置分数 使用PlayerPrefs.SetInt("totalScore",score);