Unity 2D(5.6.1)でクイズを出題するアプリをつくっています。
その中に解答の文字をパネルから選ぶ形式のモードがあります。
知っている方に説明すると、QMAの「文字パネル」と同じ形式です。
具体的には、解答欄に
あ い う え お
か き く け こ
と並んでいて、答えが「きあい」ならき、あ、いとボタンを押すことで解答します。
いま、ボタンを押すと、押した文字が表示される部分を作っています。
ボタンの上に□□□と枠が並んでいて、き、あ、いと押すと□それぞれに文字が表示されるようにしたいと思っています。
この表示枠は、背景をSpriteとして置き、その上にUI Textを貼り付けています。
クイズの答えとなる文字数は変化するので、問題を読み込む度にこの枠の位置を設定したいと思っています。
そのコードを組んだのですが、Sprite部分は正しく並んでいる(中央揃え)なのに、
Text部分はなぜか「1問前の解答文字数」を反映してしまい、表示がおかしくなっています。
1問目ではUnity側でデフォルトに配置した位置にTextが出てしまいます。
これが1問目をうつしたスクリーンショットです。解答は5文字で、背景の黒い部分は正しく設定されているのに文字はデフォルトの位置になっているのでずれてしまっています。
C#
1public class PanelQuizManager : MonoBehaviour, QuizManager { 2 3 private GameObject[] panels; //解答パネル 4 private GameObject[] panelTexts; //解答パネルに表示する文字 5 private GameObject[] inputPanes; //解答表示パネル 6 private GameObject[] inputPaneTexts; //解答表示パネルに表示する文字(バグが起きている) 7 private GameObject question; 8 9 public string answerText { get; set; } 10 11 private PanelQuiz quiz; 12 private string inputAnswer = ""; // 現在入力中の解答 13 14 private System.Random random; 15 16 public PanelQuiz Quiz { 17 get { 18 return quiz; 19 } 20 set { 21 quiz = value; 22 panels = new GameObject[10]; 23 panels[0] = GameObject.Find("PanelButton1"); 24 panels[1] = GameObject.Find("PanelButton2"); 25 panels[2] = GameObject.Find("PanelButton3"); 26 panels[3] = GameObject.Find("PanelButton4"); 27 panels[4] = GameObject.Find("PanelButton5"); 28 panels[5] = GameObject.Find("PanelButton6"); 29 panels[6] = GameObject.Find("PanelButton7"); 30 panels[7] = GameObject.Find("PanelButton8"); 31 panels[8] = GameObject.Find("PanelButton9"); 32 panels[9] = GameObject.Find("PanelButton10"); 33 panelTexts = new GameObject[10]; 34 panelTexts[0] = GameObject.Find("PanelText1"); 35 panelTexts[1] = GameObject.Find("PanelText2"); 36 panelTexts[2] = GameObject.Find("PanelText3"); 37 panelTexts[3] = GameObject.Find("PanelText4"); 38 panelTexts[4] = GameObject.Find("PanelText5"); 39 panelTexts[5] = GameObject.Find("PanelText6"); 40 panelTexts[6] = GameObject.Find("PanelText7"); 41 panelTexts[7] = GameObject.Find("PanelText8"); 42 panelTexts[8] = GameObject.Find("PanelText9"); 43 panelTexts[9] = GameObject.Find("PanelText10"); 44 inputPanes = new GameObject[8]; 45 inputPanes[0] = GameObject.Find("InputPane1"); 46 inputPanes[1] = GameObject.Find("InputPane2"); 47 inputPanes[2] = GameObject.Find("InputPane3"); 48 inputPanes[3] = GameObject.Find("InputPane4"); 49 inputPanes[4] = GameObject.Find("InputPane5"); 50 inputPanes[5] = GameObject.Find("InputPane6"); 51 inputPanes[6] = GameObject.Find("InputPane7"); 52 inputPanes[7] = GameObject.Find("InputPane8"); 53 inputPaneTexts = new GameObject[8]; 54 inputPaneTexts[0] = GameObject.Find("InputPaneText1"); 55 inputPaneTexts[1] = GameObject.Find("InputPaneText2"); 56 inputPaneTexts[2] = GameObject.Find("InputPaneText3"); 57 inputPaneTexts[3] = GameObject.Find("InputPaneText4"); 58 inputPaneTexts[4] = GameObject.Find("InputPaneText5"); 59 inputPaneTexts[5] = GameObject.Find("InputPaneText6"); 60 inputPaneTexts[6] = GameObject.Find("InputPaneText7"); 61 inputPaneTexts[7] = GameObject.Find("InputPaneText8"); 62 question = GameObject.Find("QuestionText"); 63 question.GetComponent<Text>().text = ""; 64 65 question.GetComponent<Text>().alignment = TextAnchor.UpperLeft; 66 67 random = new System.Random(); 68 69 startQuestion(); 70 } 71 } 72 73 private void startQuestion() { 74 int choiceNumber = quiz.Choice.Length; 75 List<string> choices = new List<string>(); 76 for (int i = 0; i < choiceNumber; i++) { 77 choices.Add(quiz.Choice[i].ToString()); 78 } 79 if (choiceNumber == 8) { 80 for (int i = 0; i < 8; i++) { 81 //解答パネルの位置設定 82 int index = random.Next(0, choices.Count); 83 panelTexts[i].GetComponent<Text>().text = choices[index]; 84 float panelX = (float)(i % 4 * 2 - 3.0); 85 float panelY = (float)(i / 4 * (-2.25) - 1.5); 86 float textX = i % 4 * 216 - 344 + 960; 87 float textY = i / 4 * (-240) - 160 + 540; 88 panels[i].transform.position = new Vector2(panelX, panelY); 89 panelTexts[i].transform.position = new Vector2(textX, textY); 90 choices.RemoveAt(index); 91 } 92 for (int i = 0; i < 2; i++) { 93 //いらないパネルを画面外に飛ばす 94 panels[8 + i].transform.position = new Vector2(30, 0); 95 panelTexts[8 + i].transform.position = new Vector2(3000, 0); 96 } 97 for (int i = 0; i < 3; i++) { 98 //解答表示パネルの位置設定 99 float inputPaneX = (float)(i * 1.2 - 1.2); 100 float inputPaneTextX = (float)(i * 1.2 - 1.2); 101 inputPanes[i].transform.position = new Vector2(inputPaneX, (float)0.15); 102 inputPaneTexts[i].transform.position = new Vector2(inputPaneTextX, (float)0.2); 103 inputPaneTexts[i].GetComponent<Text>().text = ""; 104 } 105 for (int i = 0; i < 5; i++) { 106 //いらないパネルを画面外に飛ばす 107 inputPanes[3 + i].transform.position = new Vector2(30, 0); 108 inputPaneTexts[3 + i].transform.position = new Vector2(3000, 0); 109 } 110 } else { 111 for (int i = 0; i < 10; i++) { 112 //解答パネルの位置設定 113 int index = random.Next(0, choices.Count); 114 panelTexts[i].GetComponent<Text>().text = choices[index]; 115 float panelX = (float)(i % 5 * 2 - 4.0); 116 float panelY = (float)(i / 5 * (-2.25) - 1.5); 117 float textX = i % 5 * 216 - 432 + 960; 118 float textY = i / 5 * (-240) - 160 + 540; 119 panels[i].transform.position = new Vector2(panelX, panelY); 120 panelTexts[i].transform.position = new Vector2(textX, textY); 121 choices.RemoveAt(index); 122 } 123 int answerLength = quiz.Answer.Length; 124 for (int i = 0; i < answerLength; i++) { 125 //解答表示パネルの位置設定 126 float inputPaneX = (float)((i - ((answerLength - 1) / 2.0)) * 1.2); 127 float inputPaneTextX = (float)((i - ((answerLength - 1) / 2.0)) * 1.2); 128 inputPanes[i].transform.position = new Vector2(inputPaneX, (float)0.15); 129 inputPaneTexts[i].transform.position = new Vector2(inputPaneTextX, (float)0.2); 130 Debug.Log(inputPaneTexts[i].transform.position); 131 inputPaneTexts[i].GetComponent<Text>().text = ""; 132 } 133 for (int i = 0; i < 8 - answerLength; i++) { 134 //いらないパネルを画面外に飛ばす 135 inputPanes[answerLength + i].transform.position = new Vector2(30, 0); 136 inputPaneTexts[answerLength + i].transform.position = new Vector2(3000, 0); 137 } 138 } 139 resultText.GetComponent<Text>().text = ""; 140 answerText = quiz.Answer; 141 } 142}
Textを設定している部分は
C#
1 for (int i = 0; i < 3; i++) { 2 //解答表示パネルの位置設定 3 float inputPaneX = (float)(i * 1.2 - 1.2); 4 float inputPaneTextX = (float)(i * 1.2 - 1.2); 5 inputPanes[i].transform.position = new Vector2(inputPaneX, (float)0.15); 6 inputPaneTexts[i].transform.position = new Vector2(inputPaneTextX, (float)0.2); 7 inputPaneTexts[i].GetComponent<Text>().text = ""; 8 }
と
C#
1 int answerLength = quiz.Answer.Length; 2 for (int i = 0; i < answerLength; i++) { 3 //解答表示パネルの位置設定 4 float inputPaneX = (float)((i - ((answerLength - 1) / 2.0)) * 1.2); 5 float inputPaneTextX = (float)((i - ((answerLength - 1) / 2.0)) * 1.2); 6 inputPanes[i].transform.position = new Vector2(inputPaneX, (float)0.15); 7 inputPaneTexts[i].transform.position = new Vector2(inputPaneTextX, (float)0.2); 8 Debug.Log(inputPaneTexts[i].transform.position); 9 inputPaneTexts[i].GetComponent<Text>().text = ""; 10 }
の部分です(細かく言うと、選択肢が8個のとき解答文字数は3、選択肢が10個のとき解答文字数は4~6なのでこのような処理になっています)。
Spriteと同じように設定しているはずなのに(しかも選択肢の部分は同じように設定して動いているのに)、このようなズレがでるのはなぜなのでしょうか。ご教授お願いします。
あなたの回答
tips
プレビュー