質問編集履歴

3

追記

2018/11/19 14:11

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -52,9 +52,9 @@
52
52
 
53
53
 
54
54
 
55
- float t = 0;
55
+ float t = 0;
56
-
56
+
57
- float x = 0;
57
+ float x = 0;
58
58
 
59
59
 
60
60
 
@@ -115,3 +115,159 @@
115
115
  error CS0103: The name `x' does not exist in the current context
116
116
 
117
117
  ```
118
+
119
+
120
+
121
+ ### ご回答を受けて。
122
+
123
+
124
+
125
+ ```
126
+
127
+ void Start () {
128
+
129
+ StartCoroutine(ChangeCoroutine(cube.transform.position.x, 5f, 3f, (posx)=>{cube.transform.position = new Vector3(posx, cube.transform.position.y, cube.transform.position.z);} ));
130
+
131
+ }
132
+
133
+
134
+
135
+ IEnumerator ChangeCoroutine(float start, float end, float duration, UnityAction<float> action){
136
+
137
+
138
+
139
+ float t = 0;
140
+
141
+ float x = 0;
142
+
143
+
144
+
145
+ while(t < 1){
146
+
147
+ x = Mathf.Lerp(start, end, t);
148
+
149
+ t += Time.deltaTime/duration;
150
+
151
+ action(x);
152
+
153
+ yield return null;
154
+
155
+ }
156
+
157
+
158
+
159
+ //Mathf.Lerpに渡されるときのtが1未満でwhile文から抜ける為、調整。
160
+
161
+ x = end;
162
+
163
+ action(x);
164
+
165
+ }
166
+
167
+
168
+
169
+ ```
170
+
171
+
172
+
173
+ 分ける場合。
174
+
175
+ ```
176
+
177
+ void Start () {
178
+
179
+ StartCoroutine(ChangeCoroutine(1f, 5f, 3f, MyAction));
180
+
181
+ }
182
+
183
+
184
+
185
+ void MyAction(float posx){
186
+
187
+ cube.transform.position = new Vector3(posx, cube.transform.position.y, cube.transform.position.z);
188
+
189
+ }
190
+
191
+
192
+
193
+ IEnumerator ChangeCoroutine(float start, float end, float duration, UnityAction<float> action){
194
+
195
+
196
+
197
+ float t = 0;
198
+
199
+ float x = 0;
200
+
201
+
202
+
203
+ while(t < 1){
204
+
205
+ x = Mathf.Lerp(start, end, t);
206
+
207
+ t += Time.deltaTime/duration;
208
+
209
+ action(x);
210
+
211
+ yield return null;
212
+
213
+ }
214
+
215
+
216
+
217
+ //Mathf.Lerpに渡されるときのtが1未満でwhile文から抜ける為、調整。
218
+
219
+ x = end;
220
+
221
+ action(x);
222
+
223
+ }
224
+
225
+ ```
226
+
227
+
228
+
229
+ ### UnityActionで何でジェネリックが必要なのか自分なりの解釈。
230
+
231
+
232
+
233
+ ```
234
+
235
+ namespace UnityEngine.Events
236
+
237
+ {
238
+
239
+ public delegate void UnityAction();
240
+
241
+ public delegate void UnityAction<T0>( T0 arg0 );
242
+
243
+ public delegate void UnityAction<T0, T1>( T0 arg0, T1 arg1 );
244
+
245
+ public delegate void UnityAction<T0, T1, T2>( T0 arg0, T1 arg1, T2 arg2 );
246
+
247
+ public delegate void UnityAction<T0, T1, T2, T3>( T0 arg0, T1 arg1, T2 arg2, T3 arg3 );
248
+
249
+ }
250
+
251
+ ```
252
+
253
+
254
+
255
+ 疑問だった点、「UnityAction<T0>( T0 arg0 );」じゃなくて、「UnityAction(T0 arg0 );」でもいいのでは?という疑問がありました。
256
+
257
+ しかし、UnityActionは、呼び出すときにUnityAction(値)の形で呼び出すので、呼び出し時には、引数の型を設定できない。
258
+
259
+ UnityAction型を宣言をするときに、UnityAction<float>のようにジェネリックに型を指定して、そのUnityActionの引数が何の型を扱うものなのか指定している。上記のようにUnityActionは宣言されているので、UnityActionのジェネリックの型指定の数が、引数の数と一致するようになっている。
260
+
261
+
262
+
263
+ ```
264
+
265
+ UnityAction<T0>の型宣言をした場合、UnityAction<T0>(T0 arg0)を使用することになる。
266
+
267
+ UnityAction<T0, T1>の型宣言をした場合、UnityAction<T0, T1>( T0 arg0, T1 arg1 )を使用することになる。
268
+
269
+ UnityAction<T0, T1, T2>の型宣言をした場合、UnityAction<T0, T1, T2>( T0 arg0, T1 arg1, T2 arg2 )を使用することになる。
270
+
271
+ UnityAction<T0, T1, T2, T3>も同様。
272
+
273
+ ```

2

修正

2018/11/19 14:11

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  [以前の質問](https://teratail.com/questions/158232)でActionを使うことをご教示いただき、UnityActionを使って、
6
6
 
7
- コールバッグする方法を実装しているのですが、UnityActionの書き方が間違っているらしく、エラーが出てしまいます。
7
+ ~~コールバッグ~~コールバックする方法を実装しているのですが、UnityActionの書き方が間違っているらしく、エラーが出てしまいます。
8
8
 
9
9
  解決方法のご教示をお願い致します。
10
10
 
@@ -14,7 +14,7 @@
14
14
 
15
15
 
16
16
 
17
- Mathf.Lerpで取得した値を毎回コールバッさせて、cubeのtransfrom.postion.xに反映させることを試みています。
17
+ Mathf.Lerpで取得した値を毎回コールバッさせて、cubeのtransfrom.postion.xに反映させることを試みています。
18
18
 
19
19
 
20
20
 
@@ -103,3 +103,15 @@
103
103
  error CS1002: ; expected
104
104
 
105
105
  ```
106
+
107
+
108
+
109
+ ### 続いて発生しているエラーメッセージ
110
+
111
+
112
+
113
+ ```
114
+
115
+ error CS0103: The name `x' does not exist in the current context
116
+
117
+ ```

1

説明修正

2018/11/18 09:47

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -72,7 +72,7 @@
72
72
 
73
73
 
74
74
 
75
- //Mathf.Lerpに渡されるtが1未満でwhile文から抜ける為、調整。
75
+ //Mathf.Lerpに渡されるときのtが1未満でwhile文から抜ける為、調整。
76
76
 
77
77
  x = end;
78
78