Glow effect on selected items
This commit is contained in:
@@ -14,6 +14,6 @@ public class ObjectController : MonoBehaviour {
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
//transform.GetComponent<Light>().intensity = 1.0f + Mathf.Abs(Mathf.Sin(2*Time.time));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,11 @@ public class QuizController : MonoBehaviour {
|
||||
|
||||
public Button btnNextQuestion;
|
||||
|
||||
private float counterValue;
|
||||
public GameObject haloPrefab;
|
||||
|
||||
private int scoreValue = 0;
|
||||
|
||||
private int questionIdx = 0;
|
||||
|
||||
private const int numberOfAnswers = 4;
|
||||
|
||||
@@ -31,16 +35,19 @@ public class QuizController : MonoBehaviour {
|
||||
|
||||
private const int counterDisplayAnswersInitialValue = 3;
|
||||
|
||||
private float counterValue;
|
||||
|
||||
private float counterDisplayAnswersValue;
|
||||
|
||||
private bool counterDisplayAnswersStarted = false;
|
||||
|
||||
private int scoreValue = 0;
|
||||
|
||||
private int questionIdx = 0;
|
||||
|
||||
// List of selected answers
|
||||
private List<int> selectedAnswers;
|
||||
|
||||
// References to answers halo prefabs
|
||||
private List<GameObject> answersHaloPrefabs;
|
||||
|
||||
// List of questions and correct answers
|
||||
List<Question> questions = new List<Question>() {
|
||||
new Question("1. Neki od ovih prozora imaju 2-3 puta manje toplotnih gubitaka za razliku od drugih, koji su to?", new int[] {1, 4}),
|
||||
new Question("2. Na jedan od načina možete uštedjeti i do 10 litara vode po osobi dnevno, uz koji predmet?", new int[] {3, 4}),
|
||||
@@ -54,20 +61,24 @@ public class QuizController : MonoBehaviour {
|
||||
new Question("10. Uvijek odaberite program s najnižom temperaturom vode koji još uvijek obezbjeđuje dobar kvalitet pranja, za šta od navedenog? ", new int[] {1, 4}),
|
||||
};
|
||||
|
||||
public GameObject objController;
|
||||
|
||||
// Use this for initialization
|
||||
/// <summary>
|
||||
/// Use this for initialization
|
||||
/// </summary>
|
||||
void Start () {
|
||||
selectedAnswers = new List<int>();
|
||||
answersHaloPrefabs = new List<GameObject>();
|
||||
counterValue = counterInitialValue;
|
||||
UpdateScene();
|
||||
UpdateCounter();
|
||||
UpdateScore();
|
||||
btnNextQuestion.GetComponent<Button>().onClick.AddListener(LoadNextQuestion);
|
||||
InstantiateHaloPrefabs();
|
||||
}
|
||||
|
||||
|
||||
// Update is called once per frame
|
||||
/// <summary>
|
||||
/// // Update is called once per frame
|
||||
/// </summary>
|
||||
void Update()
|
||||
{
|
||||
if (counterValue > 0)
|
||||
@@ -79,9 +90,7 @@ public class QuizController : MonoBehaviour {
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LoadNextQuestion();
|
||||
}
|
||||
if (counterDisplayAnswersValue > 0 && counterDisplayAnswersStarted)
|
||||
counterDisplayAnswersValue -= Time.deltaTime;
|
||||
else
|
||||
@@ -90,7 +99,7 @@ public class QuizController : MonoBehaviour {
|
||||
counterDisplayAnswersStarted = false;
|
||||
}
|
||||
|
||||
|
||||
// Select answers event handler
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
@@ -101,20 +110,37 @@ public class QuizController : MonoBehaviour {
|
||||
if (counterDisplayAnswersStarted)
|
||||
return;
|
||||
Debug.Log(hit.transform.gameObject.name);
|
||||
int answerIdx = GetAnswerIdx(hit.transform.gameObject.name) - numberOfAnswers * questionIdx;
|
||||
// answerArrayIdx: [1, 40]
|
||||
int answerArrayIdx = GetAnswerIdx(hit.transform.gameObject.name);
|
||||
// answerIdx: [1, 4]
|
||||
int answerIdx = answerArrayIdx - numberOfAnswers * questionIdx;
|
||||
// Answer not selected - select it
|
||||
if (!selectedAnswers.Contains(answerIdx))
|
||||
{
|
||||
SetGlowEffect(answerArrayIdx, true);
|
||||
selectedAnswers.Add(answerIdx);
|
||||
}
|
||||
// Answer selected - unselect it
|
||||
else
|
||||
{
|
||||
SetGlowEffect(answerArrayIdx, false);
|
||||
selectedAnswers.Remove(answerIdx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Next question btn click event handler
|
||||
/// </summary>
|
||||
void LoadNextQuestion()
|
||||
{
|
||||
if (counterDisplayAnswersStarted)
|
||||
return;
|
||||
scoreValue += GetScore();
|
||||
UpdateScore();
|
||||
selectedAnswers.Clear();
|
||||
ClearCurrentSceneGlowEffect();
|
||||
counterDisplayAnswersValue = counterDisplayAnswersInitialValue;
|
||||
counterDisplayAnswersStarted = true;
|
||||
counterValue = counterInitialValue;
|
||||
@@ -122,16 +148,25 @@ public class QuizController : MonoBehaviour {
|
||||
questionIdx = (questionIdx + 1) % questions.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates score value - called from Update() method
|
||||
/// </summary>
|
||||
private void UpdateScore()
|
||||
{
|
||||
scoreText.text = scoreValue.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates counter value - called from Update() method
|
||||
/// </summary>
|
||||
private void UpdateCounter()
|
||||
{
|
||||
counterText.text = counterValue.ToString("f0");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows correct answers
|
||||
/// </summary>
|
||||
private void ShowAnswers()
|
||||
{
|
||||
for(int i = numberOfAnswers * questionIdx; i < numberOfAnswers * (questionIdx + 1); i++)
|
||||
@@ -140,19 +175,15 @@ public class QuizController : MonoBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
private void MarkAnswer(bool isCorrect, int idx)
|
||||
{
|
||||
answers[idx].GetComponent<ObjectController>().imageCorrect.enabled = isCorrect;
|
||||
answers[idx].GetComponent<ObjectController>().imageIncorrect.enabled = !isCorrect;
|
||||
}
|
||||
|
||||
// Update scene with new question + answers
|
||||
/// <summary>
|
||||
/// Updates scene with new question and related answers
|
||||
/// </summary>
|
||||
private void UpdateScene()
|
||||
{
|
||||
RemoveMarks();
|
||||
// Set new question
|
||||
questionText.text = questions[questionIdx].getQuestionText();
|
||||
// Set right answers
|
||||
// Set answers for selected question
|
||||
for(int i = 0; i < answers.Length; i++)
|
||||
{
|
||||
if (i >= numberOfAnswers * questionIdx && i < numberOfAnswers * (questionIdx + 1))
|
||||
@@ -164,19 +195,68 @@ public class QuizController : MonoBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hides correct/incorrect images for currently shown answers
|
||||
/// </summary>
|
||||
private void RemoveMarks()
|
||||
{
|
||||
foreach(GameObject answer in answers)
|
||||
{
|
||||
answer.GetComponent<ObjectController>().imageCorrect.enabled = false;
|
||||
answer.GetComponent<ObjectController>().imageIncorrect.enabled = false;
|
||||
for(int i = 0; i < numberOfAnswers; i++) {
|
||||
int answerIdx = questionIdx * numberOfAnswers + i;
|
||||
answers[answerIdx].GetComponent<ObjectController>().imageCorrect.enabled = false;
|
||||
answers[answerIdx].GetComponent<ObjectController>().imageIncorrect.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets visibility of correct/incorrect overlay images
|
||||
/// </summary>
|
||||
/// <param name="isCorrect"></param>
|
||||
/// <param name="idx"></param>
|
||||
private void MarkAnswer(bool isCorrect, int idx)
|
||||
{
|
||||
answers[idx].GetComponent<ObjectController>().imageCorrect.enabled = isCorrect;
|
||||
answers[idx].GetComponent<ObjectController>().imageIncorrect.enabled = !isCorrect;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds Halo prefabs to all answers
|
||||
/// </summary>
|
||||
private void InstantiateHaloPrefabs()
|
||||
{
|
||||
for(int i = 0; i < answers.Count(); i++)
|
||||
{
|
||||
GameObject answerPrefab = Instantiate(haloPrefab) as GameObject;
|
||||
answerPrefab.SetActive(false);
|
||||
answerPrefab.transform.SetParent(answers[i].transform, false);
|
||||
answersHaloPrefabs.Add(answerPrefab);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears glow from all currently shown answers
|
||||
/// </summary>
|
||||
private void ClearCurrentSceneGlowEffect()
|
||||
{
|
||||
for (int i = 0; i < numberOfAnswers; i++)
|
||||
{
|
||||
int prefabIdx = questionIdx * numberOfAnswers + i;
|
||||
answersHaloPrefabs[prefabIdx].SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears/Adds glow from a specific answer GameObject
|
||||
/// </summary>
|
||||
/// <param name="prefabIdx"></param>
|
||||
private void SetGlowEffect(int prefabIdx, bool showGlowEffect)
|
||||
{
|
||||
answersHaloPrefabs[prefabIdx - 1].SetActive(showGlowEffect);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns answer index (X) from answer's name (AnswerX)
|
||||
/// </summary>
|
||||
/// <param name="gameObjName"></param>
|
||||
/// <param name="gameObjName">GameObject's name</param>
|
||||
/// <returns></returns>
|
||||
private int GetAnswerIdx(string gameObjName)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user