Je suis en train de développer un logiciel éducatif de style QCM. En UI, j'affiche des boutons pour que les utilisateurs puissent répondre. Le problème, c'est qu'ils peuvent cliquer rapidement une dizaine de fois lors de la question 1 et cela rempli automatiquement les autres questions.
J'ai fait des recherches et j'ai trouvé une solution. J'ai désactivé les boutons après le premier click et demandé de les réactiver en lançant la question suivante. Le problème, c'est que lors de la désactivation, ça a bloqué le programme.
Vous pouvez trouver en dessous le script fonctionnel.
Si en plus je peux avoir des conseils pour améliorer le script, ça serait génial.
Un point important, je suis un très grand novice en Unity, alors ne soyez pas surpris du code de cochon, je me suis servi de ce que j'ai pu trouver à droite et à gauche.
Merci pour votre aide.
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class modA01 : MonoBehaviour
{
// nb de boutons
private Text txtQuestion0;
private Text txtQuestion1;
private Text txtQuestion2;
private Text txtQuestion3;
private Text txtQuestion4;
private Text txtScore;
private TextMesh txtBtn1;
private TextMesh txtBtn2;
private TextMesh txtBtn3;
private TextMesh txtBtn4;
private TextMesh txtBtn5;
private TextMesh txtBtn6;
private TextMesh txtBtn7;
private TextMesh txtBtn8;
private TextMesh txtBtn9;
private TextMesh txtBtn10;
private int nombrerandom; // nombre aléatoire
private int nombrei = 0;
public string reponse;
public int score = 0;
private int totalQuestion = 12; // nombre de question par qcm
private int nbQuestion = 0; // nombre de questions posées
private int tmpi = 0; // initialisation du compteur tmpi
// Déclaration du tableau
string[] Quizz = new string[12]; // nb de questions dans tableau
// Start is called before the first frame update
void Start()
{
// Composants
txtQuestion0 = GameObject.Find("TxtQuestion0").GetComponent<Text>();
txtQuestion1 = GameObject.Find("TxtQuestion1").GetComponent<Text>();
txtQuestion2 = GameObject.Find("TxtQuestion2").GetComponent<Text>();
txtQuestion3 = GameObject.Find("TxtQuestion3").GetComponent<Text>();
txtQuestion4 = GameObject.Find("TxtQuestion4").GetComponent<Text>();
txtScore = GameObject.Find("TxtScore").GetComponent<Text>();
txtBtn1 = GameObject.Find("Txt1").GetComponent<TextMesh>();
txtBtn2 = GameObject.Find("Txt2").GetComponent<TextMesh>();
txtBtn3 = GameObject.Find("Txt3").GetComponent<TextMesh>();
txtBtn4 = GameObject.Find("Txt4").GetComponent<TextMesh>();
txtBtn5 = GameObject.Find("Txt5").GetComponent<TextMesh>();
txtBtn6 = GameObject.Find("Txt6").GetComponent<TextMesh>();
txtBtn7 = GameObject.Find("Txt7").GetComponent<TextMesh>();
txtBtn8 = GameObject.Find("Txt8").GetComponent<TextMesh>();
txtBtn9 = GameObject.Find("Txt9").GetComponent<TextMesh>();
txtBtn10 = GameObject.Find("Txt10").GetComponent<TextMesh>();
// Déclaration du contenu du tableau
Quizz[0] = "1;+;1;=;?;0;1;2;3;4;5;6;7;8;9;2";
Quizz[1] = "1;+;2;=;?;0;1;2;3;4;5;6;7;8;9;3";
Quizz[2] = "1;+;3;=;?;0;1;2;3;4;5;6;7;8;9;4";
Quizz[3] = "1;+;4;=;?;0;1;2;3;4;5;6;7;8;9;5";
Quizz[4] = "5;+;1;=;?;0;1;2;3;4;5;6;7;8;9;6";
Quizz[5] = "3;+;1;=;?;0;1;2;3;4;5;6;7;8;9;4";
Quizz[6] = "4;+;1;=;?;0;1;2;3;4;5;6;7;8;9;5";
Quizz[7] = "2;+;1;=;?;0;1;2;3;4;5;6;7;8;9;3";
Quizz[8] = "3;+;?;=;4;0;1;2;3;4;5;6;7;8;9;1";
Quizz[9] = "5;+;1;=;?;0;1;2;3;4;5;6;7;8;9;6";
Quizz[10] = "2;+;3;=;?;0;1;2;3;4;5;6;7;8;9;5";
Quizz[11] = "2;+;2;=;?;0;1;2;3;4;5;6;7;8;9;4";
PoseUneQuestion();
}
// Update is called once per frame
void Update()
{
}
public void PoseUneQuestion()
{
txtScore.text = "Score : " + score;
if (nbQuestion < totalQuestion)
{
nbQuestion += 1;
// Questions en mode aléatoire
// nombrerandom = Random.Range(0, Quizz.Length);
// string[] Col = Quizz[nombrerandom].Split(';');
// tableau boutons
string[] Col = Quizz[nombrei].Split(';');
txtQuestion0.text = Col[0];
txtQuestion1.text = Col[1];
txtQuestion2.text = Col[2];
txtQuestion3.text = Col[3];
txtQuestion4.text = Col[4];
txtBtn1.text = Col[5];
txtBtn2.text = Col[6];
txtBtn3.text = Col[7];
txtBtn4.text = Col[8];
txtBtn5.text = Col[9];
txtBtn6.text = Col[10];
txtBtn7.text = Col[11];
txtBtn8.text = Col[12];
txtBtn9.text = Col[13];
txtBtn10.text = Col[14];
reponse = Col[15];
PlayerPrefs.SetInt("tmpi", nombrei);
nombrei += 1;
}
else
{
// sauvegarde du score
PlayerPrefs.SetInt("scoreA01", score);
PlayerPrefs.Save();
// Debug.Log("Partie terminée, score de " + Score + "/" + totalQuestion);
SceneManager.LoadScene("modules");
}
}
void OnMouseDown()
{
// module 1
if (GameObject.Find("Canvas").GetComponent<modA01>().reponse == transform.GetChild(0).GetComponent<TextMesh>().text)
{
// Debug.Log("Gagné !");
int tmpi = PlayerPrefs.GetInt("tmpi");
// Debug.Log("gagner : tmpi = " + tmpi);
// module 2
GameObject.Find("Canvas").GetComponent<modA01>().score += 1;
// tmpi1 - exception pour format du qcm (ex 1+?=3 au lieu de 1+2=?)
if (tmpi == 8)
{
txtQuestion2.color = Color.green;
// module 3
txtQuestion2.text = GameObject.Find("Canvas").GetComponent<modA01>().reponse;
}
else
{
txtQuestion4.color = Color.green;
// module 4
txtQuestion4.text = GameObject.Find("Canvas").GetComponent<modA01>().reponse;
}
}
else
{
// Debug.Log("Perdu !");
int tmpi = PlayerPrefs.GetInt("tmpi");
// Debug.Log("perdre : tmpi = " + tmpi);
// tmpi2
if (tmpi == 8)
{
txtQuestion2.color = Color.red;
txtQuestion2.text = transform.GetChild(0).GetComponent<TextMesh>().text;
}
else
{
txtQuestion4.color = Color.red;
txtQuestion4.text = transform.GetChild(0).GetComponent<TextMesh>().text;
}
}
StartCoroutine(MyCoroutine());
IEnumerator MyCoroutine()
{
yield return new WaitForSeconds(2);
txtQuestion0.color = Color.black;
txtQuestion2.color = Color.black;
txtQuestion4.color = Color.black;
// module 5
GameObject.Find("Canvas").GetComponent<modA01>().PoseUneQuestion();
}
}
}