public void OnAnswerBtnUnCheck(Toggle toggle, int idx) { if (!previewMode) { List <PollsConfig.Question> qs = PollsConfig.QuestionMap[PollsConfig.selectedDepartment.questionID]; PollsConfig.Question q = qs[numQuestion]; if (q.limit == 1) { currentAnswer[numQuestion].answers[idx] = 0; } else { currentAnswer[numQuestion].answers[idx] = 0; currentChecked--; currentAnswer[numQuestion].longtxt = ""; for (int i = 0; i < currentAnswer[numQuestion].answers.Length; ++i) { if (currentAnswer[numQuestion].answers[i] == 1) { currentAnswer[numQuestion].longtxt += (i + 1) + "." + q.answers[i] + " "; } } } Debug.Log(" currentChecked " + currentChecked); } }
private void parseJson(string guid, string json) { try { var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }; List <QuestionParser> configs = JsonConvert.DeserializeObject <List <QuestionParser> >(json, settings); List <PollsConfig.Question> qs = new List <PollsConfig.Question>(); foreach (QuestionParser d in configs) { PollsConfig.Question q = new PollsConfig.Question(); q.id = d.id; q.type = d.type; q.limit = d.limit; q.question = d.text; q.shorttext = d.shorttxt; q.indeices[0] = d.index1; q.indeices[1] = d.index2; q.indeices[2] = d.index3; q.indeices[3] = d.index4; q.indeices[4] = d.index5; q.answers[0] = d.answer1; q.answers[1] = d.answer2; q.answers[2] = d.answer3; q.answers[3] = d.answer4; q.answers[4] = d.answer5; q.answers[5] = d.answer6; q.answers[6] = d.answer7; q.answers[7] = d.answer8; q.answers[8] = d.answer9; q.answers[9] = d.answer10; q.answers[10] = d.answer11; q.answers[11] = d.answer12; q.answers[12] = d.answer13; q.answers[13] = d.answer14; q.answers[14] = d.answer15; q.answers[15] = d.answer16; q.answers[16] = d.answer17; q.answers[17] = d.answer18; q.answers[18] = d.answer19; q.answers[19] = d.answer20; q.icons[0] = d.icon1; q.icons[1] = d.icon2; q.icons[2] = d.icon3; q.icons[3] = d.icon4; q.icons[4] = d.icon5; q.icons[5] = d.icon6; q.icons[6] = d.icon7; q.icons[7] = d.icon8; q.icons[8] = d.icon9; q.icons[9] = d.icon10; q.icons[10] = d.icon11; q.icons[11] = d.icon12; qs.Add(q); } PollsConfig.QuestionMap.Add(guid, qs); } catch (Exception e) { Debug.LogError(e.Message + "\n" + e.StackTrace); } }
/// <summary> /// 设置回答按钮被按下的相应 /// currentChecked为当前有多少个回答被选中 /// 如果大于问题设置,则将Toggle设为未选中,注意由于uncheck的时候会 /// 减去currentChecked的值,所以在设置为未选中之前要先将currentChecked++ /// </summary> /// <param name="toggle"></param> /// <param name="idx"></param> public void OnAnswerBtnCheck(Toggle toggle, int idx, bool isLoogPress) { if (!previewMode) { List <PollsConfig.Question> qs = PollsConfig.QuestionMap[PollsConfig.selectedDepartment.questionID]; PollsConfig.Question q = qs[numQuestion]; if (q.limit == 1) { //if (!isLoogPress) if (Application.platform != RuntimePlatform.WindowsEditor && Time.time - lastAnswerTime < 1.3f) { #if UNITY_ANDROID Toast.ShowToast("请仔细阅读题目,谨慎作答,谢谢"); #endif //OnAnswerBtnCheck toggle.isOn = false; return; } Debug.Log("time is " + (Time.time - lastAnswerTime).ToString()); lastAnswerTime = Time.time; currentAnswer[numQuestion].id = q.id; currentAnswer[numQuestion].answers[idx] = 1; currentAnswer[numQuestion].answer = (byte)(idx + 1); currentAnswer[numQuestion].type = q.type; if (currentAnswer[numQuestion].type == 1) { currentAnswer[numQuestion].longtxt = (idx + 1).ToString(); } else if (currentAnswer[numQuestion].type == 3) { currentAnswer[numQuestion].longtxt = (5 - idx).ToString(); } else if (currentAnswer[numQuestion].type == 4) { currentAnswer[numQuestion].longtxt = (idx + 1).ToString(); } currentAnswer[numQuestion].aid = q.aid[idx].ToString(); if (q.rev1 != "") //判断是跳转还是互斥 { if (q.rev2 == "") //跳转模式 { string[] _params = q.rev1.Split(new char[1] { ',' }); int found = -1; for (int i = 0; i < _params.Length; i += 2) { if (_params[i] == (idx + 1).ToString()) { found = Int32.Parse(_params[i + 1]); break; } } if (found != -1) { foreach (PollsConfig.Question _q in qs) { if (_q.oid == found) { /*if (qs.IndexOf(_q) - numQuestion > 1) * { * for (int i = numQuestion + 1; i < qs.IndexOf(_q); ++i) * { * PollsConfig.Question __q = qs[i]; * currentAnswer[i].id = __q.id; * } * }*/ OnRightBtnClick(qs.IndexOf(_q)); } } } else { questionAccessed[numQuestion] = 1; //若无跳转,则将accessed设置为1 OnRightBtnClick(); } } else { questionAccessed[numQuestion] = 1; //若无跳转,则将accessed设置为1 OnRightBtnClick(); } } else { OnRightBtnClick(); } } else if (currentChecked < q.limit) { currentAnswer[numQuestion].answers[idx] = 1; currentAnswer[numQuestion].type = q.type; currentAnswer[numQuestion].id = q.id; currentChecked++; currentAnswer[numQuestion].longtxt = ""; currentAnswer[numQuestion].aid = ""; for (int i = 0; i < currentAnswer[numQuestion].answers.Length; ++i) { if (currentAnswer[numQuestion].answers[i] == 1) { currentAnswer[numQuestion].longtxt += (i + 1).ToString() + ","; currentAnswer[numQuestion].aid += q.aid[i].ToString() + ","; } } currentAnswer[numQuestion].aid = currentAnswer[numQuestion].aid.Remove(currentAnswer[numQuestion].aid.Length - 1); } else { #if UNITY_ANDROID Toast.ShowToast(string.Format("此题最多能选择{0}个答案", q.limit)); #endif currentChecked++; toggle.isOn = false; } Debug.Log(" currentChecked " + currentChecked); } }
void StartQuestions() { //leftAvatarImage.enabled = (numQuestion % 2 == 0); //rightAvatarImage.enabled = (numQuestion % 2 != 0); leftAvatarImage.enabled = true; rightAvatarImage.enabled = true; if (numQuestion % 2 == 0) { leftAvatarImage.sprite = Resources.Load <Sprite>("avatar/man" + UnityEngine.Random.Range(1, 7)); leftAvatarImage.SetNativeSize(); } else { rightAvatarImage.sprite = Resources.Load <Sprite>("avatar/woman" + UnityEngine.Random.Range(1, 7)); rightAvatarImage.SetNativeSize(); } List <PollsConfig.Question> qs = PollsConfig.QuestionMap[PollsConfig.selectedDepartment.questionID]; ProgressText.text = (numQuestion + 1) + " / " + qs.Count.ToString(); progressSlider.value = (float)(numQuestion) / (float)(qs.Count - 1); if (qs != null) { //take photo if ((numQuestion == 1 || numQuestion == qs.Count - 1 || numQuestion == (qs.Count - 1) / 3 || numQuestion == (qs.Count - 1) / 3 * 2) && webcamTexture != null) { if (frameCaptured[numQuestion] == 0) { webcamTexture.Play(); //webcamTexture.Pause(); Invoke("CaptureWebCamTexture", 2.0f); frameCaptured[numQuestion] = 1; } } PollsConfig.Question q = qs[numQuestion]; if (!previewMode) { currentAnswer[numQuestion].startTime = DateTime.Now; } leftButton.gameObject.SetActive(numQuestion != 0); if (numQuestion == qs.Count - 1) { rightButton.GetComponent <Image>().sprite = doneNormalSprite; SpriteState ss = new SpriteState(); ss.highlightedSprite = doneNormalSprite; ss.pressedSprite = donePressSprite; ss.disabledSprite = null; rightButton.GetComponent <Button>().spriteState = ss; rightButton.gameObject.SetActive(true); } else { rightButton.GetComponent <Image>().sprite = rightNormalSprite; SpriteState ss = new SpriteState(); ss.highlightedSprite = rightNormalSprite; ss.pressedSprite = rightPressSprite; ss.disabledSprite = null; rightButton.GetComponent <Button>().spriteState = ss; if (!previewMode) { //rightButton.gameObject.SetActive(q.limit > 1); //任何题目可以右转 } else { rightButton.gameObject.SetActive(true); } } textQuestion.text = (numQuestion + 1).ToString() + "." + q.question; currentChecked = 0; if (q.type == 1 || q.type == 2) { Image bg = GetComponent <Image>(); bg.sprite = textAnswerBG; //count question int qcount = 0; for (int i = 0; i < grid.transform.childCount; i++) { Destroy(grid.transform.GetChild(i).gameObject); } grid.gameObject.SetActive(true); for (int i = 0; i < iconGrid.transform.childCount; i++) { Destroy(iconGrid.transform.GetChild(i).gameObject); } iconGrid.gameObject.SetActive(false); for (int i = 0; i < 20; i++) { if (q.answers[i] != null && q.answers[i] != "") { qcount++; } } int c = (int)(qcount / 5.0f + 0.9f); int r = qcount % 5; if (c > 1) { r = 5; } else if (r == 0) { r = c * 5; } for (int i = 0; i < 20; i++) { if (q.answers[i] != null && q.answers[i] != "") { //判断是否有互斥 bool found = false; if (PollsConfig.QExcludeMap.ContainsKey(q.oid.ToString())) { //找到idxb PollsConfig.QExclude qe = PollsConfig.QExcludeMap[q.oid.ToString()]; foreach (PollsConfig.QExclude _qe in qe.List) { foreach (PollsConfig.Question _q in qs) { if (_q.oid.ToString() == _qe.idxB) { //判断idxb的qidxb是否作答 if (currentAnswer[qs.IndexOf(_q)].answers[_qe.qidxB - 1] != 0 && _qe.qidxA == i + 1) { found = true; break; } } } } } if (found) { continue; } GameObject newone = Instantiate(Resources.Load("ui/AnswerToggle") as GameObject); if (newone != null) { newone.transform.SetParent(grid.transform); newone.transform.localScale = Vector3.one; newone.transform.position = Vector3.zero; RectTransform rti = newone.GetComponent <RectTransform>(); rti.anchoredPosition3D = new Vector3(rti.anchoredPosition3D.x, rti.anchoredPosition3D.y, 0); newone.GetComponentInChildren <Text>().text = q.answers[i]; if (r == 1) { newone.GetComponentInChildren <Text>().fontSize = 50; } else if (r == 2) { newone.GetComponentInChildren <Text>().fontSize = 40; } else if (r == 3) { newone.GetComponentInChildren <Text>().fontSize = 32; } else if (r == 4) { newone.GetComponentInChildren <Text>().fontSize = 30; } else if (r == 5) { newone.GetComponentInChildren <Text>().fontSize = 26; } AnswerBtn ab = newone.GetComponent <AnswerBtn>(); ab.index = i; ab.controller = this; if (q.limit == 1) { Toggle toggle = newone.GetComponent <Toggle>(); toggle.group = grid.GetComponent <ToggleGroup>(); } if (!previewMode) { if (currentAnswer[numQuestion].answers[i] != 0) { Toggle toggle = newone.GetComponent <Toggle>(); ab.ignoreOnce = true; toggle.isOn = true; currentChecked++; } } } } } float ymax = (grid.GetComponent <RectTransform>().sizeDelta.x - 50) / r * 0.4f; ymax = Math.Min(ymax, (grid.GetComponent <RectTransform>().sizeDelta.y - 50) / c); float xmax = ymax / 0.4f; xmax = Math.Min(xmax, 330); grid.cellSize = new Vector2(xmax, xmax * 0.4f); //grid.GetComponent<RectTransform>().sizeDelta.y; } else { Image bg = GetComponent <Image>(); bg.sprite = iconAnswerBG; for (int i = 0; i < grid.transform.childCount; i++) { Destroy(grid.transform.GetChild(i).gameObject); } grid.gameObject.SetActive(false); for (int i = 0; i < iconGrid.transform.childCount; i++) { Destroy(iconGrid.transform.GetChild(i).gameObject); } int qcount = 0; iconGrid.gameObject.SetActive(true); for (int i = 0; i < 13; i++) { if (q.answers[i] != null && q.answers[i] != "") { GameObject newone = Instantiate(Resources.Load("ui/IconAnswerToggle") as GameObject); if (newone != null) { newone.transform.SetParent(iconGrid.transform); newone.transform.localScale = Vector3.one; newone.transform.position = Vector3.zero; RectTransform rti = newone.GetComponent <RectTransform>(); rti.anchoredPosition3D = new Vector3(rti.anchoredPosition3D.x, rti.anchoredPosition3D.y, 0); AnswerBtn ab = newone.GetComponent <AnswerBtn>(); ab.index = i; ab.controller = this; Toggle toggle = newone.GetComponent <Toggle>(); //toggle.spriteState.highlightedSprite = emojiNormalSprite[q.icons[i]]; SpriteState ss = new SpriteState(); if (q.type == 3) { ss.highlightedSprite = emojiNormalSprite[Int32.Parse(q.icons[i]) - 1]; ss.pressedSprite = emojiNormalSprite[Int32.Parse(q.icons[i]) - 1]; ss.disabledSprite = null; toggle.spriteState = ss; ab.imageNormal.sprite = emojiNormalSprite[Int32.Parse(q.icons[i]) - 1]; ab.imageSelect.sprite = emojiDisableSprite[Int32.Parse(q.icons[i]) - 1]; ab.answerText.text = q.answers[i]; ab.answerNumber.text = ""; } else { ss.highlightedSprite = emojiNormalSprite[5]; ss.pressedSprite = emojiNormalSprite[5]; ss.disabledSprite = null; toggle.spriteState = ss; ab.imageNormal.sprite = emojiNormalSprite[5]; ab.imageSelect.sprite = emojiDisableSprite[5]; ab.answerText.text = ""; ab.answerNumber.text = q.answers[i]; } if (q.limit == 1) { toggle = newone.GetComponent <Toggle>(); toggle.group = iconGrid.GetComponent <ToggleGroup>(); } if (!previewMode) { if (currentAnswer[numQuestion].answers[i] != 0) { toggle = newone.GetComponent <Toggle>(); ab.ignoreOnce = true; toggle.isOn = true; } } qcount++; } } } iconGrid.cellSize = new Vector2(Math.Min(150 * (6.0f / (float)qcount), 150), Math.Min(150 * (6.0f / (float)qcount), 150) * 1.0f); if (qcount == 5 && q.type == 3) { iconGrid.spacing = new Vector2(50, 5); } else { iconGrid.spacing = new Vector2(5, 5); } } } }
private void parseJson() { try { if (!previewMode) { if (photoList != null) { photoList.Clear(); } currentAnswer = new List <PollsConfig.Answer>(); photoList = new List <byte[]>(); } if (!PollsConfig.QuestionMap.ContainsKey(jsonPath)) { var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }; List <PollsConfig.QuestionParser> configs = JsonConvert.DeserializeObject <List <PollsConfig.QuestionParser> >(json, settings); List <PollsConfig.Question> qs = new List <PollsConfig.Question>(); foreach (PollsConfig.QuestionParser d in configs) { PollsConfig.Question q = new PollsConfig.Question(); q.id = d.id; q.type = d.type; q.limit = d.limit; q.question = d.text; q.shorttext = d.shorttxt; if (d.originalid != null) { q.oid = d.originalid != "" ? Int32.Parse(d.originalid) : 0; } else { q.oid = 0; } q.rev1 = d.rev1; q.rev2 = d.rev2; if (q.rev1 != null && q.rev1 != "") //判断是跳转还是互斥 { if (q.rev2 != null && q.rev2 != "") //跳转模式 { try { string[] _params = q.rev1.Split(new char[1] { ',' }); string[] _params2 = q.rev2.Split(new char[1] { ',' }); for (int i = 0; i < _params.Length; i += 2) { PollsConfig.QExclude qe = new PollsConfig.QExclude(); qe.idxA = _params[i]; qe.idxB = _params2[i]; qe.qidxA = Int32.Parse(_params[i + 1]); qe.qidxB = Int32.Parse(_params2[i + 1]); PollsConfig.QExclude qe2 = new PollsConfig.QExclude(); qe2.idxA = _params2[i]; qe2.idxB = _params[i]; qe2.qidxA = Int32.Parse(_params2[i + 1]); qe2.qidxB = Int32.Parse(_params[i + 1]); if (PollsConfig.QExcludeMap.ContainsKey(qe.idxA)) { PollsConfig.QExcludeMap[qe.idxA].List.Add(qe); } else { PollsConfig.QExclude _qe = new PollsConfig.QExclude(); _qe.List.Add(qe); PollsConfig.QExcludeMap.Add(qe.idxA, _qe); } if (PollsConfig.QExcludeMap.ContainsKey(qe.idxB)) { PollsConfig.QExcludeMap[qe.idxB].List.Add(qe2); } else { PollsConfig.QExclude _qe = new PollsConfig.QExclude(); _qe.List.Add(qe2); PollsConfig.QExcludeMap.Add(qe.idxB, _qe); } } } catch { Toast.ShowToast("解析跳转题目失败, 请检查题库"); } } } q.indeices[0] = d.index1; q.indeices[1] = d.index2; q.indeices[2] = d.index3; q.indeices[3] = d.index4; q.indeices[4] = d.index5; q.answers[0] = d.answer1; q.answers[1] = d.answer2; q.answers[2] = d.answer3; q.answers[3] = d.answer4; q.answers[4] = d.answer5; q.answers[5] = d.answer6; q.answers[6] = d.answer7; q.answers[7] = d.answer8; q.answers[8] = d.answer9; q.answers[9] = d.answer10; q.answers[10] = d.answer11; q.answers[11] = d.answer12; q.answers[12] = d.answer13; q.answers[13] = d.answer14; q.answers[14] = d.answer15; q.answers[15] = d.answer16; q.answers[16] = d.answer17; q.answers[17] = d.answer18; q.answers[18] = d.answer19; q.answers[19] = d.answer20; q.icons[0] = d.icon1; q.icons[1] = d.icon2; q.icons[2] = d.icon3; q.icons[3] = d.icon4; q.icons[4] = d.icon5; q.icons[5] = d.icon6; q.icons[6] = d.icon7; q.icons[7] = d.icon8; q.icons[8] = d.icon9; q.icons[9] = d.icon10; q.icons[10] = d.icon11; q.icons[11] = d.icon12; qs.Add(q); if (!previewMode) { PollsConfig.Answer a = new PollsConfig.Answer(); a.limit = q.limit; a.shorttxt = q.shorttext; a.longtxt = ""; currentAnswer.Add(a); } } PollsConfig.QuestionMap.Add(jsonPath, qs); } else { if (!previewMode) { List <PollsConfig.Question> qs = PollsConfig.QuestionMap[PollsConfig.selectedDepartment.questionID]; foreach (PollsConfig.Question q in qs) { PollsConfig.Answer a = new PollsConfig.Answer(); a.limit = q.limit; a.shorttxt = q.shorttext; a.longtxt = ""; currentAnswer.Add(a); } } } holdPanel.SetActive(false); numQuestion = 0; if (!previewMode) { userGuid = Guid.NewGuid().ToString(); OpenWebCamera(); CreateTmpFolder(); } StartQuestions(); } catch (Exception e) { Debug.LogError(e.Message + "\n" + e.StackTrace); } }
//private void parseJson(string guid, string json) private void parseJson(string guid, List <PollsConfig.QuestionParser> configs) { try { PollsConfig.QExcludeMap.Clear(); List <PollsConfig.Question> qs = new List <PollsConfig.Question>(); foreach (PollsConfig.QuestionParser d in configs) { PollsConfig.Question q = new PollsConfig.Question(); q.id = d.id; q.oid = d.originalid != "" ? Int32.Parse(d.originalid) : 0; q.type = d.type; q.limit = d.limit; q.question = d.text; q.shorttext = d.shorttxt; q.rev1 = d.rev1; q.rev2 = d.rev2; if (q.rev1 != "") //判断是跳转还是互斥 { if (q.rev2 != "") //跳转模式 { try { string[] _params = q.rev1.Split(new char[1] { ',' }); string[] _params2 = q.rev2.Split(new char[1] { ',' }); for (int i = 0; i < _params.Length; i += 2) { PollsConfig.QExclude qe = new PollsConfig.QExclude(); qe.idxA = _params[i]; qe.idxB = _params2[i]; qe.qidxA = Int32.Parse(_params[i + 1]); qe.qidxB = Int32.Parse(_params2[i + 1]); PollsConfig.QExclude qe2 = new PollsConfig.QExclude(); qe2.idxA = _params2[i]; qe2.idxB = _params[i]; qe2.qidxA = Int32.Parse(_params2[i + 1]); qe2.qidxB = Int32.Parse(_params[i + 1]); if (PollsConfig.QExcludeMap.ContainsKey(qe.idxA)) { PollsConfig.QExcludeMap[qe.idxA].List.Add(qe); } else { PollsConfig.QExclude _qe = new PollsConfig.QExclude(); _qe.List.Add(qe); PollsConfig.QExcludeMap.Add(qe.idxA, _qe); } if (PollsConfig.QExcludeMap.ContainsKey(qe.idxB)) { PollsConfig.QExcludeMap[qe.idxB].List.Add(qe2); } else { PollsConfig.QExclude _qe = new PollsConfig.QExclude(); _qe.List.Add(qe2); PollsConfig.QExcludeMap.Add(qe.idxB, _qe); } } } catch { Toast.ShowToast("解析互斥题目失败, 请检查题库"); } } } q.indeices[0] = d.index1; q.indeices[1] = d.index2; q.indeices[2] = d.index3; q.indeices[3] = d.index4; q.indeices[4] = d.index5; q.answers[0] = d.answer1; q.answers[1] = d.answer2; q.answers[2] = d.answer3; q.answers[3] = d.answer4; q.answers[4] = d.answer5; q.answers[5] = d.answer6; q.answers[6] = d.answer7; q.answers[7] = d.answer8; q.answers[8] = d.answer9; q.answers[9] = d.answer10; q.answers[10] = d.answer11; q.answers[11] = d.answer12; q.answers[12] = d.answer13; q.answers[13] = d.answer14; q.answers[14] = d.answer15; q.answers[15] = d.answer16; q.answers[16] = d.answer17; q.answers[17] = d.answer18; q.answers[18] = d.answer19; q.answers[19] = d.answer20; q.aid[0] = d.aid1; q.aid[1] = d.aid2; q.aid[2] = d.aid3; q.aid[3] = d.aid4; q.aid[4] = d.aid5; q.aid[5] = d.aid6; q.aid[6] = d.aid7; q.aid[7] = d.aid8; q.aid[8] = d.aid9; q.aid[9] = d.aid10; q.aid[10] = d.aid11; q.aid[11] = d.aid12; q.aid[12] = d.aid13; q.aid[13] = d.aid14; q.aid[14] = d.aid15; q.aid[15] = d.aid16; q.aid[17] = d.aid17; q.aid[18] = d.aid18; q.aid[19] = d.aid19; q.icons[0] = d.icon1; q.icons[1] = d.icon2; q.icons[2] = d.icon3; q.icons[3] = d.icon4; q.icons[4] = d.icon5; q.icons[5] = d.icon6; q.icons[6] = d.icon7; q.icons[7] = d.icon8; q.icons[8] = d.icon9; q.icons[9] = d.icon10; q.icons[10] = d.icon11; q.icons[11] = d.icon12; qs.Add(q); } PollsConfig.QuestionMap.Add(guid, qs); } catch (Exception e) { Debug.LogError(e.Message + "\n" + e.StackTrace); } }