Skip to content

Instantly share code, notes, and snippets.

@unity3dcollege
Created September 14, 2017 16:13
Show Gist options
  • Save unity3dcollege/79ec52a38da7735bb6453c20df0cfae9 to your computer and use it in GitHub Desktop.
Save unity3dcollege/79ec52a38da7735bb6453c20df0cfae9 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using UnityEngine.UI;
public class G6 : MonoBehaviour
{
public string filePath;
public string jsonString;
public JsonData questionData;
public int NumberQuestion = 0;
public GameObject answerPrefab;
public bool nextQuestion;
public bool clickAnswer;
public GameObject wrong;
public GameObject correct;
public int score;
public GameObject VeryGood;
public GameObject Good;
public GameObject Bad;
public GameObject Poor;
public GameObject pausemenu;
public GameObject Gamebutton;
public GameObject GameLabel;
public AudioSource CorrectSound;
public AudioSource WrongSound;
public Text countText6;
public Text coinsText1;
public GameObject Hint;
private ScoreM anager theScoreMana;
private CoinManager theCoinManage;
void Start()
{
coinsText1.text = "Coins: " + FindObjectOfType<scoremanager>().currentScore;
}
public void QuestionBegin(string jsonName)
{
pausemenu.SetActive(false);
score = 0;
nextQuestion = true;
filePath = System.IO.Path.Combine(Application.streamingAssetsPath + "/Grade6", jsonName + ".json");
//jsonString = System.IO.File.ReadAllText(filePath);
StartCoroutine("Json");
questionData = JsonMapper.ToObject(jsonString);
onClick();
}
IEnumerator Json()
{
if (filePath.Contains("://"))
{
WWW www = new WWW(filePath);
yield return www;
jsonString = www.text;
}
else
{
jsonString = System.IO.File.ReadAllText(filePath);
}
}
public void onClick()
{
if (NumberQuestion >= questionData["data"].Count)
{
Debug.Log("The result");
if (score == questionData["data"].Count)
{
GameObject.Find("Rank").GetComponent<text>().text = "Ikaw ay isang mabuting mag-aaral!";
VeryGood.SetActive(true);
Good.SetActive(false);
Bad.SetActive(false);
Poor.SetActive(false);
Gamebutton.SetActive(true);
GameLabel.SetActive(true);
}
if (score == questionData["data "].Count * 0)
{
GameObject.Find("Rank").GetComponent<text>().text = "Mag-aral pa ng mabuti!";
VeryGood.SetActive(false);
Good.SetActive(false);
Bad.SetActive(false);
Poor.SetActive(true);
Gamebutton.SetActive(false);
GameLabel.SetActive(false);
}
else if (score >= questionData["data"].Count * 1 / 2)
{
GameObject.Find("Rank").GetComponent<text>().text = "Ipagpatuloy ang pag-aaral ng mabuti!";
VeryGood.SetActive(false);
Good.SetActive(true);
Bad.SetActive(false);
Poor.SetActive(false);
Gamebutton.SetActive(true);
GameLabel.SetActive(true);
}
else if (score <= questionData["data"].Count * 1 / 2)
{
GameObject.Find("Rank").GetComponent<text>().text = "Mag-aral pa ng mabuti!";
VeryGood.SetActive(false);
Good.SetActive(false);
Bad.SetActive(true);
Poor.SetActive(false);
Gamebutton.SetActive(false);
GameLabel.SetActive(false);
}
MenuManager menuResult = GameObject.Find("Canvas").GetComponent<menumanager>();
menuResult.ShowMenu(GameObject.Find("Result").GetComponent<menu>());
GameObject.Find("Score").GetComponent<text>().text = score.ToString() + "/" + questionData["data"].Count;
}
wrong.SetActive(false);
correct.SetActive(false);
if (nextQuestion)
{
Hint.SetActive(true);
GameObject[] answerDestroy = GameObject.FindGameObjectsWithTag("Answer5");
if (answerDestroy != null)
{
for (int x = 0; x < answerDestroy.Length; x++)
{
DestroyImmediate(answerDestroy[x]);
}
}
GameObject.Find("G1(C6)/Panel/QuestionC6/Question6").GetComponentInChildren<text>().text = questionData["data"][NumberQuestion]["question"].ToString();
countText6.text = GameObject.Find("G1(C6)/Panel/Image").GetComponentInChildren<text>().text = questionData["data"][NumberQuestion]["id"].ToString();
for (int i = 0; i < questionData["data"][NumberQuestion]["answer"].Count; i++)
{
GameObject answer = Instantiate(answerPrefab);
answer.GetComponentInChildren<text>().text = questionData["da ta"][NumberQuestion]["answer"][i].ToString();
Transform answerC = GameObject.Find("AnswerC6").GetComponent<transform>();
answer.transform.SetParent(answerC);
string x = i.ToString();
if (i == 0)
{
answer.name = "correctAnswer";
answer.GetComponent<button>().onClick.AddListener(() => Answer("0"));
}
else
{
answer.name = " wrongAnswer" + x;
answer.GetComponent<button>().onClick.AddListener(() => Answer(x));
}
answer.transform.SetSiblingIndex(Random.Range(0, 3));
}
NumberQuestion++;
nextQuestion = false;
clickAnswer = true;
StartCoroutine("Timer6");
}
}
public void Answer(string x)
{
if (clickAnswer)
{
if (x == "0")
{
score++;
GameObject.Find("correctAnswer").GetComponent<button>().image.color = Color.green;
correct.SetActive(true);
CorrectSound.Play();
Debug.Log("Correct Answer");
}
else
{
GameObject.Find("wrongAnswer" + x).GetComponent<button>().image.color = Color.red;
wrong.SetActive(true);
WrongSound.Play();
Debug.Log("Wrong Answer");
}
nextQuestion = true;
clickAnswer = false;
}
}
IEnumerator Timer6()
{
Image time = GameObject.Find("Timer6").GetComponent<image>();
time.fillAmount = 1;
float timeToWait = 10f;
float incrementToRemove = 0.05f;
float x = time.fillAmount / timeToWait * incrementToRemove;
while (timeToWait > 0)
{
yield return new WaitForSeconds(incrementToRemove);
if (!nextQuestion)
{
time.fillAmount -= x;
timeToWait -= incrementToRemove;
}
else
{
timeToWait = 0;
}
}
if (time.fillAmount <= 0.1f)
{
for (int i = 1; i < 4; i++)
{
GameObject.Find("wrongAnswer" + i).GetComponent<button>().image.color = Color.red;
wrong.SetActive(false);
}
GameObject.Find("correctAnswer").GetComponent<button>().image.color = Color.green;
clickAnswer = false;
nextQuestion = tr ue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment