public void PutsSystemInDefaultWorld() { var old = ClientServerBootstrap.SystemStates; ClientServerBootstrap.SystemStates = default; var system = new List <Type> { typeof(ClientInitializationSystemGroup), typeof(ClientSimulationSystemGroup), typeof(ClientPresentationSystemGroup), typeof(ServerInitializationSystemGroup), typeof(ServerSimulationSystemGroup) }; ClientServerBootstrap.GenerateSystemList(system); Assert.True( ClientServerBootstrap.SystemStates.ClientSystems.Contains(typeof(ClientInitializationSystemGroup))); Assert.True(ClientServerBootstrap.SystemStates.ClientSystems.Contains(typeof(ClientSimulationSystemGroup))); Assert.True( ClientServerBootstrap.SystemStates.ClientSystems.Contains(typeof(ClientPresentationSystemGroup))); Assert.True( ClientServerBootstrap.SystemStates.ServerSystems.Contains(typeof(ServerInitializationSystemGroup))); Assert.True(ClientServerBootstrap.SystemStates.ServerSystems.Contains(typeof(ServerSimulationSystemGroup))); ClientServerBootstrap.SystemStates = old; }
public void CreateWorlds(bool server, int numClients) { if (!m_DefaultWorldInitialized) { DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(m_DefaultWorld, ClientServerBootstrap.ExplicitDefaultWorldSystems); m_DefaultWorldInitialized = true; } if (server) { if (m_ServerWorld != null) { throw new InvalidOperationException("Server world already created"); } m_ServerWorld = ClientServerBootstrap.CreateServerWorld(m_DefaultWorld, "ServerTest"); } if (numClients > 0) { if (m_ClientWorlds != null) { throw new InvalidOperationException("Client worlds already created"); } m_ClientWorlds = new World[numClients]; for (int i = 0; i < numClients; ++i) { m_ClientWorlds[i] = ClientServerBootstrap.CreateClientWorld(m_DefaultWorld, $"ClientTest{i}"); } } }
public void BootstrapPutsTickSystemInDefaultWorld() { var oldBootstrapState = ClientServerBootstrap.s_State; ClientServerBootstrap.s_State = default; var systems = new List <Type>(); systems.Add(typeof(TickClientInitializationSystem)); systems.Add(typeof(TickClientSimulationSystem)); systems.Add(typeof(TickClientPresentationSystem)); systems.Add(typeof(TickServerInitializationSystem)); systems.Add(typeof(TickServerSimulationSystem)); ClientServerBootstrap.GenerateSystemLists(systems); Assert.True(ClientServerBootstrap.DefaultWorldSystems.Contains(typeof(TickClientInitializationSystem))); Assert.True(ClientServerBootstrap.ExplicitDefaultWorldSystems.Contains(typeof(TickClientInitializationSystem))); Assert.True(ClientServerBootstrap.DefaultWorldSystems.Contains(typeof(TickClientSimulationSystem))); Assert.True(ClientServerBootstrap.ExplicitDefaultWorldSystems.Contains(typeof(TickClientSimulationSystem))); Assert.True(ClientServerBootstrap.DefaultWorldSystems.Contains(typeof(TickClientPresentationSystem))); Assert.True(ClientServerBootstrap.ExplicitDefaultWorldSystems.Contains(typeof(TickClientPresentationSystem))); Assert.True(ClientServerBootstrap.DefaultWorldSystems.Contains(typeof(TickServerInitializationSystem))); Assert.True(ClientServerBootstrap.ExplicitDefaultWorldSystems.Contains(typeof(TickServerInitializationSystem))); Assert.True(ClientServerBootstrap.DefaultWorldSystems.Contains(typeof(TickServerSimulationSystem))); Assert.True(ClientServerBootstrap.ExplicitDefaultWorldSystems.Contains(typeof(TickServerSimulationSystem))); ClientServerBootstrap.s_State = oldBootstrapState; }
public void FindTargetWorldRootTest() { // 客户端与服务端World顶级组 Type[] clientRoots = { typeof(ClientInitializationSystemGroup), typeof(ClientSimulationSystemGroup), typeof(ClientPresentationSystemGroup), }; Type[] serverRoots = { typeof(ServerInitializationSystemGroup), typeof(ServerSimulationSystemGroup) }; foreach (var t in clientRoots) { var targetWorld = ClientServerBootstrap.FindTargetWorld(t, out var explicitWorld); Assert.IsTrue(targetWorld == TargetWorld.Client && explicitWorld); } foreach (var t in serverRoots) { var targetWorld = ClientServerBootstrap.FindTargetWorld(t, out var explicitWorld); Assert.IsTrue(targetWorld == TargetWorld.Server && explicitWorld); } }
public void Bootstrap(bool includeNetCodeSystems, params Type[] userSystems) { var systems = new List <Type>(); if (includeNetCodeSystems) { if (s_NetCodeSystems == null) { s_NetCodeSystems = new List <Type>(); var sysList = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default); foreach (var sys in sysList) { if (sys.Assembly.FullName.StartsWith("Unity.NetCode,") || sys.Assembly.FullName.StartsWith("Unity.Entities,") || sys.Assembly.FullName.StartsWith("Unity.Transforms,")) { s_NetCodeSystems.Add(sys); } } } systems.AddRange(s_NetCodeSystems); } systems.AddRange(userSystems); ClientServerBootstrap.GenerateSystemLists(systems); }
public List <World> CreateZoneWorlds(World defaultWorld, string zonePrefix) { var worlds = new List <World>(); for (int i = 0; i < 16; i++) { worlds.Add(ClientServerBootstrap.CreateServerWorld(defaultWorld, $"{zonePrefix}_{i}")); } return(worlds); }
public override void Setup() { base.Setup(); // Initialize server world first this.serverWorld = ClientServerBootstrap.CreateServerWorld(base.World, "ServerWorld"); this.serverWorld.EntityManager.CompleteAllJobs(); // Then initialize client world this.clientWorld = ClientServerBootstrap.CreateClientWorld(base.World, "ClientWorld"); this.clientWorld.EntityManager.CompleteAllJobs(); }
public void CreateWorlds(bool server) { var oldConstructor = NetworkStreamReceiveSystem.s_DriverConstructor; NetworkStreamReceiveSystem.s_DriverConstructor = this; if (server) { ServerWorld = ClientServerBootstrap.CreateServerWorld(DefaultWorld, "ServerTest"); } ClientWorld = ClientServerBootstrap.CreateClientWorld(DefaultWorld, "ClientTest"); NetworkStreamReceiveSystem.s_DriverConstructor = oldConstructor; }
public void FindTargetWorldChainTest() { Type[] chains = { typeof(ChainClientSimulationSystem), typeof(ChainServerSimulationSystem), }; foreach (var t in chains) { var targetWorld = ClientServerBootstrap.FindTargetWorld(t, out var explicitWorld); Assert.IsTrue(targetWorld == TargetWorld.Default && explicitWorld); } }
public void CreateWorlds(bool server, int numClients) { var oldConstructor = NetworkStreamReceiveSystem.s_DriverConstructor; NetworkStreamReceiveSystem.s_DriverConstructor = this; if (!m_DefaultWorldInitialized) { DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(m_DefaultWorld, ClientServerBootstrap.ExplicitDefaultWorldSystems); m_DefaultWorldInitialized = true; } if (server) { if (m_ServerWorld != null) { throw new InvalidOperationException("Server world already created"); } m_ServerWorld = ClientServerBootstrap.CreateServerWorld(m_DefaultWorld, "ServerTest"); #if UNITY_EDITOR ConvertGhostCollection(m_ServerWorld); #endif } if (numClients > 0) { if (m_ClientWorlds != null) { throw new InvalidOperationException("Client worlds already created"); } m_ClientWorlds = new World[numClients]; for (int i = 0; i < numClients; ++i) { try { m_ClientWorlds[i] = ClientServerBootstrap.CreateClientWorld(m_DefaultWorld, $"ClientTest{i}"); } catch (Exception) { m_ClientWorlds = null; throw; } #if UNITY_EDITOR ConvertGhostCollection(m_ClientWorlds[i]); #endif } } NetworkStreamReceiveSystem.s_DriverConstructor = oldConstructor; }
public void BootstrapDoesNotPutNetworkTimeSystemInDefaultWorld() { var oldBootstrapState = ClientServerBootstrap.s_State; ClientServerBootstrap.s_State = default; var systems = new List <Type>(); systems.Add(typeof(NetworkTimeSystem)); ClientServerBootstrap.GenerateSystemLists(systems); Assert.False(ClientServerBootstrap.DefaultWorldSystems.Contains(typeof(NetworkTimeSystem))); Assert.False(ClientServerBootstrap.ExplicitDefaultWorldSystems.Contains(typeof(NetworkTimeSystem))); ClientServerBootstrap.s_State = oldBootstrapState; }
public void CreateWorlds(bool server, int numClients) { var oldConstructor = NetworkStreamReceiveSystem.s_DriverConstructor; NetworkStreamReceiveSystem.s_DriverConstructor = this; if (!m_DefaultWorldInitialized) { DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(m_DefaultWorld, ClientServerBootstrap.ExplicitDefaultWorldSystems); m_DefaultWorldInitialized = true; } if (server) { if (m_ServerWorld != null) { throw new InvalidOperationException("Server world already created"); } m_ServerWorld = ClientServerBootstrap.CreateServerWorld(m_DefaultWorld, "ServerTest"); #if UNITY_EDITOR if (m_GhostCollection != null) { GameObjectConversionUtility.ConvertGameObjectHierarchy(m_GhostCollection, GameObjectConversionSettings.FromWorld(m_ServerWorld, m_BlobAssetStore)); } #endif } if (numClients > 0) { if (m_ClientWorlds != null) { throw new InvalidOperationException("Client worlds already created"); } m_ClientWorlds = new World[numClients]; for (int i = 0; i < numClients; ++i) { m_ClientWorlds[i] = ClientServerBootstrap.CreateClientWorld(m_DefaultWorld, $"ClientTest{i}"); #if UNITY_EDITOR if (m_GhostCollection != null) { GameObjectConversionUtility.ConvertGameObjectHierarchy(m_GhostCollection, GameObjectConversionSettings.FromWorld(m_ClientWorlds[i], m_BlobAssetStore)); } #endif } } NetworkStreamReceiveSystem.s_DriverConstructor = oldConstructor; }
public void Bootstrap(bool includeNetCodeSystems, params Type[] userSystems) { var systems = new List <Type>(); if (includeNetCodeSystems) { if (s_NetCodeSystems == null) { s_NetCodeSystems = new List <Type>(); var sysList = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default); foreach (var sys in sysList) { if (sys.Assembly.FullName.StartsWith("Unity.NetCode,") || sys.Assembly.FullName.StartsWith("Unity.Entities,") || sys.Assembly.FullName.StartsWith("Unity.Transforms,") || sys.Assembly.FullName.StartsWith("Unity.NetCode.Generated,") || sys.Assembly.FullName.StartsWith("Unity.Entities.Generated,") || sys.Assembly.FullName.StartsWith("Unity.Transforms.Generated,") || (sys.Assembly.FullName.StartsWith("Unity.NetCode.") && sys.Assembly.FullName.Contains(".Generated,"))) { s_NetCodeSystems.Add(sys); } } } systems.AddRange(s_NetCodeSystems); } if (NetCodeAssemblies.Count > 0) { var sysList = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default); foreach (var sys in sysList) { bool shouldAdd = false; var sysName = sys.Assembly.FullName; foreach (var asm in NetCodeAssemblies) { shouldAdd |= sysName.StartsWith(asm); } if (shouldAdd) { systems.Add(sys); } } } systems.AddRange(userSystems); ClientServerBootstrap.GenerateSystemLists(systems); }
public void Bootstrap(bool includeNetCodeSystems, params Type[] useSystems) { var systems = new List <Type>(); if (includeNetCodeSystems) { var sysList = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default); foreach (Type sys in sysList) { if (sys.Assembly.FullName.StartsWith("MyGameLib.NetCode,") || sys.Assembly.FullName.StartsWith("Unity.Entities,") || sys.Assembly.FullName.StartsWith("Unity.Transforms,")) { systems.Add(sys); } } } if (NetCodeAssemblies.Count > 0) { var sysList = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default); foreach (var sys in sysList) { bool shouldAdd = false; var sysName = sys.Assembly.FullName; foreach (var asm in NetCodeAssemblies) { shouldAdd |= sysName.StartsWith(asm); } if (shouldAdd) { systems.Add(sys); } } } systems.AddRange(useSystems); ClientServerBootstrap.GenerateSystemList(systems); DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(DefaultWorld, ClientServerBootstrap.ExplicitDefaultWorldSystems); }
private void Start() { Application.targetFrameRate = 60; World.DefaultGameObjectInjectionWorld.GetExistingSystem <GoInitSystem>().actions.Enqueue(() => { #if UNITY_CLIENT // 创建客户端世界 var clientWorld = ClientServerBootstrap.CreateClientWorld(World.DefaultGameObjectInjectionWorld, "ClientWorld"); #elif UNITY_SERVER // 创建服务端世界 var serverWorld = ClientServerBootstrap.CreateServerWorld(World.DefaultGameObjectInjectionWorld, "ServerWorld"); #elif UNITY_EDITOR // 创建服务端世界 var serverWorld = ClientServerBootstrap.CreateServerWorld(World.DefaultGameObjectInjectionWorld, "ServerWorld"); // 创建客户端世界 var clientWorld = ClientServerBootstrap.CreateClientWorld(World.DefaultGameObjectInjectionWorld, "ClientWorld"); #endif }); World.DefaultGameObjectInjectionWorld.GetExistingSystem <GoInitSystem>().actions.Enqueue(() => { ShareData.SetActive(true); foreach (var world in World.All) { var network = world.GetExistingSystem <NetworkStreamReceiveSystem>(); if (world.GetExistingSystem <ClientSimulationSystemGroup>() != null) { // 客户端连接服务器端口 NetworkEndPoint ep = NetworkEndPoint.LoopbackIpv4; ep.Port = 7979; network.Connect(ep); } else if (world.GetExistingSystem <ServerSimulationSystemGroup>() != null) { // 服务器自动监听端口 NetworkEndPoint ep = NetworkEndPoint.AnyIpv4; ep.Port = 7979; network.Listen(ep); } } }); }
/*private bool initialized = false; * * private float timeToInitialize = 2f;*/ private void Start() { #if UNITY_EDITOR if (GameSession.clientSession == null) { GameSession.serverSession = new ServerSession { hostName = "UnityEditor", numberOfPlayers = 1, laps = 2, serverPort = 7979 }; GameSession.clientSession = new ClientSession { remoteServerIpAddress = "127.0.0.1", remoteServerPort = 7979 }; } #endif #if !UNITY_SERVER World.DefaultGameObjectInjectionWorld.GetExistingSystem <TestInitSystem>().actions.Enqueue(() => { var clientWorld = ClientServerBootstrap.CreateClientWorld(World.DefaultGameObjectInjectionWorld, "ClientWorld"); if (GameSession.serverSession != null) { var serverWorld = ClientServerBootstrap.CreateServerWorld(World.DefaultGameObjectInjectionWorld, "ServerWorld"); GameObject.Instantiate(networkListenerPrefab); } allGameObjects.SetActive(true); }); #else World.DefaultGameObjectInjectionWorld.GetExistingSystem <TestInitSystem>().actions.Enqueue(() => { var serverWorld = ClientServerBootstrap.CreateServerWorld(World.DefaultGameObjectInjectionWorld, "ServerWorld"); allGameObjects.SetActive(true); }); #endif }
public void ServerLauncher(string gameName) { //Here we create the launch GameObject and load it with necessary data GameObject serverObject = Instantiate(ServerLauncherObject); DontDestroyOnLoad(serverObject); //This sets up the server object with all its necessary data serverObject.GetComponent <ServerLaunchObjectData>().GameName = gameName; serverObject.GetComponent <ServerLaunchObjectData>().BroadcastIpAddress = m_BroadcastIpAddress; serverObject.GetComponent <ServerLaunchObjectData>().BroadcastPort = m_BroadcastPort; //CreateServerWorld is a method provided by ClientServerBootstrap for precisely this reason //Manual creation of worlds //We must grab the DefaultGameObjectInjectionWorld first as it is needed to create our ServerWorld var world = World.DefaultGameObjectInjectionWorld; #if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR ClientServerBootstrap.CreateServerWorld(world, "ServerWorld"); #endif }
private void Awake() { Debug.Log("To Awake!"); #if UNITY_CLIENT var clientWorld = ClientServerBootstrap.CreateClientWorld(World.DefaultGameObjectInjectionWorld, "MyClientWorld"); // clientWorld.EntityManager.CreateEntity(typeof(LeoGameStatus), typeof(LeoPlayerGameStatus)); // 创建一个全局可访问的实体 #endif #if UNITY_SERVER ClientServerBootstrap.CreateServerWorld(World.DefaultGameObjectInjectionWorld, "MyServerWorld"); #endif #if UNITY_EDITOR var theServerWorld = ClientServerBootstrap.CreateServerWorld(World.DefaultGameObjectInjectionWorld, "MyServerWorld"); //需要生成对应数量的 client int numClientWorlds = ClientServerBootstrap.RequestedNumClients; // 客户端 int totalNumClients = numClientWorlds; //int numThinClients = ClientServerBootstrap.RequestedNumThinClients; // 轻量级客户端 //totalNumClients += numThinClients; for (int i = 0; i < numClientWorlds; ++i) { var editclientWorld = ClientServerBootstrap.CreateClientWorld(World.DefaultGameObjectInjectionWorld, "MyClientWorld" + i); // clientWorld.EntityManager.CreateEntity(typeof(LeoGameStatus), typeof(LeoPlayerGameStatus)); // 创建一个全局可访问的实体 } for (int i = numClientWorlds; i < totalNumClients; ++i) { var editnclientWorld = ClientServerBootstrap.CreateClientWorld(World.DefaultGameObjectInjectionWorld, "MyClientWorld" + i); editnclientWorld.EntityManager.CreateEntity(typeof(ThinClientComponent)); // clientWorld.EntityManager.CreateEntity(typeof(LeoGameStatus), typeof(LeoPlayerGameStatus)); // 创建一个全局可访问的实体 } #endif }
public void ClientLauncher(string playerName, string ipAddress) { //Here we create the launch GameObject and load it with necessary data GameObject clientObject = Instantiate(ClientLauncherObject); DontDestroyOnLoad(clientObject); clientObject.GetComponent <ClientLaunchObjectData>().PlayerName = playerName; clientObject.GetComponent <ClientLaunchObjectData>().IPAddress = ipAddress; //We grab the DefaultGameObjectInjectionWorld because it is needed to create ClientWorld var world = World.DefaultGameObjectInjectionWorld; //We have to account for the fact that we may be in the Editor and using ThinClients //We initially start with 1 client world which will not change if not in the editor int numClientWorlds = 1; int totalNumClients = numClientWorlds; //If in the editor we grab the amount of ThinClients from ClientServerBootstrap class (it is a static variable) //We add that to the total amount of worlds we must create #if UNITY_EDITOR int numThinClients = ClientServerBootstrap.RequestedNumThinClients; totalNumClients += numThinClients; #endif //We create the necessary number of worlds and append the number to the end for (int i = 0; i < numClientWorlds; ++i) { ClientServerBootstrap.CreateClientWorld(world, "ClientWorld" + i); } #if UNITY_EDITOR for (int i = numClientWorlds; i < totalNumClients; ++i) { var clientWorld = ClientServerBootstrap.CreateClientWorld(world, "ClientWorld" + i); clientWorld.EntityManager.CreateEntity(typeof(ThinClientComponent)); } #endif }
protected override World BootStrapCreateWorld(string name) { CalledBootStrapCreateWorld = true; return(ClientServerBootstrap.CreateClientWorld(NetCodeFixture.DefaultWorld, name)); }
protected override World BootStrapCreateWorld(string worldName) { return(ClientServerBootstrap.CreateServerWorld(World.DefaultGameObjectInjectionWorld, worldName)); }
public void SetUp() { testWorld = ClientServerBootstrap.CreateClientWorld(World.DefaultGameObjectInjectionWorld, "TestWorld"); }