提问人:Bawenang Rukmoko Pardian Putra 提问时间:8/15/2023 最后编辑:Bawenang Rukmoko Pardian Putra 更新时间:8/15/2023 访问量:29
订阅强制转换对象时不会触发事件
Event won't trigger when subscribing to a casted object
问:
我有这个代码:
public class StateController
{
public MainEventHandler EventHandler { get => eventHandler; }
private State currentState;
private MainEventHandler eventHandler = new();
private MainRepository repository = new();
private MainLogic logic = new();
public void ActivateState(State toActivate)
{
if (toActivate is StateA)
{
var properties = StateA.Properties();
properties.eventHandler = eventHandler;
properties.repository = repository;
toActivate.Setup(properties);
}
else if (toActivate is StateB)
{
var properties = StateB.Properties();
properties.eventHandler = eventHandler;
properties.logic = repository;
toActivate.Setup(properties);
}
}
public void Start()
{
if (eventHandler.onStart != null)
onStart();
}
public void DoLogic()
{
if (eventHandler.onCalculateLogic != null)
onCalculateLogic();
}
}
public class MainEventHandler
{
public delegate void OnStart();
public event OnStart onStart;
public delegate void OnCalculateLogic();
public event OnCalculateLogic onCalculateLogic;
}
public class StateA: State
{
public class Properties
{
public MainEventHandler eventHandler;
public MainRepository repository;
}
private Properties props;
public void Setup(object properties)
{
props = properties as Properties;
props.eventHandler.onStart += Start;
}
public void CleanUp()
{
props.eventHandler.onStart -= Start;
}
private void Start() //Doesn't get triggered because onStart is null
{
....
}
}
public class StateB: State
{
public class Properties
{
public MainEventHandler eventHandler;
public MainLogic repository;
}
private Properties props;
public void Setup(object properties)
{
props = properties as Properties;
props.eventHandler.onCalculateLogic += Calculate;
}
public void CleanUp()
{
props.eventHandler.onCalculateLogic -= Calculate;
}
private void Calculate() //Doesn't get triggered because onCalculateLogic is null
{
....
}
}
public MainSceneController: Monobehavior
{
private StateController stateController = new();
private StateA firstState = new();
private StateB secondState = new();
Awake()
{
stateController.ActivateState(firstState);
}
Start()
{
if(stateController.EventHandler.onStart != null) stateController.EventHandler.onStart(); // stateController.EventHandler.onStart is null apparently even though I have set it in ActivateState()
stateController.ActivateState(secondState);
}
Update()
{
if(stateController.EventHandler.onCalculateLogic != null) stateController.EventHandler.onCalculateLogic(); // stateController.EventHandler.onCalculateLogic is null apparently even though I have set it in ActivateState()
}
}
我想从中传递一个对象,该对象具有可以从状态内部订阅的事件。的参数是一个对象,因为我们可以有多种类型的属性。而属性是国家与其外部系统交互的唯一方式。但不幸的是,这些事件根本不会被触发,因为它们总是空的。虽然正如你所看到的,我已经添加了StateController
Setup()
`props.eventHandler.onStart += Start;`
和
`props.eventHandler.onCalculateLogic += Calculate;`
如何解决此问题?
谢谢。
答: 暂无答案
评论
onStart
event
MainEventHandler
if(stateController.EventHandler.onStart != null) stateController.EventHandler.onStart();