Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
613 changes: 606 additions & 7 deletions NetcodeSamples/Assets/Samples/Asteroids/Asteroids.unity

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Unity.Entities;
using Unity.NetCode.HostMigration;
using UnityEngine;

[DisallowMultipleComponent]
Expand All @@ -15,6 +16,7 @@ public override void Bake(ServerSettingsAuthoring authoring)
component.levelData = authoring.levelData;
var entity = GetEntity(TransformUsageFlags.Dynamic);
AddComponent(entity, component);
AddComponent<IncludeInMigration>(entity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void Execute(ref ParticleEmitterComponentData emitter, in ShipStateCompon
}

[UpdateInGroup(typeof(PresentationSystemGroup))]
[BurstCompile]
public partial class ShipTrackingSystem : SystemBase
{
EntityQuery m_LevelGroup;
Expand All @@ -54,7 +55,8 @@ protected override void OnDestroy()
m_RenderOffset.Dispose();
}

partial struct ShipTrackingJob : IJobEntity
[BurstCompile]
partial struct ShipTrackingJob : IJob
{
public float deltaTime;
public int screenHeight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ partial struct StaticAsteroidJob : IJobChunk

public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
{
// This job is not written to support queries with enableable component types.
UnityEngine.Assertions.Assert.IsFalse(useEnabledMask);

// We update the server values infrequently, as we only need the updated
// LocalTransform positions for distance importance scaling & relevancy calculations.
if (isServer && (chunk.SequenceNumber + tick.TickIndexForValidTick) % roundRobinFrequency >= simulationStepBatchSize) return;
Expand Down
51 changes: 51 additions & 0 deletions NetcodeSamples/Assets/Samples/Asteroids/Server/GameOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Unity.Entities;
using Unity.NetCode;
using UnityEngine;
using UnityEngine.UI;

namespace Samples.Asteroids.Host.UI
{
public class GameOptions : MonoBehaviour
{
GameObject m_OptionsPanel;
InputField m_AsteroidCountTextBox;
EntityQuery m_ServerSettingsQuery;

void Start()
{
m_OptionsPanel = gameObject.transform.Find("Panel").gameObject;
m_OptionsPanel.SetActive(false);
m_AsteroidCountTextBox = m_OptionsPanel.transform.Find("Count").GetComponent<InputField>();
}

void Update()
{
// Can only set options on the host/server
if (ClientServerBootstrap.ServerWorld == null)
return;

if ( Input.GetKeyDown("o") )
{
m_OptionsPanel.SetActive(!m_OptionsPanel.activeSelf);

if (m_OptionsPanel.activeSelf)
{
if (m_ServerSettingsQuery==default)
m_ServerSettingsQuery = ClientServerBootstrap.ServerWorld.EntityManager.CreateEntityQuery(typeof(ServerSettings));

if (m_ServerSettingsQuery.IsEmpty)
return;

var numAsteroids = m_ServerSettingsQuery.GetSingleton<ServerSettings>().levelData.numAsteroids;
m_AsteroidCountTextBox.text = $"{numAsteroids}";
}
}
}

public void SetAsteroidCount()
{
m_ServerSettingsQuery.GetSingletonRW<ServerSettings>().ValueRW.levelData.numAsteroids = Int32.Parse( m_AsteroidCountTextBox.text );
}
}
}
11 changes: 11 additions & 0 deletions NetcodeSamples/Assets/Samples/Asteroids/Server/GameOptions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,19 @@ public void Execute()

ecb.SetComponent(e, trans);
if (staticAsteroidOptimization == 1)
{
UnityEngine.Debug.Assert(tick.IsValid);
ecb.SetComponent(e,
new StaticAsteroid
{
InitialPosition = trans.Position.xy, InitialVelocity = vel.Value, InitialAngle = angle,
SpawnTick = tick
SpawnTick = tick,
});
}
else
{
ecb.SetComponent(e, vel);
}
}

random.Value = rand;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Unity.Burst;
using Unity.Entities;
using Unity.Jobs;
using Unity.Collections;
using Unity.NetCode;
using Unity.Mathematics;
using Unity.NetCode.HostMigration;
using System;

namespace Asteroids.Server
{
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
[UpdateBefore(typeof(RpcSystem))]
public partial struct RegisterMigrationTypesSystem : ISystem
{
public void OnCreate(ref SystemState state)
{
var migrationTypesEntity = state.EntityManager.CreateSingletonBuffer<NonGhostMigrationComponents>();
var migreationTypesBuffer = state.EntityManager.GetBuffer<NonGhostMigrationComponents>(migrationTypesEntity, false);

migreationTypesBuffer.Add( new NonGhostMigrationComponents(){ ComponentToMigrate = ComponentType.ReadOnly<ServerSettings>() } );
}

[BurstCompile]
public void OnUpdate(ref SystemState state)
{
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override bool Initialize(string defaultWorldName)
var activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
if (!isFromCommandLine) targetScene = activeScene;

var isFrontend = targetScene == frontendScene;
var isFrontend = targetScene.Contains(frontendScene);

// Handle server setup errors:
if (IsServerPlatform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ See
* _Establish a connection_ section in the [Getting Started](https://docs.unity3d.com/Packages/com.unity.netcode@latest?subfolder=/manual/getting-started.html) guide
* [Client Server Worlds](https://docs.unity3d.com/Packages/com.unity.netcode@latest?subfolder=/manual/client-server-worlds.html)
* [Network Connection](https://docs.unity3d.com/Packages/com.unity.netcode@latest?subfolder=/manual/network-connection.html)
* [Multiplayer sessions](https://docs.unity.com/ugs/en-us/manual/mps-sdk/manual)
* [Host migration](https://docs.unity3d.com/Packages/com.unity.netcode@1.10/manual/host-migration/host-migration.html)

## Sample description

Expand All @@ -21,13 +23,16 @@ This sample shows the modification needed to the bootstrap functionality to enab

The frontend is placed in a separate assembly from the bootstrap assembly since this is something we do not want to include in dedicated server builds (UNITY_SERVER is defined).

The frontend allows you to either use direct IP/port connections or use the sessions feature. Sessions allow you to set up a session ID which anyone can then connect to, either with relay or directly. It also allows you to enable host migration support in a simple manner.

There three scenes here:

* _Frontend.unity_ has manual connection setup (connect or host) and on demand world creation (at runtime) with scene selection of all sample scenes in the project.
* There is a companion _FrontendHUD.unity_ scene which shows the UI for going back to the main menu after a scene has been opened from the frontend, and text for the connection status.
* There is a companion _FrontendHUD.unity_ scene which shows the UI for going back to the main menu after a scene has been opened from the frontend, a text for the connection status, as well as the session ID in use (if any).
* There is also a _HostMigrationHUD.unity_ scene which is only loaded when host migration support is enabled. This shows statistics about host migration like the migration data size.
* A _FrontendBootstrap_ scene is meant to be placed first in the build settings scene list and sets up a few flows for the first sample scene loaded. It loads the Asteroids sample by default when a dedicated server is launched, but otherwise the frontend scene. When a scene name is given on the command line it picks that instead.

## Note

> [!NOTE]
> The Frontend scene contains a toggle for relay support. To read more about what this feature entails, refer to the 01b_RelaySupport description.
> There are two extra frontend scenes for showing how to manually set up relay and host migration support without using sessions. To read more about these, refer to the 01b_RelaySupport and 01d_HostMigration descriptions. Also note that only one frontend can be enabled in the build settings at a time, to try out a different frontend in a build select it (and deselect current one) in the build settings scene list.
Loading