コード例 #1
0
    public void Initialize()
    {
        PlayerPrefab = settings.playerPrefab;

        var initializationSystem = World.Active.GetOrCreateSystem <InitializationSystemGroup>();
        var simulationSystem     = World.Active.GetOrCreateSystem <SimulationSystemGroup>();
        var presentationSystem   = World.Active.GetOrCreateSystem <PresentationSystemGroup>();

        if (settings.HasServer)
        {
            ServerWorld = new World(WorldTypes.Server);

            ServerInitializationSystemGroup serverInitializationGroup = ServerWorld.GetOrCreateSystem <ServerInitializationSystemGroup>();
            initializationSystem.AddSystemToUpdateList(serverInitializationGroup);

            ServerSystemGroup serverSimulationGroup = ServerWorld.GetOrCreateSystem <ServerSystemGroup>();
            simulationSystem.AddSystemToUpdateList(serverSimulationGroup);
            var entity = ServerWorld.EntityManager.CreateEntity();
            ServerWorld.EntityManager.AddComponentData(entity, new NetworkSettingsComponent
            {
                // server doesnot care about ip or name
                Port = networkSettings.Port
            });
        }

        if (settings.HasClient)
        {
            var count = Mathf.Clamp(settings.clientCount, 1, 100);
            ClientWorlds = new World[count];
            for (int i = 0; i < count; i++)
            {
                var cWorld = new World($"{WorldTypes.Client} {i + 1}");

                ClientInitializationSystemGroup clientInitializationSystem = cWorld.GetOrCreateSystem <ClientInitializationSystemGroup>();
                initializationSystem.AddSystemToUpdateList(clientInitializationSystem);

                ClientSystemGroup clientSimulationGroup = cWorld.GetOrCreateSystem <ClientSystemGroup>();
                simulationSystem.AddSystemToUpdateList(clientSimulationGroup);

                ClientWorlds[i] = cWorld;
                if (i != settings.playerClient)
                {
                    var inputSystem = cWorld.GetExistingSystem(typeof(ClientInputSystem));
                    inputSystem.Enabled = false;
                    var randomWalker = cWorld.GetOrCreateSystem(typeof(ClientRandomWalkSystem));
                    clientSimulationGroup.AddSystemToUpdateList(randomWalker);
                    clientSimulationGroup.SortSystemUpdateList();
                }
                else
                {
                    ClientPresentationSystemGroup clientPresentationSystem = cWorld.GetOrCreateSystem <ClientPresentationSystemGroup>();
                    presentationSystem.AddSystemToUpdateList(clientPresentationSystem);
                }

                var entity = cWorld.EntityManager.CreateEntity();
                cWorld.EntityManager.AddComponentData(entity, new NetworkSettingsComponent
                {
                    Name = new NativeString64(networkSettings.Name),
                    Ip   = new NativeString64(networkSettings.Ip),
                    Port = networkSettings.Port
                });
            }
        }
    }
