【发布时间】:2015-03-26 17:06:09
【问题描述】:
我正在尝试创建自己的状态机,但在使用泛型类型的类列表方面遇到了一些麻烦。我的代码如下。
State.cs:
using UnityEngine;
using System.Collections;
public abstract class State<T> where T:StateMachine
{
public T sm;
public State()
{
}
public virtual void OnEnter()
{
sm.currentState = sm.futureState;
}
public abstract void OnExit();
public abstract void OnLoop();
}
StateMachine.cs:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public abstract class StateMachine : MonoBehaviour
{
public List<State<T>> stateList = new List<T>>();
public int currentState = -1;
public int futureState;
protected virtual void Start()
{
foreach (State<T> s in stateList)
{
s.sm = this;
}
}
protected virtual void Update()
{
if (currentState != futureState)
{
stateList[futureState].OnEnter();
}
stateList[currentState].OnLoop();
if (currentState != futureState)
{
stateList[currentState].OnExit();
}
}
}
TestStateMachine.cs:
using UnityEngine;
using System.Collections;
public class TestStateMachine : StateMachine
{
public enum StateNames:int
{
State1,
State2,
};
public KeyCode kc;
// Use this for initialization
protected override void Start ()
{
stateList.Add(new TestStateMachineFirstState());
stateList.Add(new TestStateMachineSecondState());
base.Start();
}
}
public class TestStateMachineFirstState : State<StateMachine>
{
public override void OnEnter()
{
Debug.Log("SM1 OnEnter");
base.OnEnter();
}
public override void OnLoop()
{
Debug.Log("SM1 OnLoop");
if (Input.GetKeyDown(sm.kc))
{
sm.futureState = (int)TestStateMachine.StateNames.State2;
}
}
public override void OnExit()
{
Debug.Log("SM1 OnExit");
}
}
public class TestStateMachineSecondState : State<StateMachine>
{
public override void OnEnter()
{
Debug.Log("SM2 OnEnter");
base.OnEnter();
}
public override void OnLoop()
{
Debug.Log("SM2 OnLoop");
if (Input.GetKeyDown(sm.kc))
{
sm.futureState = (int)TestStateMachine.StateNames.State1;
}
}
public override void OnExit()
{
Debug.Log("SM2 OnExit");
}
}
我收到错误 CS0246: Type or namespace name T cannot be found(或类似的东西)。
如果我将所有State<T> 和State<TestStateMachine> 替换为State<StateMachine> 并将(Input.GetKeyDown(sm.kc)) 替换为(Input.GetKeyDown(KeyCode.A)),我的状态机“功能”。
但这并不理想,因为我无法从子状态机中获取变量。有没有办法保持这种结构(尽可能糟糕),还是我应该尝试另一种方法来做状态机?
【问题讨论】:
-
在哪里你得到错误?你能用更少的代码重现它吗?你能正确缩进你的代码吗?这将使阅读更容易......
-
我最好的猜测是您在将实例添加到 List
> 时遇到了一些问题,但请提供一些说明。 -
好吧,对于初学者来说,这是:
public List<State<T>> stateList = new List<T>>();
标签: c# list generics unity3d state-machine