分数重置脚本 [英] Score reset script
问题描述
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 个问题的分数不同)我想要一个适用于特定分数脚本的代码(不要重置所有分数同时进行游戏)
I make a quiz game and I want my score to reset when you touch the button reset score. The quiz have different score scripts for 20 questions ( after you finish the first 20 questions , you start another quiz with different score for the next 20 questions ) I want a code that works with a specific Score script ( not to reset all scores from the game in the same time)
ScorePanel16:
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:
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);
}
}
}
Score16:
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;
}
}
推荐答案
在完成一个测验(20 个)问题后.使用 PlayerPrefs.SetInt("totalScore",score);
在 PlayerPref
中保存并添加分数game ,然后 score=PlayerPrefs.GetInt("totalScore")+currentScore;
.在下一行再次设置分数使用 PlayerPrefs.SetInt("totalScore",score);
After a completing a quiz(with 20) questions. Save and add the score in PlayerPref
using PlayerPrefs.SetInt("totalScore",score);
If you like to reset before completing just clear currentScore.Else if you completed the game ,then score=PlayerPrefs.GetInt("totalScore")+currentScore;
.In next line again set the score
using PlayerPrefs.SetInt("totalScore",score);
这篇关于分数重置脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!