unity dots中,默认是存在一些world,这些world就像上帝一样掌控一切。每个world可以存在一些system。本文记录如何新建world和在自己创建的world中来新建system。
首先创建一个system:
//[DisableAutoCreation]
public class PrintSystem : ComponentSystem
{
protected override void OnUpdate()
{
Debug.Log("XXXXXXXxx");
}
}
创建world和在world中新增system:
World world = new World("aaa");
PrintSystem printsystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<PrintSystem>();
printsystem= world.AddSystem(printsystem);
在这两段代码中,要注意,第一个system创建的时候,不能有[DisableAutoCreation],第二段代码中,注意第二行,一定要先用World.DefaultGameObjectInjectionWorld.GetOrCreateSystem来创建,然后再通过world.AddSystem方法来添加,将结果返回为printsystem,否则都会添加不成功。