我有SceneA(A)和SceneB(B)。 B也有一个由SceneContext安装程序安装的Foo AsSingle和NonLazy。 Foo对Debog.Log(“ Hello Errors!”)实现了IInitialize:
yield return LoadScene("SceneA");
yield return new WaitForSeconds(2.0f);
SceneManager.LoadScene("SceneB");
yield return new WaitForSeconds(2.0f);
var newSceneContainer = ProjectContext.Instance.Container.Resolve<SceneContextRegistry>()
.SceneContexts.Single().Container;
Foo foo = newSceneContainer.Resolve<Foo>();
我猜到“无法解析该类”,而在调试中我看到了Foo的问候消息,因此安装程序已完成其工作。现在,如果Foo是MonoBehaviour,则在Zeneject将游戏对象绑定为“来自场景”的组件的游戏对象上,那么毫无问题,它将得到解决。
在这种情况下,请让我知道如何解决。
**-编辑-1 **
Foo重命名为“ BClass”。
A的安装程序为空。
B的代码:
public class BInstaller : MonoInstaller
{
public override void InstallBindings()
{
Container.BindInterfacesAndSelfTo<BClass>().AsSingle().NonLazy();
}
}
public class BClass : IInitializable, IDisposable {
public void Initialize() { Debug.Log("Hello Errors!"); }
public void Dispose() { }
}
最后的测试:
public class ScenesFlow_A_to_B : SceneTestFixture
{
[UnityTest]
public IEnumerator TestScene()
{
yield return LoadScene("SceneA");
//waiting
SceneManager.LoadScene("SceneB");
//waiting
var newSceneContainer = ProjectContext.Instance.Container
.Resolve<SceneContextRegistry>().SceneContexts.Single().Container;
BClass bc = newSceneContainer.Resolve<BClass>() as BClass;
Assert.IsNotNull(bc);// fail!
}
}
-编辑2-
新变化:
public class ScenesFlow_A_to_B : SceneTestFixture
{
[UnityTest]
public IEnumerator TestScene()
{
yield return LoadScene("SceneA");
//SceneManager.LoadScene("SceneB");
yield return SceneManager.LoadSceneAsync("SceneB");
var newSceneContainer = ProjectContext.Instance.Container
.Resolve<SceneContextRegistry>().SceneContexts.Single().Container;
BClass bc = newSceneContainer.Resolve<BClass>() as BClass;
Assert.IsNotNull(bc);
}
}
结果将是这样:
TestScene (0.323s)
---
System.InvalidOperationException : Operation is not valid due to the current state of the object
---
at System.Linq.Enumerable.Single[SceneContext] (IEnumerable`1 source) [0x00000] in <filename unknown>:0
at ScenesFlow_A_to_B+<TestScene>c__Iterator0.MoveNext () [0x00089] in ... at UnityEngine.TestTools.TestEnumerator+<Execute>c__Iterator0.MoveNext () [0x00031] in .../TestEnumerator.cs:29
---
Loading scene 'SceneA' for testing
I am class B