質問編集履歴
3
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -25,8 +25,8 @@
|
|
25
25
|
|
26
26
|
IEnumerator ChangeCoroutine(UnityAction<float> action, float start, float end, float duration){
|
27
27
|
|
28
|
-
|
28
|
+
float t = 0;
|
29
|
-
|
29
|
+
float x = 0;
|
30
30
|
|
31
31
|
while(t < 1){
|
32
32
|
x = Mathf.Lerp(start, end, t);
|
@@ -56,4 +56,82 @@
|
|
56
56
|
|
57
57
|
```
|
58
58
|
error CS0103: The name `x' does not exist in the current context
|
59
|
+
```
|
60
|
+
|
61
|
+
### ご回答を受けて。
|
62
|
+
|
63
|
+
```
|
64
|
+
void Start () {
|
65
|
+
StartCoroutine(ChangeCoroutine(cube.transform.position.x, 5f, 3f, (posx)=>{cube.transform.position = new Vector3(posx, cube.transform.position.y, cube.transform.position.z);} ));
|
66
|
+
}
|
67
|
+
|
68
|
+
IEnumerator ChangeCoroutine(float start, float end, float duration, UnityAction<float> action){
|
69
|
+
|
70
|
+
float t = 0;
|
71
|
+
float x = 0;
|
72
|
+
|
73
|
+
while(t < 1){
|
74
|
+
x = Mathf.Lerp(start, end, t);
|
75
|
+
t += Time.deltaTime/duration;
|
76
|
+
action(x);
|
77
|
+
yield return null;
|
78
|
+
}
|
79
|
+
|
80
|
+
//Mathf.Lerpに渡されるときのtが1未満でwhile文から抜ける為、調整。
|
81
|
+
x = end;
|
82
|
+
action(x);
|
83
|
+
}
|
84
|
+
|
85
|
+
```
|
86
|
+
|
87
|
+
分ける場合。
|
88
|
+
```
|
89
|
+
void Start () {
|
90
|
+
StartCoroutine(ChangeCoroutine(1f, 5f, 3f, MyAction));
|
91
|
+
}
|
92
|
+
|
93
|
+
void MyAction(float posx){
|
94
|
+
cube.transform.position = new Vector3(posx, cube.transform.position.y, cube.transform.position.z);
|
95
|
+
}
|
96
|
+
|
97
|
+
IEnumerator ChangeCoroutine(float start, float end, float duration, UnityAction<float> action){
|
98
|
+
|
99
|
+
float t = 0;
|
100
|
+
float x = 0;
|
101
|
+
|
102
|
+
while(t < 1){
|
103
|
+
x = Mathf.Lerp(start, end, t);
|
104
|
+
t += Time.deltaTime/duration;
|
105
|
+
action(x);
|
106
|
+
yield return null;
|
107
|
+
}
|
108
|
+
|
109
|
+
//Mathf.Lerpに渡されるときのtが1未満でwhile文から抜ける為、調整。
|
110
|
+
x = end;
|
111
|
+
action(x);
|
112
|
+
}
|
113
|
+
```
|
114
|
+
|
115
|
+
### UnityActionで何でジェネリックが必要なのか自分なりの解釈。
|
116
|
+
|
117
|
+
```
|
118
|
+
namespace UnityEngine.Events
|
119
|
+
{
|
120
|
+
public delegate void UnityAction();
|
121
|
+
public delegate void UnityAction<T0>( T0 arg0 );
|
122
|
+
public delegate void UnityAction<T0, T1>( T0 arg0, T1 arg1 );
|
123
|
+
public delegate void UnityAction<T0, T1, T2>( T0 arg0, T1 arg1, T2 arg2 );
|
124
|
+
public delegate void UnityAction<T0, T1, T2, T3>( T0 arg0, T1 arg1, T2 arg2, T3 arg3 );
|
125
|
+
}
|
126
|
+
```
|
127
|
+
|
128
|
+
疑問だった点、「UnityAction<T0>( T0 arg0 );」じゃなくて、「UnityAction(T0 arg0 );」でもいいのでは?という疑問がありました。
|
129
|
+
しかし、UnityActionは、呼び出すときにUnityAction(値)の形で呼び出すので、呼び出し時には、引数の型を設定できない。
|
130
|
+
UnityAction型を宣言をするときに、UnityAction<float>のようにジェネリックに型を指定して、そのUnityActionの引数が何の型を扱うものなのか指定している。上記のようにUnityActionは宣言されているので、UnityActionのジェネリックの型指定の数が、引数の数と一致するようになっている。
|
131
|
+
|
132
|
+
```
|
133
|
+
UnityAction<T0>の型宣言をした場合、UnityAction<T0>(T0 arg0)を使用することになる。
|
134
|
+
UnityAction<T0, T1>の型宣言をした場合、UnityAction<T0, T1>( T0 arg0, T1 arg1 )を使用することになる。
|
135
|
+
UnityAction<T0, T1, T2>の型宣言をした場合、UnityAction<T0, T1, T2>( T0 arg0, T1 arg1, T2 arg2 )を使用することになる。
|
136
|
+
UnityAction<T0, T1, T2, T3>も同様。
|
59
137
|
```
|
2
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
3
|
[以前の質問](https://teratail.com/questions/158232)でActionを使うことをご教示いただき、UnityActionを使って、
|
4
|
-
コールバッグする方法を実装しているのですが、UnityActionの書き方が間違っているらしく、エラーが出てしまいます。
|
4
|
+
~~コールバッグ~~コールバックする方法を実装しているのですが、UnityActionの書き方が間違っているらしく、エラーが出てしまいます。
|
5
5
|
解決方法のご教示をお願い致します。
|
6
6
|
|
7
7
|
### 試したこと
|
8
8
|
|
9
|
-
Mathf.Lerpで取得した値を毎回コールバッ
|
9
|
+
Mathf.Lerpで取得した値を毎回コールバックさせて、cubeのtransfrom.postion.xに反映させることを試みています。
|
10
10
|
|
11
11
|
```C#
|
12
12
|
using System.Collections;
|
@@ -50,4 +50,10 @@
|
|
50
50
|
|
51
51
|
```
|
52
52
|
error CS1002: ; expected
|
53
|
+
```
|
54
|
+
|
55
|
+
### 続いて発生しているエラーメッセージ
|
56
|
+
|
57
|
+
```
|
58
|
+
error CS0103: The name `x' does not exist in the current context
|
53
59
|
```
|
1
説明修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -35,7 +35,7 @@
|
|
35
35
|
yield return null;
|
36
36
|
}
|
37
37
|
|
38
|
-
//Mathf.Lerpに渡されるtが1未満でwhile文から抜ける為、調整。
|
38
|
+
//Mathf.Lerpに渡されるときのtが1未満でwhile文から抜ける為、調整。
|
39
39
|
x = end;
|
40
40
|
action(x);
|
41
41
|
}
|