C#
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Test : MonoBehaviour { public StateDataClass currentState; public AttackState attackState; private void Start() { attackState = new AttackState(); currentState = attackState; currentState.Method(this); } } public abstract class StateDataClass { public string hoge = "抽象ステート"; public bool piyo = false; public virtual void Method(Test test) { } } public class AttackState : StateDataClass { public string hoge = "攻撃ステート"; public bool piyo = true; public override void Method(Test test) { Debug.Log("hoge:" + hoge + " piyo:" + piyo); Debug.Log("test.attackState.hoge:" + test.attackState.hoge+ " test.attackState.piyo:" + test.attackState.piyo); Debug.Log("test.currentState.hoge:" + test.currentState.hoge + " test.currentState.piyo:" + test.currentState.piyo); } }
キャラクターのステートをクラスで管理するために、まず抽象クラスを作り、overrideしたメソッドと変数を持たせた継承先の子クラスAttackStateをTestキャラクター内でインスタンスしました。
TestキャラクターはcurrentStateという抽象クラス型の変数を持ち、この中にインスタンスした待機、攻撃、移動、といった子クラスを入れて状態遷移を行います。
しかしcurrentState経由でインスタンスした子クラスの変数にアクセスしたところ以下のように抽象クラスの変数が返ってきてしまいうまくいきません。
currentState経由で継承先の子クラスのメンバ変数にアクセスするにはどのようにすればよいでしょうか?
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
(追記)
C#
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Test : MonoBehaviour { public StateDataClass currentState; public AttackState attackState; private void Start() { attackState = new AttackState(); attackState.Huga = 50; currentState = attackState; currentState.Method(this); if (currentState.piyo) { Debug.Log("現在のステートのpiyoがtrueの場合に処理する。piyoはステートによってtrueかflaseか差異がある(待機アニメはループ=trueだがジャンプアニメはfalse、など"); } } } public abstract class StateDataClass { public string hoge = "抽象ステート"; public bool piyo = false; public int Huga { get; set; } = 100; public virtual void Method(Test test) { } } public class AttackState : StateDataClass { public string hoge = "攻撃ステート"; public bool piyo = true; public int Huga { get; set; } = 50; public override void Method(Test test) { Debug.Log("hoge:" + hoge + " piyo:" + piyo); Debug.Log("test.attackState.hoge:" + test.attackState.hoge+ " test.attackState.piyo:" + test.attackState.piyo); Debug.Log("test.currentState.hoge:" + test.currentState.hoge + " test.currentState.piyo:" + test.currentState.piyo); Debug.Log("huga:" + Huga); Debug.Log("test.currentState.Huga:" + test.currentState.Huga); } }
プロパティを試してみたのですが、上手く行きませんでした。
currentStateの中にあるAttackStateのhogeにアクセスしたいのですが、currentState自身のhogeにアクセスしているように見えます。
実装の仕方が間違っているでしょうか?
実装したい運用としては、
・TestキャラクタのステートであるcurrentStateは常に変化する
・各継承先ステートのメンバ変数は各ステート毎に差異がある(待機ならアニメループ判定はtrueだが、ジャンプはfalse)
・currentState.Method()とするとcurrentStateに代入されている継承クラスのメソッドにアクセスできるように、currentStateの中にある継承先クラスのメンバにアクセスしたい
となります。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
(追記)
TN8001さんの回答を元に記述したところ期待する結果が実装できました
C#
public class Test : MonoBehaviour { public StateDataClass currentState; public AttackState attackState; private void Start() { attackState = new AttackState(); currentState = attackState; currentState.Method(this); if (currentState.piyo) { Debug.Log("現在のステートのpiyoがtrueの場合に処理する。piyoはステートによってtrueかflaseか差異がある(待機アニメはループ=trueだがジャンプアニメはfalse、など"); } } } public abstract class StateDataClass { public string hoge = "抽象ステート"; public bool piyo = false; public int Huga { get; set; } = 100; public virtual void Method(Test test) { } } public class AttackState : StateDataClass { //public string hoge = "攻撃ステート"; //public bool piyo = true; //public int Huga { get; set; } = 50; public AttackState() { hoge = "攻撃ステート"; piyo = true; } public override void Method(Test test) { Debug.Log("hoge:" + hoge + " piyo:" + piyo); Debug.Log("test.attackState.hoge:" + test.attackState.hoge+ " test.attackState.piyo:" + test.attackState.piyo); Debug.Log("test.currentState.hoge:" + test.currentState.hoge + " test.currentState.piyo:" + test.currentState.piyo); Debug.Log("huga:" + Huga); Debug.Log("test.currentState.Huga:" + test.currentState.Huga); } }
まだ回答がついていません
会員登録して回答してみよう