質問編集履歴
3
TN8001さんの回答をもとに public AttackState() を記述しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -125,5 +125,70 @@
|
|
125
125
|
となります。
|
126
126
|
|
127
127
|
|
128
|
+
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
|
129
|
+
(追記)
|
130
|
+
TN8001さんの回答を元に記述したところ期待する結果が実装できました
|
131
|
+
|
132
|
+
```C#
|
133
|
+
public class Test : MonoBehaviour
|
134
|
+
{
|
135
|
+
public StateDataClass currentState;
|
136
|
+
public AttackState attackState;
|
137
|
+
|
138
|
+
private void Start()
|
139
|
+
{
|
140
|
+
attackState = new AttackState();
|
141
|
+
|
142
|
+
|
143
|
+
currentState = attackState;
|
144
|
+
currentState.Method(this);
|
145
|
+
|
146
|
+
if (currentState.piyo) { Debug.Log("現在のステートのpiyoがtrueの場合に処理する。piyoはステートによってtrueかflaseか差異がある(待機アニメはループ=trueだがジャンプアニメはfalse、など"); }
|
147
|
+
|
148
|
+
}
|
128
149
|
|
129
150
|
|
151
|
+
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
public abstract class StateDataClass
|
156
|
+
{
|
157
|
+
public string hoge = "抽象ステート";
|
158
|
+
public bool piyo = false;
|
159
|
+
|
160
|
+
public int Huga { get; set; } = 100;
|
161
|
+
|
162
|
+
public virtual void Method(Test test) { }
|
163
|
+
}
|
164
|
+
|
165
|
+
public class AttackState : StateDataClass
|
166
|
+
{
|
167
|
+
|
168
|
+
//public string hoge = "攻撃ステート";
|
169
|
+
//public bool piyo = true;
|
170
|
+
//public int Huga { get; set; } = 50;
|
171
|
+
|
172
|
+
public AttackState()
|
173
|
+
{
|
174
|
+
hoge = "攻撃ステート";
|
175
|
+
piyo = true;
|
176
|
+
}
|
177
|
+
|
178
|
+
|
179
|
+
public override void Method(Test test)
|
180
|
+
{
|
181
|
+
|
182
|
+
|
183
|
+
Debug.Log("hoge:" + hoge + " piyo:" + piyo);
|
184
|
+
Debug.Log("test.attackState.hoge:" + test.attackState.hoge+ " test.attackState.piyo:" + test.attackState.piyo);
|
185
|
+
Debug.Log("test.currentState.hoge:" + test.currentState.hoge + " test.currentState.piyo:" + test.currentState.piyo);
|
186
|
+
|
187
|
+
Debug.Log("huga:" + Huga);
|
188
|
+
Debug.Log("test.currentState.Huga:" + test.currentState.Huga);
|
189
|
+
|
190
|
+
}
|
191
|
+
}
|
192
|
+
```
|
193
|
+

|
194
|
+
|
2
画像の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -112,7 +112,7 @@
|
|
112
112
|
}
|
113
113
|
|
114
114
|
```
|
115
|
-
|
115
|
+

|
116
116
|
|
117
117
|
プロパティを試してみたのですが、上手く行きませんでした。
|
118
118
|
currentStateの中にあるAttackStateのhogeにアクセスしたいのですが、currentState自身のhogeにアクセスしているように見えます。
|
1
プロパティの実装を試してみたため追記しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -48,8 +48,82 @@
|
|
48
48
|
|
49
49
|
currentState経由で継承先の子クラスのメンバ変数にアクセスするにはどのようにすればよいでしょうか?
|
50
50
|
|
51
|
+
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
|
52
|
+
(追記)
|
53
|
+
|
54
|
+
```C#
|
55
|
+
using System.Collections;
|
56
|
+
using System.Collections.Generic;
|
57
|
+
using UnityEngine;
|
58
|
+
|
59
|
+
public class Test : MonoBehaviour
|
60
|
+
{
|
61
|
+
public StateDataClass currentState;
|
62
|
+
public AttackState attackState;
|
63
|
+
|
64
|
+
private void Start()
|
65
|
+
{
|
66
|
+
attackState = new AttackState();
|
67
|
+
attackState.Huga = 50;
|
68
|
+
|
69
|
+
|
70
|
+
currentState = attackState;
|
71
|
+
currentState.Method(this);
|
72
|
+
|
73
|
+
if (currentState.piyo) { Debug.Log("現在のステートのpiyoがtrueの場合に処理する。piyoはステートによってtrueかflaseか差異がある(待機アニメはループ=trueだがジャンプアニメはfalse、など"); }
|
74
|
+
|
75
|
+
}
|
51
76
|
|
52
77
|
|
53
78
|
|
54
79
|
|
80
|
+
}
|
55
81
|
|
82
|
+
public abstract class StateDataClass
|
83
|
+
{
|
84
|
+
public string hoge = "抽象ステート";
|
85
|
+
public bool piyo = false;
|
86
|
+
|
87
|
+
public int Huga { get; set; } = 100;
|
88
|
+
|
89
|
+
public virtual void Method(Test test) { }
|
90
|
+
}
|
91
|
+
|
92
|
+
public class AttackState : StateDataClass
|
93
|
+
{
|
94
|
+
|
95
|
+
public string hoge = "攻撃ステート";
|
96
|
+
public bool piyo = true;
|
97
|
+
public int Huga { get; set; } = 50;
|
98
|
+
|
99
|
+
|
100
|
+
public override void Method(Test test)
|
101
|
+
{
|
102
|
+
|
103
|
+
|
104
|
+
Debug.Log("hoge:" + hoge + " piyo:" + piyo);
|
105
|
+
Debug.Log("test.attackState.hoge:" + test.attackState.hoge+ " test.attackState.piyo:" + test.attackState.piyo);
|
106
|
+
Debug.Log("test.currentState.hoge:" + test.currentState.hoge + " test.currentState.piyo:" + test.currentState.piyo);
|
107
|
+
|
108
|
+
Debug.Log("huga:" + Huga);
|
109
|
+
Debug.Log("test.currentState.Huga:" + test.currentState.Huga);
|
110
|
+
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
```
|
115
|
+
|
116
|
+
|
117
|
+
プロパティを試してみたのですが、上手く行きませんでした。
|
118
|
+
currentStateの中にあるAttackStateのhogeにアクセスしたいのですが、currentState自身のhogeにアクセスしているように見えます。
|
119
|
+
実装の仕方が間違っているでしょうか?
|
120
|
+
実装したい運用としては、
|
121
|
+
・TestキャラクタのステートであるcurrentStateは常に変化する
|
122
|
+
・各継承先ステートのメンバ変数は各ステート毎に差異がある(待機ならアニメループ判定はtrueだが、ジャンプはfalse)
|
123
|
+
・currentState.Method()とするとcurrentStateに代入されている継承クラスのメソッドにアクセスできるように、currentStateの中にある継承先クラスのメンバにアクセスしたい
|
124
|
+
|
125
|
+
となります。
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|