質問編集履歴
3
完成コードを記載
title
CHANGED
File without changes
|
body
CHANGED
@@ -79,4 +79,67 @@
|
|
79
79
|
|
80
80
|
何卒、よろしくお願い申し上げます。
|
81
81
|
|
82
|
-

|
82
|
+

|
83
|
+
|
84
|
+
###修正済み完成コード
|
85
|
+
using UnityEngine;
|
86
|
+
using System;
|
87
|
+
using UnityEngine.UI;
|
88
|
+
|
89
|
+
public class Calc : MonoBehaviour
|
90
|
+
{
|
91
|
+
//★合計金額表示用のText用変数
|
92
|
+
public Text TotalText;
|
93
|
+
|
94
|
+
//InputFieldに入力されたものを受け取る。受け取る時の形式はInputField
|
95
|
+
InputField textInput1;
|
96
|
+
InputField textInput2;
|
97
|
+
|
98
|
+
|
99
|
+
void Start()
|
100
|
+
{
|
101
|
+
//Textコンポーネントの取得は最初の一度だけでいいので、Start内で行う
|
102
|
+
textInput1 = GameObject.Find("FieldInput1").GetComponent<InputField>();//「("FieldInput1_Text")」ではなく「("FieldInput1")」。FieldInput1_TextオブジェクトにはInputFieldが付いていない。「("Canvas/FieldInput1")」でもOK。
|
103
|
+
textInput2 = GameObject.Find("FieldInput2").GetComponent<InputField>();
|
104
|
+
|
105
|
+
//★TotalTextに出力するにはvoid Start(){}内でGameObjectを宣言しておく必要がある。これがあれば、一番下の「TotalText.text = Total.ToString();」がNull Reference Exceptionにならない。
|
106
|
+
//★詳しくは「http://docs.unity3d.com/ja/current/Manual/ControllingGameObjectsComponents.html」を参照
|
107
|
+
TotalText = GameObject.Find("TotalText").GetComponent< Text > ();
|
108
|
+
}
|
109
|
+
|
110
|
+
|
111
|
+
//入力されたテキストを受け取ってコンソールにログを吐き出す
|
112
|
+
public void ValueChange(string text)
|
113
|
+
{
|
114
|
+
//★HierarchyのFieldInput1にCalc.csをAddComponentし、On Value Changedを+し、FieldInput1をドラッグ&ドロップ1し、プルダウンでCalc>ValueChangeを選択
|
115
|
+
//★On End Editも+し、FieldInput1をドラッグ&ドロップ1し、プルダウンでCalc>CalcTotal()を選択
|
116
|
+
//★HierarchyのFieldInput2にCalc.csをAddComponentし、On Value Changedを+し、FieldInput2をドラッグ&ドロップ1し、プルダウンでCalc>ValueChangeを選択
|
117
|
+
//★On End Editも+し、FieldInput2をドラッグ&ドロップ1し、プルダウンでCalc>CalcTotal()を選択
|
118
|
+
Debug.Log(textInput1); //textInput1を受け取れているか否かの確認
|
119
|
+
Debug.Log(textInput2); //textInput2を受け取れているか否かの確認
|
120
|
+
}
|
121
|
+
|
122
|
+
|
123
|
+
//合計値を算出
|
124
|
+
public void CalcTotal()
|
125
|
+
{
|
126
|
+
//テキストを数値に変換
|
127
|
+
int input1 = int.Parse(textInput1.text);
|
128
|
+
int input2 = int.Parse(textInput2.text);
|
129
|
+
|
130
|
+
//int型+int型=int型です、ValueChangeのデバッグログと見比べてみてください
|
131
|
+
int Total = input1 + input2; //Totalはローカル変数
|
132
|
+
Debug.Log(Total);
|
133
|
+
|
134
|
+
//計算結果をとりあえずFieldInput1_Textに戻します
|
135
|
+
//.textはstring型なので、int型をそのまま入れるとエラーを起こします
|
136
|
+
//int型の変数の後ろに.ToString()を付けるとstring型に変換されます(←ここポイント)
|
137
|
+
//.ToString()の代わりに「+ ""」でもstring型になります
|
138
|
+
//textInput1.text = total.ToString();
|
139
|
+
|
140
|
+
//★計算結果をTextコンポーネントに出力するために変更
|
141
|
+
//合計金額を出力するTextコンポーネント「TotalOutput」にCalc.csをAddComponent
|
142
|
+
//InspectorのTotalTextにHierarchyからTotalOutputをドラッグ&ドロップ
|
143
|
+
TotalText.text = Total.ToString();//intのTotalをToStringでString化して、TotalTextに代入
|
144
|
+
}
|
145
|
+
}
|
2
Hierarchyのスクリーンショットを追加しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -77,4 +77,6 @@
|
|
77
77
|
TextFieldをスクリプトで生成して表示させる方法は、最終的な出力のイメージが異なっております。また、足し算の結果は相変わらず出力できませんでした。
|
78
78
|
http://techacademy.jp/magazine/4198
|
79
79
|
|
80
|
-
何卒、よろしくお願い申し上げます。
|
80
|
+
何卒、よろしくお願い申し上げます。
|
81
|
+
|
82
|
+

|
1
TextFieldを用いた方法の検討を追記しました。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
【Unity】InputFieldで得た数値の計算結果をText
|
1
|
+
【Unity】InputFieldで得た数値の計算結果を配置済みのTextコンポーネントに表示したい
|
body
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
###前提・実現したいこと
|
2
2
|
①Unityにおいて、InputFieldで得た数値(入力1と入力2)の合計値を算出
|
3
|
-
②計算結果をuGUIのText
|
3
|
+
②計算結果をuGUIのTextコンポーネントに表示させたい
|
4
4
|
|
5
5
|
###発生している問題・エラーメッセージ
|
6
6
|
超絶初心者で申し訳ございません。JavaやC#を1年ほど週末趣味でやってきて、ようやくUnityに挑戦するぞ、となったものの、いきなり躓いております。
|
7
7
|
|
8
8
|
そもそも合計値を算出できているか否かすら不明です。Debug.Logでも出てきません。
|
9
|
-
uGUIで出したText
|
9
|
+
uGUIで出したTextコンポーネントに計算結果を代入して出力する方法を数日書けて調べたのですが、いくら調べてもわかりません。
|
10
|
+
(Textコンポーネントはその場でスクリプトで生成するものではなく、予め設置してあるものを使用したい)
|
10
11
|
|
11
12
|
エラーメッセージ
|
12
13
|
UnityException: You are not allowed to call this function when declaring a variable.
|
@@ -73,4 +74,7 @@
|
|
73
74
|
しかし、出力する方法が一向に分かりません。。。
|
74
75
|
http://www.peliphilo.net/archives/1040
|
75
76
|
|
77
|
+
TextFieldをスクリプトで生成して表示させる方法は、最終的な出力のイメージが異なっております。また、足し算の結果は相変わらず出力できませんでした。
|
78
|
+
http://techacademy.jp/magazine/4198
|
79
|
+
|
76
80
|
何卒、よろしくお願い申し上げます。
|