218 lines
5.9 KiB
C#
218 lines
5.9 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.Networking;
|
||
using UnityEngine.UI;
|
||
|
||
public class QuestionItem : MonoBehaviour
|
||
{
|
||
public GameObject topHint;
|
||
public TextMeshProUGUI hintTxt;
|
||
|
||
public TextMeshProUGUI questionText;
|
||
|
||
public Button submitBtn;
|
||
|
||
public Toggle togPrefab;
|
||
public ToggleGroup togPrefabGroup;
|
||
|
||
public InputField input;
|
||
|
||
public DataItem2 currentData;
|
||
|
||
int currentSelectIndex = -1;
|
||
|
||
List<Toggle> allChoose;
|
||
|
||
public GameObject imgShowObj;
|
||
public Transform imgShowPanel;
|
||
public Image imgShowInfo;
|
||
public void Init(DataItem2 data, bool isEnd, bool isExit = false)
|
||
{
|
||
currentData = data;
|
||
if (currentData != null)
|
||
{
|
||
if (!string.IsNullOrEmpty(currentData.introduce))
|
||
{
|
||
topHint.SetActive(true);
|
||
hintTxt.text = currentData.introduce;
|
||
}
|
||
if (isEnd)
|
||
{
|
||
submitBtn.gameObject.SetActive(true);
|
||
submitBtn.onClick.AddListener(OnClickSubmit);
|
||
}
|
||
else
|
||
{
|
||
submitBtn.gameObject.SetActive(false);
|
||
}
|
||
questionText.text = currentData.title;
|
||
}
|
||
var replyList = currentData.problemAnswerRespList;
|
||
allChoose = new List<Toggle>();
|
||
for (int i = 0; i < replyList.Count; i++)
|
||
{
|
||
Toggle newUI = GameObject.Instantiate(togPrefab, togPrefabGroup.transform);
|
||
newUI.transform.Find("Label").GetComponent<Text>().text = replyList[i].replyText;
|
||
newUI.gameObject.SetActive(true);
|
||
newUI.isOn = false;
|
||
newUI.transform.SetSiblingIndex(i);
|
||
newUI.onValueChanged.AddListener((bool value) => OnSelect(newUI));
|
||
newUI.name = i.ToString();
|
||
allChoose.Add(newUI);
|
||
}
|
||
if (currentData.type == 1)
|
||
{
|
||
input.gameObject.SetActive(false);
|
||
}
|
||
else if (currentData.type == 2)
|
||
{
|
||
for (int i = 0; i < allChoose.Count; i++)
|
||
{
|
||
allChoose[i].group = null;
|
||
}
|
||
input.gameObject.SetActive(false);
|
||
}
|
||
else if (currentData.type == 3)
|
||
{
|
||
input.gameObject.SetActive(true);
|
||
}
|
||
|
||
if (currentData.problemDescribeFiles != null)
|
||
{
|
||
if (currentData.problemDescribeFiles.Count > 0)
|
||
{
|
||
for (int i = 0; i < currentData.problemDescribeFiles.Count; i++)
|
||
{
|
||
var item = currentData.problemDescribeFiles[i];
|
||
var newUI = GameObject.Instantiate(imgShowInfo, imgShowPanel);
|
||
StartCoroutine(DownSprite(newUI, item.filePath));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
imgShowObj.gameObject.SetActive(false);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
imgShowObj.gameObject.SetActive(false);
|
||
}
|
||
|
||
if (isExit)//<2F>˳<EFBFBD><CBB3><EFBFBD>ʾ
|
||
{
|
||
//if(currentData.problemFeedbackFiles)
|
||
}
|
||
}
|
||
|
||
IEnumerator DownSprite(Image img, string path)
|
||
{
|
||
UnityWebRequest wr = new UnityWebRequest(path);
|
||
DownloadHandlerTexture texD1 = new DownloadHandlerTexture(true);
|
||
wr.downloadHandler = texD1;
|
||
yield return wr.SendWebRequest();
|
||
int width = 280;
|
||
int high = 100;
|
||
if (!wr.isNetworkError)
|
||
{
|
||
Texture2D tex = new Texture2D(width, high);
|
||
tex = texD1.texture;
|
||
//<2F><><EFBFBD>汾<EFBFBD><E6B1BE>
|
||
//Byte[] bytes = tex.EncodeToPNG();
|
||
//File.WriteAllBytes(Application.dataPath + "/" + data.name + ".png", bytes);
|
||
if (tex != null)
|
||
{
|
||
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
||
img.GetComponent<Image>().sprite = sprite;
|
||
img.gameObject.SetActive(true);
|
||
}
|
||
//transform.GetComponent<Image>().sprite = sprite;
|
||
}
|
||
}
|
||
|
||
private void OnClickSubmit()
|
||
{
|
||
EventCenter.dispatcher.SendMessage(MsgType.OnSubmitQuestion, null);
|
||
}
|
||
|
||
private void OnSelect(Toggle t)
|
||
{
|
||
if (t.isOn)
|
||
{
|
||
currentSelectIndex = int.Parse(t.name);
|
||
if (t.group != null)
|
||
t.group.allowSwitchOff = false;
|
||
}
|
||
}
|
||
|
||
public int GetTogSelect()
|
||
{
|
||
return currentSelectIndex;
|
||
}
|
||
|
||
public string GetTogSelectStr()
|
||
{
|
||
return allChoose[currentSelectIndex].transform.Find("Label").GetComponent<Text>().text;
|
||
}
|
||
|
||
public string GetAllSelectStr()
|
||
{
|
||
var ints = GetAllSelect();
|
||
string str = "";
|
||
for (int i = 0; i < ints.Count; i++)
|
||
{
|
||
str += allChoose[currentSelectIndex].transform.Find("Label").GetComponent<Text>().text;
|
||
if(i != ints.Count -1)
|
||
{
|
||
str += ",";
|
||
}
|
||
}
|
||
return str;
|
||
}
|
||
|
||
public List<int> GetAllSelect()
|
||
{
|
||
List<int> result = new List<int>();
|
||
for (int i = 0; i < allChoose.Count; i++)
|
||
{
|
||
if (allChoose[i].isOn)
|
||
{
|
||
result.Add(i);
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public string GetStrAnswer()
|
||
{
|
||
return input.text;
|
||
}
|
||
|
||
public void SetChooseSingle(int index)
|
||
{
|
||
if (index == -1) return;
|
||
allChoose[index].isOn = true;
|
||
}
|
||
|
||
public void SetMoreChoose(List<int> index)
|
||
{
|
||
if (index == null) return;
|
||
if (index.Count == 0) return;
|
||
for (int i = 0; i < index.Count; i++)
|
||
{
|
||
if (index[i] < allChoose.Count)
|
||
{
|
||
allChoose[i].isOn = true;
|
||
}
|
||
}
|
||
}
|
||
|
||
public void SetStrAnswer(string str)
|
||
{
|
||
if (str == null) return;
|
||
input.text = str;
|
||
}
|
||
}
|