You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
4.7 KiB
145 lines
4.7 KiB
using UnityEngine; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine.UI; |
|
using AX.MessageSystem; |
|
|
|
/// <summary> |
|
/// Question.cs——CSV信息类 |
|
/// </summary> |
|
public class Question |
|
{ |
|
/// <summary> |
|
/// id |
|
/// </summary> |
|
public string ID { get; set; } |
|
|
|
/// <summary> |
|
/// 类型(1单选,2多选,3填空) |
|
/// </summary> |
|
public QuestionType Type { get; set; } |
|
|
|
/// <summary> |
|
/// 描述 |
|
/// </summary> |
|
public string Description { get; set; } |
|
/// <summary> |
|
/// 内容 |
|
/// </summary> |
|
public List<string> Content { get; set; } |
|
/// <summary> |
|
/// 选择题结果 |
|
/// </summary> |
|
public Dictionary<string, bool> SelectionResult = new Dictionary<string, bool>(); |
|
/// <summary> |
|
/// 评分结果 |
|
/// </summary> |
|
public Dictionary<string, int> ScoreResult = new Dictionary<string, int>(); |
|
} |
|
public enum QuestionType |
|
{ |
|
RadioSelect = 1, |
|
MultiSelect = 2, |
|
FillInTheBlanks = 3 |
|
} |
|
|
|
public class QuestionItem : MonoBehaviour |
|
{ |
|
// 单选按钮预设 |
|
public GameObject RadioSelectItem; |
|
// 多选按钮预设 |
|
public GameObject MultiSelectItem; |
|
// 填空按钮预设 |
|
public GameObject FillInTheBlanksItem; |
|
// Item父对象 |
|
public GameObject ItemParent; |
|
// 问题描述 |
|
public Text QuestionDescription; |
|
// Question |
|
private Question Data; |
|
public IEnumerator SetData(Question question) |
|
{ |
|
Data = question; |
|
if (Data.ID == "2" || Data.ID == "1") |
|
{ |
|
// 参演人员打分 |
|
ItemParent.GetComponent<GridLayoutGroup>().cellSize = new Vector2(1100, 50); |
|
} |
|
QuestionDescription.text = string.Format("{0}", Data.Description); |
|
for (int i = 0; i < Data.Content.Count; i++) |
|
{ |
|
GameObject obj = null; |
|
switch (Data.Type) |
|
{ |
|
case QuestionType.RadioSelect: |
|
obj = Instantiate(RadioSelectItem); |
|
break; |
|
case QuestionType.MultiSelect: |
|
obj = Instantiate(MultiSelectItem); |
|
break; |
|
case QuestionType.FillInTheBlanks: |
|
obj = Instantiate(FillInTheBlanksItem); |
|
break; |
|
} |
|
// 记录问题位置,大小 |
|
Vector3 tempPos = obj.transform.localPosition; |
|
Vector3 tempSca = obj.transform.localScale; |
|
// 设置父对象 |
|
obj.transform.SetParent(ItemParent.transform); |
|
obj.transform.localPosition = tempPos; |
|
obj.transform.localScale = tempSca; |
|
// 获取ItemScript |
|
QuestionSubItem sub = obj.GetComponent<QuestionSubItem>(); |
|
sub.UIText.text = Data.Content[i]; |
|
string key = Data.Content[i]; |
|
if (Data.Type == QuestionType.FillInTheBlanks) |
|
{ |
|
// 添加结果 |
|
Data.ScoreResult.Add(key, 0); |
|
// 添加事件 |
|
sub.UIInput.onEndEdit.AddListener(a => |
|
{ |
|
if (string.IsNullOrEmpty(a)) |
|
{ |
|
return; |
|
} |
|
int temp = int.Parse(a); |
|
temp = Mathf.Clamp(temp, 1, 10); |
|
sub.UIInput.text = temp.ToString(); |
|
Data.ScoreResult[key] = temp; |
|
//Debug.Log(string.Format("结果有{0}条,当前是第{1}条,结果{2}", Data.Result.Count, num, Data.Result[num])); |
|
MessageDispatcher.SendMessage("CHECK_QUESTION_BUTTON"); |
|
}); |
|
} |
|
else |
|
{ |
|
if (Data.Type == QuestionType.RadioSelect) |
|
{ |
|
// 单选组 |
|
sub.UIToggle.group = ItemParent.GetComponent<ToggleGroup>(); |
|
} |
|
// 添加结果 |
|
Data.SelectionResult.Add(key, false); |
|
// 添加事件 |
|
sub.UIToggle.onValueChanged.AddListener(a => |
|
{ |
|
Data.SelectionResult[sub.UIText.text] = a; |
|
|
|
//Debug.Log(string.Format("单选结果为:{0}", Data.Result[0])); |
|
MessageDispatcher.SendMessage("CHECK_QUESTION_BUTTON"); |
|
|
|
}); |
|
} |
|
yield return new WaitForEndOfFrame(); |
|
} |
|
// 调整大小 |
|
SetSize(); |
|
} |
|
private void SetSize() |
|
{ |
|
RectTransform temp = (ItemParent.transform as RectTransform); |
|
float y = temp.sizeDelta.y; |
|
RectTransform itemRect = (transform as RectTransform); |
|
itemRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, y + 40); |
|
} |
|
}
|
|
|