コード例 #2
0
    public List <Type> Initialize(List <Type> systems)
    {
        // Workaround for initialization being called multiple times when using game object conversion
#if !UNITY_SERVER
        if (clientWorld != null)
        {
            return(systems);
        }
#endif
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
        if (serverWorld != null)
        {
            return(systems);
        }
#endif

#if !UNITY_SERVER
#if UNITY_EDITOR
        int numClientWorlds = UnityEditor.EditorPrefs.GetInt("MultiplayerPlayMode_" + UnityEngine.Application.productName + "_NumClients");
        if (numClientWorlds < 1)
        {
            numClientWorlds = 1;
        }
        if (numClientWorlds > 8)
        {
            numClientWorlds = 8;
        }
        int playModeType = UnityEditor.EditorPrefs.GetInt("MultiplayerPlayMode_" + UnityEngine.Application.productName + "_Type");
#else
        int numClientWorlds = 1;
#endif
#endif

        var defaultBootstrap = new List <Type>();
#if !UNITY_SERVER
        clientWorld = null;
        ClientInitializationSystemGroup[] clientInitializationSystemGroup = null;
        ClientSimulationSystemGroup[]     clientSimulationSystemGroup     = null;
        ClientPresentationSystemGroup[]   clientPresentationSystemGroup   = null;
#if UNITY_EDITOR
        if (playModeType != 2)
#endif
        {
            clientWorld = new World[numClientWorlds];
            clientInitializationSystemGroup = new ClientInitializationSystemGroup[clientWorld.Length];
            clientSimulationSystemGroup     = new ClientSimulationSystemGroup[clientWorld.Length];
            clientPresentationSystemGroup   = new ClientPresentationSystemGroup[clientWorld.Length];
            for (int i = 0; i < clientWorld.Length; ++i)
            {
                clientWorld[i] = new World("ClientWorld" + i);
                clientInitializationSystemGroup[i] = clientWorld[i].GetOrCreateSystem <ClientInitializationSystemGroup>();
                clientSimulationSystemGroup[i]     = clientWorld[i].GetOrCreateSystem <ClientSimulationSystemGroup>();
#if UNITY_EDITOR
                clientSimulationSystemGroup[i].ClientWorldIndex = i;
#endif
                clientPresentationSystemGroup[i] = clientWorld[i].GetOrCreateSystem <ClientPresentationSystemGroup>();
            }
        }
#endif
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
        serverWorld = null;
        ServerInitializationSystemGroup serverInitializationSystemGroup = null;
        ServerSimulationSystemGroup     serverSimulationSystemGroup     = null;
#if UNITY_EDITOR
        if (playModeType != 1)
#endif
        {
            serverWorld = new World("ServerWorld");
            serverInitializationSystemGroup = serverWorld.GetOrCreateSystem <ServerInitializationSystemGroup>();
            serverSimulationSystemGroup     = serverWorld.GetOrCreateSystem <ServerSimulationSystemGroup>();
        }
#endif
        foreach (var type in systems)
        {
            if (type.GetCustomAttributes(typeof(NotClientServerSystemAttribute), true).Length > 0 ||
                type == typeof(InitializationSystemGroup) ||
                type == typeof(SimulationSystemGroup) ||
                type == typeof(PresentationSystemGroup))
            {
                defaultBootstrap.Add(type);
                continue;
            }
            var groups = type.GetCustomAttributes(typeof(UpdateInGroupAttribute), true);
            if (groups.Length == 0)
            {
                defaultBootstrap.Add(type);
                // Treat the default group as both client and server
                groups = new object[] { new UpdateInGroupAttribute(typeof(ClientAndServerSimulationSystemGroup)) };
            }

            foreach (var grp in groups)
            {
                var group = grp as UpdateInGroupAttribute;
                if (group.GroupType == typeof(ClientAndServerSimulationSystemGroup) || group.GroupType == typeof(SimulationSystemGroup))
                {
                    if (group.GroupType == typeof(SimulationSystemGroup))
                    {
                        defaultBootstrap.Add(type);
                    }
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
                    if (serverWorld != null)
                    {
                        var defaultWorld = World.Active;
                        World.Active = serverWorld;
                        serverSimulationSystemGroup.AddSystemToUpdateList(
                            serverWorld.GetOrCreateSystem(type));
                        World.Active = defaultWorld;
                    }
#endif
#if !UNITY_SERVER
                    if (clientWorld != null)
                    {
                        var defaultWorld = World.Active;
                        for (int i = 0; i < clientSimulationSystemGroup.Length; ++i)
                        {
                            World.Active = clientWorld[i];
                            clientSimulationSystemGroup[i]
                            .AddSystemToUpdateList(clientWorld[i].GetOrCreateSystem(type));
                        }
                        World.Active = defaultWorld;
                    }
#endif
                }
                else if (group.GroupType == typeof(ClientAndServerInitializationSystemGroup) || group.GroupType == typeof(InitializationSystemGroup))
                {
                    if (group.GroupType == typeof(InitializationSystemGroup))
                    {
                        defaultBootstrap.Add(type);
                    }
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
                    if (serverWorld != null)
                    {
                        var defaultWorld = World.Active;
                        World.Active = serverWorld;
                        serverInitializationSystemGroup.AddSystemToUpdateList(
                            serverWorld.GetOrCreateSystem(type));
                        World.Active = defaultWorld;
                    }
#endif
#if !UNITY_SERVER
                    if (clientWorld != null)
                    {
                        var defaultWorld = World.Active;
                        for (int i = 0; i < clientInitializationSystemGroup.Length; ++i)
                        {
                            World.Active = clientWorld[i];
                            clientInitializationSystemGroup[i]
                            .AddSystemToUpdateList(clientWorld[i].GetOrCreateSystem(type));
                        }
                        World.Active = defaultWorld;
                    }
#endif
                }
                else if (group.GroupType == typeof(ServerInitializationSystemGroup))
                {
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
                    if (serverWorld != null)
                    {
                        var defaultWorld = World.Active;
                        World.Active = serverWorld;
                        serverInitializationSystemGroup.AddSystemToUpdateList(
                            serverWorld.GetOrCreateSystem(type));
                        World.Active = defaultWorld;
                    }
#endif
                }
                else if (group.GroupType == typeof(ClientInitializationSystemGroup))
                {
#if !UNITY_SERVER
                    if (clientWorld != null)
                    {
                        var defaultWorld = World.Active;
                        for (int i = 0; i < clientInitializationSystemGroup.Length; ++i)
                        {
                            World.Active = clientWorld[i];
                            clientInitializationSystemGroup[i]
                            .AddSystemToUpdateList(clientWorld[i].GetOrCreateSystem(type));
                        }
                        World.Active = defaultWorld;
                    }
#endif
                }
                else if (group.GroupType == typeof(ServerSimulationSystemGroup))
                {
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
                    if (serverWorld != null)
                    {
                        var defaultWorld = World.Active;
                        World.Active = serverWorld;
                        serverSimulationSystemGroup.AddSystemToUpdateList(
                            serverWorld.GetOrCreateSystem(type));
                        World.Active = defaultWorld;
                    }
#endif
                }
                else if (group.GroupType == typeof(ClientSimulationSystemGroup))
                {
#if !UNITY_SERVER
                    if (clientWorld != null)
                    {
                        var defaultWorld = World.Active;
                        for (int i = 0; i < clientSimulationSystemGroup.Length; ++i)
                        {
                            World.Active = clientWorld[i];
                            clientSimulationSystemGroup[i]
                            .AddSystemToUpdateList(clientWorld[i].GetOrCreateSystem(type));
                        }
                        World.Active = defaultWorld;
                    }
#endif
                }
                else if (group.GroupType == typeof(ClientPresentationSystemGroup) || group.GroupType == typeof(PresentationSystemGroup))
                {
                    if (group.GroupType == typeof(PresentationSystemGroup))
                    {
                        defaultBootstrap.Add(type);
                    }
#if !UNITY_SERVER
                    if (clientWorld != null)
                    {
                        var defaultWorld = World.Active;
                        for (int i = 0; i < clientPresentationSystemGroup.Length; ++i)
                        {
                            World.Active = clientWorld[i];
                            clientPresentationSystemGroup[i]
                            .AddSystemToUpdateList(clientWorld[i].GetOrCreateSystem(type));
                        }
                        World.Active = defaultWorld;
                    }
#endif
                }
                else
                {
                    var mask = GetTopLevelWorldMask(group.GroupType);
                    if ((mask & WorldType.DefaultWorld) != 0)
                    {
                        defaultBootstrap.Add(type);
                    }
#if !UNITY_SERVER
                    if ((mask & WorldType.ClientWorld) != 0 && clientWorld != null)
                    {
                        var defaultWorld = World.Active;
                        for (int i = 0; i < clientWorld.Length; ++i)
                        {
                            World.Active = clientWorld[i];
                            var groupSys = clientWorld[i].GetOrCreateSystem(group.GroupType) as ComponentSystemGroup;
                            groupSys.AddSystemToUpdateList(clientWorld[i].GetOrCreateSystem(type));
                        }
                        World.Active = defaultWorld;
                    }
#endif
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
                    if ((mask & WorldType.ServerWorld) != 0 && serverWorld != null)
                    {
                        var defaultWorld = World.Active;
                        World.Active = serverWorld;
                        var groupSys = serverWorld.GetOrCreateSystem(group.GroupType) as ComponentSystemGroup;
                        groupSys.AddSystemToUpdateList(serverWorld.GetOrCreateSystem(type));
                        World.Active = defaultWorld;
                    }
#endif
                }
            }
        }
#if !UNITY_CLIENT || UNITY_SERVER || UNITY_EDITOR
        if (serverWorld != null)
        {
            serverInitializationSystemGroup.SortSystemUpdateList();
            serverSimulationSystemGroup.SortSystemUpdateList();
            World.Active.GetOrCreateSystem <TickServerInitializationSystem>().AddSystemToUpdateList(serverInitializationSystemGroup);
            World.Active.GetOrCreateSystem <TickServerSimulationSystem>().AddSystemToUpdateList(serverSimulationSystemGroup);
        }
#endif
#if !UNITY_SERVER
        if (clientWorld != null)
        {
            for (int i = 0; i < clientWorld.Length; ++i)
            {
                clientInitializationSystemGroup[i].SortSystemUpdateList();
                clientSimulationSystemGroup[i].SortSystemUpdateList();
                clientPresentationSystemGroup[i].SortSystemUpdateList();
                World.Active.GetOrCreateSystem <TickClientInitializationSystem>().AddSystemToUpdateList(clientInitializationSystemGroup[i]);
                World.Active.GetOrCreateSystem <TickClientSimulationSystem>().AddSystemToUpdateList(clientSimulationSystemGroup[i]);
                World.Active.GetOrCreateSystem <TickClientPresentationSystem>().AddSystemToUpdateList(clientPresentationSystemGroup[i]);
            }
        }
#endif
        return(defaultBootstrap);
    }