如何独立重设测验分数?

时间:2019-09-06 20:04:58

标签: c# unity3d

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

public class Resetscr : MonoBehaviour
{
    

    public List<GameInstance> quizInstances;
    // Start is called before the first frame update
    void awake()
    {
        quizInstances[0].Score = 0;


    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

我做了一个问答游戏,并且有一个得分面板,得分和游戏实例。 (每个测试都有自己的得分面板,得分和游戏实例。)

我需要一个按钮脚本,单击该脚本可以将当前的测试和分数面板重置为0。

要执行此操作,我应该在脚本中写些什么(分数面板和当前测试将得分的数据从一个场景传输到另一个场景,并且我需要在单击该按钮时擦除该数据)。

我应该为每个游戏实例使用按钮脚本还是有使其通用的方法?


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using System;
    public class ScorePanel1 : 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
            GameInstance1.OnScoreChanged -= OnScoreChanged;
            GameInstance1.OnScoreChanged += OnScoreChanged;

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

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

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



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

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

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

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

            GameInstance1.OnScoreChanged -= OnScoreChanged;
            GameInstance1.OnScoreChanged += OnScoreChanged;

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

        }

        private void OnDestroy()
        {
            GameInstance1.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)
        {
            GameInstance1.Score += s;
        }

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



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

    public static class GameInstance1 
    {
        // 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);
            }
        }
    }


1 个答案:

答案 0 :(得分:0)

您需要提供更多详细信息。 这种方法看起来有些问题。

  button.onClick.AddListener(() => { GameInstance1.Score++; });

因为没有检查答案是对还是错。它将继续增加分数。

您可以通过在整个游戏中使用一个Canvas来解决问题,而不破坏Canvas和gameobject使用以下按钮保持回调:

 DontDestroyOnLoad(gameObject);

现在,您需要在该脚本中保留按钮的“ Refreence”(而不是所有Score1实例),以便每个Score1实例可以在需要时添加侦听器和删除侦听器。

现在,在每个Score1实例的开始,您可以订阅Button侦听器并删除旧的侦听器。

 private ButtoncallbackHandler buttoncallback;

 void Start ()
 {
  buttoncallback= GameObject.FindObjectOfType<ButtoncallbackHandler >();
  if(buttoncallback)
 {
    buttoncallback.ScoreButton.onClick.AddListener(() => AnswerValidity());
    buttoncallback.ResetButton.onClick.AddListener(() => OnResetClick());
 }
 }

 void AnswerValidity()
 {
  if(answerisCorrect)
  {

   GameInstance1.Score++;

  }
  else
  {
  ///Wrong Answer Logic.
  }

   // Update your Text Box here
   UpdateTextBox();
 }
 void OnResetClick()
 {

  // Do what you want to do in reset
  UpdateTextBox();

 }

类似地实现ResetButton,您可以重置当前Instacne得分,或者所有得分取决于您,在整个游戏中保留单个文本框。