C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Test : MonoBehaviour 6{ 7 public StateDataClass currentState; 8 public AttackState attackState; 9 10 private void Start() 11 { 12 attackState = new AttackState(); 13 currentState = attackState; 14 currentState.Method(this); 15 } 16} 17 18public abstract class StateDataClass 19{ 20 public string hoge = "抽象ステート"; 21 public bool piyo = false; 22 public virtual void Method(Test test) { } 23} 24 25public class AttackState : StateDataClass 26{ 27 public string hoge = "攻撃ステート"; 28 public bool piyo = true; 29 30 public override void Method(Test test) 31 { 32 Debug.Log("hoge:" + hoge + " piyo:" + piyo); 33 Debug.Log("test.attackState.hoge:" + test.attackState.hoge+ " test.attackState.piyo:" + test.attackState.piyo); 34 Debug.Log("test.currentState.hoge:" + test.currentState.hoge + " test.currentState.piyo:" + test.currentState.piyo); 35 } 36} 37 38
キャラクターのステートをクラスで管理するために、まず抽象クラスを作り、overrideしたメソッドと変数を持たせた継承先の子クラスAttackStateをTestキャラクター内でインスタンスしました。
TestキャラクターはcurrentStateという抽象クラス型の変数を持ち、この中にインスタンスした待機、攻撃、移動、といった子クラスを入れて状態遷移を行います。
しかしcurrentState経由でインスタンスした子クラスの変数にアクセスしたところ以下のように抽象クラスの変数が返ってきてしまいうまくいきません。
currentState経由で継承先の子クラスのメンバ変数にアクセスするにはどのようにすればよいでしょうか?
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
(追記)
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Test : MonoBehaviour 6{ 7 public StateDataClass currentState; 8 public AttackState attackState; 9 10 private void Start() 11 { 12 attackState = new AttackState(); 13 attackState.Huga = 50; 14 15 16 currentState = attackState; 17 currentState.Method(this); 18 19 if (currentState.piyo) { Debug.Log("現在のステートのpiyoがtrueの場合に処理する。piyoはステートによってtrueかflaseか差異がある(待機アニメはループ=trueだがジャンプアニメはfalse、など"); } 20 21 } 22 23 24 25 26} 27 28public abstract class StateDataClass 29{ 30 public string hoge = "抽象ステート"; 31 public bool piyo = false; 32 33 public int Huga { get; set; } = 100; 34 35 public virtual void Method(Test test) { } 36} 37 38public class AttackState : StateDataClass 39{ 40 41 public string hoge = "攻撃ステート"; 42 public bool piyo = true; 43 public int Huga { get; set; } = 50; 44 45 46 public override void Method(Test test) 47 { 48 49 50 Debug.Log("hoge:" + hoge + " piyo:" + piyo); 51 Debug.Log("test.attackState.hoge:" + test.attackState.hoge+ " test.attackState.piyo:" + test.attackState.piyo); 52 Debug.Log("test.currentState.hoge:" + test.currentState.hoge + " test.currentState.piyo:" + test.currentState.piyo); 53 54 Debug.Log("huga:" + Huga); 55 Debug.Log("test.currentState.Huga:" + test.currentState.Huga); 56 57 } 58} 59
プロパティを試してみたのですが、上手く行きませんでした。
currentStateの中にあるAttackStateのhogeにアクセスしたいのですが、currentState自身のhogeにアクセスしているように見えます。
実装の仕方が間違っているでしょうか?
実装したい運用としては、
・TestキャラクタのステートであるcurrentStateは常に変化する
・各継承先ステートのメンバ変数は各ステート毎に差異がある(待機ならアニメループ判定はtrueだが、ジャンプはfalse)
・currentState.Method()とするとcurrentStateに代入されている継承クラスのメソッドにアクセスできるように、currentStateの中にある継承先クラスのメンバにアクセスしたい
となります。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
(追記)
TN8001さんの回答を元に記述したところ期待する結果が実装できました
C#
1public class Test : MonoBehaviour 2{ 3 public StateDataClass currentState; 4 public AttackState attackState; 5 6 private void Start() 7 { 8 attackState = new AttackState(); 9 10 11 currentState = attackState; 12 currentState.Method(this); 13 14 if (currentState.piyo) { Debug.Log("現在のステートのpiyoがtrueの場合に処理する。piyoはステートによってtrueかflaseか差異がある(待機アニメはループ=trueだがジャンプアニメはfalse、など"); } 15 16 } 17 18 19 20 21} 22 23public abstract class StateDataClass 24{ 25 public string hoge = "抽象ステート"; 26 public bool piyo = false; 27 28 public int Huga { get; set; } = 100; 29 30 public virtual void Method(Test test) { } 31} 32 33public class AttackState : StateDataClass 34{ 35 36 //public string hoge = "攻撃ステート"; 37 //public bool piyo = true; 38 //public int Huga { get; set; } = 50; 39 40 public AttackState() 41 { 42 hoge = "攻撃ステート"; 43 piyo = true; 44 } 45 46 47 public override void Method(Test test) 48 { 49 50 51 Debug.Log("hoge:" + hoge + " piyo:" + piyo); 52 Debug.Log("test.attackState.hoge:" + test.attackState.hoge+ " test.attackState.piyo:" + test.attackState.piyo); 53 Debug.Log("test.currentState.hoge:" + test.currentState.hoge + " test.currentState.piyo:" + test.currentState.piyo); 54 55 Debug.Log("huga:" + Huga); 56 Debug.Log("test.currentState.Huga:" + test.currentState.Huga); 57 58 } 59}
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/06/12 10:52
2022/06/12 11:40
2022/06/12 12:53
2022/06/12 14:28
2022/06/12 15:41 編集