質問編集履歴
3
完成コードを記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -161,3 +161,129 @@
|
|
161
161
|
|
162
162
|
|
163
163
|

|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
###修正済み完成コード
|
168
|
+
|
169
|
+
using UnityEngine;
|
170
|
+
|
171
|
+
using System;
|
172
|
+
|
173
|
+
using UnityEngine.UI;
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
public class Calc : MonoBehaviour
|
178
|
+
|
179
|
+
{
|
180
|
+
|
181
|
+
//★合計金額表示用のText用変数
|
182
|
+
|
183
|
+
public Text TotalText;
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
//InputFieldに入力されたものを受け取る。受け取る時の形式はInputField
|
188
|
+
|
189
|
+
InputField textInput1;
|
190
|
+
|
191
|
+
InputField textInput2;
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
void Start()
|
198
|
+
|
199
|
+
{
|
200
|
+
|
201
|
+
//Textコンポーネントの取得は最初の一度だけでいいので、Start内で行う
|
202
|
+
|
203
|
+
textInput1 = GameObject.Find("FieldInput1").GetComponent<InputField>();//「("FieldInput1_Text")」ではなく「("FieldInput1")」。FieldInput1_TextオブジェクトにはInputFieldが付いていない。「("Canvas/FieldInput1")」でもOK。
|
204
|
+
|
205
|
+
textInput2 = GameObject.Find("FieldInput2").GetComponent<InputField>();
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
//★TotalTextに出力するにはvoid Start(){}内でGameObjectを宣言しておく必要がある。これがあれば、一番下の「TotalText.text = Total.ToString();」がNull Reference Exceptionにならない。
|
210
|
+
|
211
|
+
//★詳しくは「http://docs.unity3d.com/ja/current/Manual/ControllingGameObjectsComponents.html」を参照
|
212
|
+
|
213
|
+
TotalText = GameObject.Find("TotalText").GetComponent< Text > ();
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
//入力されたテキストを受け取ってコンソールにログを吐き出す
|
222
|
+
|
223
|
+
public void ValueChange(string text)
|
224
|
+
|
225
|
+
{
|
226
|
+
|
227
|
+
//★HierarchyのFieldInput1にCalc.csをAddComponentし、On Value Changedを+し、FieldInput1をドラッグ&ドロップ1し、プルダウンでCalc>ValueChangeを選択
|
228
|
+
|
229
|
+
//★On End Editも+し、FieldInput1をドラッグ&ドロップ1し、プルダウンでCalc>CalcTotal()を選択
|
230
|
+
|
231
|
+
//★HierarchyのFieldInput2にCalc.csをAddComponentし、On Value Changedを+し、FieldInput2をドラッグ&ドロップ1し、プルダウンでCalc>ValueChangeを選択
|
232
|
+
|
233
|
+
//★On End Editも+し、FieldInput2をドラッグ&ドロップ1し、プルダウンでCalc>CalcTotal()を選択
|
234
|
+
|
235
|
+
Debug.Log(textInput1); //textInput1を受け取れているか否かの確認
|
236
|
+
|
237
|
+
Debug.Log(textInput2); //textInput2を受け取れているか否かの確認
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
//合計値を算出
|
246
|
+
|
247
|
+
public void CalcTotal()
|
248
|
+
|
249
|
+
{
|
250
|
+
|
251
|
+
//テキストを数値に変換
|
252
|
+
|
253
|
+
int input1 = int.Parse(textInput1.text);
|
254
|
+
|
255
|
+
int input2 = int.Parse(textInput2.text);
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
//int型+int型=int型です、ValueChangeのデバッグログと見比べてみてください
|
260
|
+
|
261
|
+
int Total = input1 + input2; //Totalはローカル変数
|
262
|
+
|
263
|
+
Debug.Log(Total);
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
//計算結果をとりあえずFieldInput1_Textに戻します
|
268
|
+
|
269
|
+
//.textはstring型なので、int型をそのまま入れるとエラーを起こします
|
270
|
+
|
271
|
+
//int型の変数の後ろに.ToString()を付けるとstring型に変換されます(←ここポイント)
|
272
|
+
|
273
|
+
//.ToString()の代わりに「+ ""」でもstring型になります
|
274
|
+
|
275
|
+
//textInput1.text = total.ToString();
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
//★計算結果をTextコンポーネントに出力するために変更
|
280
|
+
|
281
|
+
//合計金額を出力するTextコンポーネント「TotalOutput」にCalc.csをAddComponent
|
282
|
+
|
283
|
+
//InspectorのTotalTextにHierarchyからTotalOutputをドラッグ&ドロップ
|
284
|
+
|
285
|
+
TotalText.text = Total.ToString();//intのTotalをToStringでString化して、TotalTextに代入
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
}
|
2
Hierarchyのスクリーンショットを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -157,3 +157,7 @@
|
|
157
157
|
|
158
158
|
|
159
159
|
何卒、よろしくお願い申し上げます。
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+

|
1
TextFieldを用いた方法の検討を追記しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
【Unity】InputFieldで得た数値の計算結果をText
|
1
|
+
【Unity】InputFieldで得た数値の計算結果を配置済みのTextコンポーネントに表示したい
|
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
①Unityにおいて、InputFieldで得た数値(入力1と入力2)の合計値を算出
|
4
4
|
|
5
|
-
②計算結果をuGUIのText
|
5
|
+
②計算結果をuGUIのTextコンポーネントに表示させたい
|
6
6
|
|
7
7
|
|
8
8
|
|
@@ -14,7 +14,9 @@
|
|
14
14
|
|
15
15
|
そもそも合計値を算出できているか否かすら不明です。Debug.Logでも出てきません。
|
16
16
|
|
17
|
-
uGUIで出したText
|
17
|
+
uGUIで出したTextコンポーネントに計算結果を代入して出力する方法を数日書けて調べたのですが、いくら調べてもわかりません。
|
18
|
+
|
19
|
+
(Textコンポーネントはその場でスクリプトで生成するものではなく、予め設置してあるものを使用したい)
|
18
20
|
|
19
21
|
|
20
22
|
|
@@ -148,4 +150,10 @@
|
|
148
150
|
|
149
151
|
|
150
152
|
|
153
|
+
TextFieldをスクリプトで生成して表示させる方法は、最終的な出力のイメージが異なっております。また、足し算の結果は相変わらず出力できませんでした。
|
154
|
+
|
155
|
+
http://techacademy.jp/magazine/4198
|
156
|
+
|
157
|
+
|
158
|
+
|
151
159
|
何卒、よろしくお願い申し上げます。
|