using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System;
public class SelectPrefab : MonoBehaviour
{
[Header("题目主题")]
public Text nameText;
[Header("题目文本")]
public Text text;
[Header("答案文本数组")]
public string[] answerTexts;
[Header("答案数组")]
public bool[] answers;
[Header("是否显示认知提问对话框")]
public bool textActive;
[Header(" ")]
public Transform answerParent;
public Toggle answerPrefab;
///
/// 提交按钮
///
public Button selectButton;
///
/// 关闭按钮
///
public Button closeButton;
///
/// 高级认知正确/错误
///
public GameObject trueText1;
public GameObject falseText1;
///
/// 低级认知正确/错误
///
public GameObject trueText2;
public GameObject falseText2;
///
/// 答案UI预制体列表
///
private List answerPrefabList;
///
/// 提交回调
///
private Action selectCallBack;
private ToggleGroup toggleGroup;
private bool isRight;
///
/// 题目音频文件
///
private AudioSource audioSource;
private void Awake()
{
selectButton.onClick.AddListener(SelectButtonOnClick);
closeButton.onClick.AddListener(CloseButtonOnClick);
audioSource = GetComponent();
toggleGroup = GetComponent();
}
private void Start()
{
int number = 0;
for (int i = 0; i < answers.Length; i++)
{
if (answers[i])
{
number += 1;
}
}
answerPrefabList = new List();
for (int i = 0; i < answerTexts.Length; i++)
{
answerPrefabList.Add(Instantiate(answerPrefab, answerParent));
answerPrefabList[i].GetComponentInChildren().text = answerTexts[i];
answerPrefabList[i].onValueChanged.AddListener(ToggleOnSelect);
if (number == 1)
{
answerPrefabList[i].group = toggleGroup;
}
}
}
private void ToggleOnSelect(bool value)
{
isRight = false;
for (int i = 0; i < answerPrefabList.Count; i++)
{
if (answerPrefabList[i].isOn != answers[i])
{
return;
}
}
isRight = true;
}
///
/// 选完确认按钮
///
private void SelectButtonOnClick()
{
text.gameObject.SetActive(false);
audioSource.enabled = false;
if (textActive)
{
closeButton.gameObject.SetActive(true);
if (answers[0])
{
if (isRight)
{
trueText1.gameObject.SetActive(true);
}
else
{
falseText1.gameObject.SetActive(true);
}
}
else
{
if (isRight)
{
trueText2.gameObject.SetActive(true);
}
else
{
falseText2.gameObject.SetActive(true);
}
}
}
else
{
selectCallBack?.Invoke(isRight);
}
}
///
/// 关闭按钮
///
private void CloseButtonOnClick()
{
selectCallBack?.Invoke(isRight);
}
///
/// 初始化
///
public void SelectPrefabCreateInit(Action callBack)
{
selectCallBack = callBack;
text.gameObject.SetActive(true);
audioSource.enabled = true;
trueText1.gameObject.SetActive(false);
falseText1.gameObject.SetActive(false);
trueText2.gameObject.SetActive(false);
falseText2.gameObject.SetActive(false);
}
}