我正在尝试在运行时在我的状态机的某个特定时刻加载预制件。但是我在运行时得到了这个异常。使用Ctrl Left + Maj Left + V快捷键验证所有场景。
我的状态机绑定到项目上下文:
using Zenject;
namespace AVF.StateMachine
{
/**
* @class AVFStateMachineInstaller
* Class used to represent the installer of the state machine
*/
public class AVFStateMachineInstaller : MonoInstaller<AVFStateMachineInstaller>
{
/**
* Install the bindings between the objects
*/
public override void InstallBindings()
{
// Global factory
Container.Bind<AVFStateFactory>().AsSingle();
// States factories
Container.BindFactory<AVFInitState, AVFInitState.Factory>().WhenInjectedInto<AVFStateFactory>();
Container.BindFactory<AVFLoginState, AVFLoginState.Factory>().WhenInjectedInto<AVFStateFactory>();
Container.BindFactory<AVFSelectJobState, AVFSelectJobState.Factory>().WhenInjectedInto<AVFStateFactory>();
}
}
}
使用AVFFactory类中的函数CreateState创建状态。在我想要的时候,没有创建状态和在它们之间导航的问题。除非我尝试加载包含游戏对象上下文的预制件。所以,我知道状态机的所有管理都只存在于项目上下文中。
我在场景环境的安装程序中放置了我的预制件工厂的绑定:
using UnityEngine;
using Zenject;
using AVF.StateMachine;
public class AVFSceneHubInstaller : MonoInstaller<AVFSceneHubInstaller>
{
[SerializeField]
GameObject mPrefabWeldingPedestal;
[SerializeField]
GameObject mPrefabSandboxPedestal;
public override void InstallBindings()
{
Container.BindFactory<AVFSandboxPedestal, AVFSandboxPedestal.Factory>().FromSubContainerResolve().ByNewPrefab(mPrefabSandboxPedestal).WhenInjectedInto<AVFSelectJobState>();
Container.BindFactory<AVFWeldingPedestal, AVFWeldingPedestal.Factory>().FromSubContainerResolve().ByNewPrefab(mPrefabWeldingPedestal).WhenInjectedInto<AVFSelectJobState>();
}
}
我的预制件包含一个带安装程序的游戏对象上下文:
using UnityEngine;
using Zenject;
public class AVFSandboxPedestalInstaller : MonoInstaller
{
[SerializeField]
private GameObject mPrefab;
[SerializeField]
private GameObject mAttachObject;
public override void InstallBindings()
{
Container.BindInterfacesAndSelfTo<AVFSandboxPedestal>()
.FromComponentInNewPrefab(mPrefab)
.UnderTransform(mAttachObject.transform)
.AsSingle();
}
}
用于在工厂中使用该对象的组件:
using UnityEngine;
using VRTK;
using Zenject;
public class AVFSandboxPedestal : VRTK_InteractableObject
{
public void Start()
{
Debug.LogWarning("HELLO SANDBOX");
}
public override void OnInteractableObjectUsed(InteractableObjectEventArgs e)
{
base.OnInteractableObjectUsed(e);
Debug.LogWarning(gameObject.name + "USED");
}
public class Factory : Factory<AVFSandboxPedestal>
{
}
}
我需要工厂实例的状态是这样的,当我想创建这个对象时,我到达异常,因为它无法解析类型AVFSandboxPedestal.Factory:
using UnityEngine;
using Zenject;
using Mimbus.StateMachine;
using AVF.Context;
namespace AVF.StateMachine
{
/**
* @class AVFSelectJobState
* Class used to represent the select job state of the application
*/
public class AVFSelectJobState : MbStateEntity
{
// Class fields
private readonly AVFStateManager mStateManager; /**< The state manager */
private readonly AVFContext mContext; /**< Context of the application */
private readonly AVFHeadState mHeadState; /**< Common state of all states and scenes */
private readonly AVFSandboxPedestal.Factory mSandboxPedestalFactory; /**< */
/**
* Constructor
* @param[in] pStateManager The state manager of the application
* @param[in] pContext The context of the application
*/
public AVFSelectJobState(AVFStateManager pStateManager, AVFContext pContext, AVFHeadState pHeadState,
AVFSandboxPedestal.Factory pSandboxPedestalFactory)
{
this.mStateManager = pStateManager;
this.mContext = pContext;
this.mHeadState = pHeadState;
this.mSandboxPedestalFactory = pSandboxPedestalFactory;
}
/**
* Initialize this object
*/
public override void Initialize()
{
Debug.Log("Select job state start");
this.mSandboxPedestalFactory.Create();
}
// Other lines of code .....
我认为问题是:我的状态机和状态进入项目上下文,我的预制件在他自己的上下文中。所以,显然,它无法奏效。 (NO 3)
所以我尝试在我的预制件上添加一个Zenject Binding组件,我在其中添加AVFSandboxPedestal并在其中添加项目上下文。 但它不起作用。
我尝试在类AVFSandboxPedestal之外创建一个工厂,并使用组件Zenject Binding将其绑定到项目上下文。 (但是我在场景中的场景上下文中安装了一个绑定。也许它不会引发错误,但它是一个),将类AVFSandboxPedestal绑定到场景上下文。 但似乎没有任何效果。我没有解决这个问题的方法,一遍又一遍地阅读文档并没有帮助我。
注意:我不希望实际预制件在实际场景中的所有场景中都存在。但状态机必须坚持所有场景。
注意:如果你需要更多精确度。请问。 ;)
谢谢! :)