Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Upgrade to entities 0.9.0 #1339

Merged
merged 11 commits into from
Apr 21, 2020
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
### Breaking Changes

- The minimum version of Unity that the GDK supports is now 2019.3. You need to upgrade your Unity project. [#1336](https://github.com/spatialos/gdk-for-unity/pull/1336)
- Upgraded the `com.unity.entities` packages to `0.7.0-preview.19`. [#1340](https://github.com/spatialos/gdk-for-unity/pull/1340)
- Upgraded the `com.unity.entities` packages to `0.9.1-preview.15`. [#1339](https://github.com/spatialos/gdk-for-unity/pull/1339)
- Authority in ECS is no longer a `SharedComponentData`, but instead a tag component.
- `ComponentAuthority` has been renamed to `HasAuthority`.
- No need to filter your ECS query anymore, instead match for `HasAuthority`.
- Built-in Unity AssetBundle module is now required.
- The `UpdateInSubSystem` attribute has been removed.
- Built-in Unity AssetBundle and UI Elements modules are now required.
- Burst is now enabled for all PC platforms.
- Component replication will no longer attempt to replicate components the worker does not have authority over.

## Added

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ protected override void OnUpdate()
for (var i = 0; i < componentReplicators.Count; i++)
{
var replicator = componentReplicators[i];
if (replicator.Group.IsEmptyIgnoreFilter)
{
continue;
}

chunkArrayCache[i] =
replicator.Group.CreateArchetypeChunkArrayAsync(Allocator.TempJob, out var jobHandle);
gatheringJobs[i] = jobHandle;
Expand All @@ -62,12 +67,23 @@ protected override void OnUpdate()
{
using (executeReplication.Auto())
{
gatheringJobs[i].Complete();
var replicator = componentReplicators[i];
var chunkArray = chunkArrayCache[i];

if (!chunkArray.IsCreated)
{
continue;
}

// Wait for gathering to complete
gatheringJobs[i].Complete();

// Process
replicator.Handler.SendUpdates(chunkArray, this, EntityManager, componentUpdateSystem);

// Cleanup
chunkArray.Dispose();
chunkArrayCache[i] = default;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Unity.Entities;
using UnityEngine.PlayerLoop;
using UnityEngine;

namespace Improbable.Gdk.Core
{
Expand Down Expand Up @@ -45,9 +45,16 @@ public class SpatialOSUpdateGroup : ComponentSystemGroup
{
}

[PlayerLoopUtils.UpdateInSubSystemAttribute(typeof(FixedUpdate))]
[UpdateInGroup(typeof(SimulationSystemGroup))]
[UpdateBefore(typeof(SpatialOSUpdateGroup))]
[DisableAutoCreation]
public class FixedUpdateSystemGroup : ComponentSystemGroup
{
protected override void OnCreate()
{
base.OnCreate();

FixedRateUtils.EnableFixedRateWithCatchUp(this, UnityEngine.Time.fixedDeltaTime);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,20 @@
using Unity.Entities;
using UnityEngine;
using UnityEngine.LowLevel;
using UnityEngine.PlayerLoop;

namespace Improbable.Gdk.Core
{
internal static class PlayerLoopUtils
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class UpdateInSubSystemAttribute : Attribute
{
public Type SubSystemType { get; }

public UpdateInSubSystemAttribute(Type subSystemType)
{
SubSystemType = subSystemType;
}
}

// UTY-2059: We need to call this specific private method to ensure the EntityDebugger shows our groups in the PlayerLoop
// No need to cache these methods, as each type will almost always be called once.
private static void InsertManagerIntoSubsystemList(PlayerLoopSystem[] subsystemList, int insertIndex,
ComponentSystemBase mgr, Type type)
{
var method = typeof(ScriptBehaviourUpdateOrder).GetMethod("InsertManagerIntoSubsystemList",
BindingFlags.Static | BindingFlags.NonPublic);
var genericMethod = method.MakeGenericMethod(type);
genericMethod.Invoke(null, new object[] { subsystemList, insertIndex, mgr });
}

public static void ResolveSystemGroups(World world)
{
// Create simulation system for the default group
var simulationSystemGroup = world.GetOrCreateSystem<SimulationSystemGroup>();

var systems = new List<ComponentSystemBase>();
foreach (var systemBase in world.Systems)
foreach (var system in world.Systems)
{
systems.Add(systemBase);
systems.Add(system);
}

var uniqueSystemTypes = new HashSet<Type>(systems.Select(s => s.GetType()));
Expand All @@ -55,8 +32,7 @@ public static void ResolveSystemGroups(World world)
// Skip the root-level systems
if (type == typeof(InitializationSystemGroup) ||
type == typeof(SimulationSystemGroup) ||
type == typeof(PresentationSystemGroup) ||
type.GetCustomAttribute<UpdateInSubSystemAttribute>() != null)
type == typeof(PresentationSystemGroup))
{
continue;
}
Expand All @@ -77,7 +53,7 @@ public static void ResolveSystemGroups(World world)
continue;
}

var systemGroup = world.GetOrCreateSystem(groupAttr.GroupType) as ComponentSystemGroup;
var systemGroup = (ComponentSystemGroup) world.GetOrCreateSystem(groupAttr.GroupType);
systemGroup.AddSystemToUpdateList(world.GetOrCreateSystem(type));
if (!uniqueSystemTypes.Contains(groupAttr.GroupType))
{
Expand All @@ -93,126 +69,17 @@ public static void ResolveSystemGroups(World world)
var type = system.GetType();
if (type == typeof(InitializationSystemGroup) ||
type == typeof(SimulationSystemGroup) ||
type == typeof(PresentationSystemGroup) ||
type.GetCustomAttribute<UpdateInSubSystemAttribute>() != null)
type == typeof(PresentationSystemGroup))
{
var groupSystem = system as ComponentSystemGroup;
groupSystem.SortSystemUpdateList();
}
}
}

public static void AddToPlayerLoop(World world)
{
var systemGroups = new List<ComponentSystemGroup>();
foreach (var systemGroup in world.Systems)
{
if (systemGroup is ComponentSystemGroup group)
{
systemGroups.Add(group);
}
}

var subSystemToGroup = new Dictionary<Type, List<ComponentSystemGroup>>();

// Build lookup for PlayerLoop
foreach (var systemGroup in systemGroups)
{
var type = systemGroup.GetType();
Type subSystemType;

// Hardcode the subSystem types for Unity root-systems
if (type == typeof(InitializationSystemGroup))
{
subSystemType = typeof(Initialization);
}
else if (type == typeof(SimulationSystemGroup))
{
subSystemType = typeof(Update);
}
else if (type == typeof(PresentationSystemGroup))
{
subSystemType = typeof(PreLateUpdate);
}
else
{
var attributes = type.GetCustomAttributes(typeof(UpdateInSubSystemAttribute), true);
if (attributes.Length == 0)
{
continue;
}

subSystemType = ((UpdateInSubSystemAttribute) attributes[0]).SubSystemType;
}

// Add to lookup table
if (!subSystemToGroup.TryGetValue(subSystemType, out var groupList))
{
groupList = new List<ComponentSystemGroup>();
subSystemToGroup.Add(subSystemType, groupList);
}

groupList.Add(systemGroup);
}

// Insert groups into PlayerLoop
var playerLoop = ScriptBehaviourUpdateOrder.CurrentPlayerLoop;
if (playerLoop.subSystemList == null)
{
playerLoop = PlayerLoop.GetDefaultPlayerLoop();
}

for (var i = 0; i < playerLoop.subSystemList.Length; ++i)
{
ref var subSystem = ref playerLoop.subSystemList[i];
if (!subSystemToGroup.TryGetValue(subSystem.type, out var groupList))
{
// No groups to add for this subsystem
continue;
}

var originalSize = subSystem.subSystemList.Length;
var newSize = originalSize + groupList.Count;
Array.Resize(ref subSystem.subSystemList, newSize);
for (var groupIndex = 0; groupIndex < groupList.Count; groupIndex++)
{
var systemGroup = groupList[groupIndex];
InsertManagerIntoSubsystemList(subSystem.subSystemList, originalSize + groupIndex,
systemGroup, systemGroup.GetType());
}
}

// Set as new PlayerLoop
ScriptBehaviourUpdateOrder.SetPlayerLoop(playerLoop);
}

public static void PrintPlayerLoop()
{
var playerLoop = ScriptBehaviourUpdateOrder.CurrentPlayerLoop;
if (playerLoop.subSystemList == null)
{
playerLoop = PlayerLoop.GetDefaultPlayerLoop();
}

foreach (var subSystem in playerLoop.subSystemList)
{
Debug.Log($"{subSystem.type}");

for (var i = 0; i < subSystem.subSystemList.Length; ++i)
{
Debug.Log($"-- {subSystem.subSystemList[i].type}");
}
}
}

public static void RemoveFromPlayerLoop(World world)
{
var playerLoop = ScriptBehaviourUpdateOrder.CurrentPlayerLoop;
if (playerLoop.subSystemList == null)
{
Debug.LogWarning("Cannot remove a world from default PlayerLoop.");
return;
}
var playerLoop = PlayerLoop.GetCurrentPlayerLoop();

//Reflection to get world from PlayerLoopSystem
var wrapperType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -10,6 +11,7 @@
using Unity.Entities;
using UnityEditor;
using UnityEngine;
using UnityEngine.LowLevel;

namespace Improbable.Gdk.Core
{
Expand Down Expand Up @@ -103,7 +105,7 @@ protected async Task Connect(IConnectionHandlerBuilder builder, ILogDispatcher l

// Update PlayerLoop
PlayerLoopUtils.ResolveSystemGroups(Worker.World);
PlayerLoopUtils.AddToPlayerLoop(Worker.World);
ScriptBehaviourUpdateOrder.UpdatePlayerLoop(Worker.World, PlayerLoop.GetCurrentPlayerLoop());
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static async Task<WorkerInWorld> CreateWorkerInWorldAsync(IConnectionHand

private void AddCoreSystems()
{
World.CreateSystem<WorkerSystem>(this);
World.AddSystem(new WorkerSystem(this));
World.GetOrCreateSystem<UpdateWorldTimeSystem>();
World.GetOrCreateSystem<CommandSystem>();
World.GetOrCreateSystem<ComponentUpdateSystem>();
Expand Down
2 changes: 1 addition & 1 deletion workers/unity/Packages/io.improbable.gdk.core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"io.improbable.worker.sdk": "0.3.4",
"io.improbable.gdk.tools": "0.3.4",
"io.improbable.gdk.testutils": "0.3.4",
"com.unity.entities": "0.7.0-preview.19"
"com.unity.entities": "0.9.1-preview.15"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void EnableStandardGameObjectCreation(World world, IEntityGameObje
return;
}

world.CreateSystem<GameObjectInitializationSystem>(creator, workerGameObject);
world.AddSystem(new GameObjectInitializationSystem(creator, workerGameObject));
}
}
}
3 changes: 2 additions & 1 deletion workers/unity/Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"com.unity.modules.assetbundle": "1.0.0",
"com.unity.modules.imgui": "1.0.0",
"com.unity.modules.physics": "1.0.0",
"com.unity.modules.ui": "1.0.0"
"com.unity.modules.ui": "1.0.0",
"com.unity.modules.uielements": "1.0.0"
},
"registry": "https://packages.unity.com"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"MonoBehaviour": {
"m_Enabled": true,
"m_EditorHideFlags": 0,
"m_Name": "",
"m_EditorClassIdentifier": "Unity.Burst.Editor:Unity.Burst.Editor:BurstPlatformAotSettings",
"DisableOptimisations": false,
"DisableSafetyChecks": true,
"DisableBurstCompilation": true
}
}
"MonoBehaviour": {
"Version": 2,
"EnableBurstCompilation": true,
"EnableOptimisations": true,
"EnableSafetyChecks": false,
"UsePlatformSDKLinker": false,
"CpuMinTargetX64": 3,
"CpuMaxTargetX64": 4
}
}
20 changes: 10 additions & 10 deletions workers/unity/ProjectSettings/BurstAotSettings_StandaloneOSX.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"MonoBehaviour": {
"m_Enabled": true,
"m_EditorHideFlags": 0,
"m_Name": "",
"m_EditorClassIdentifier": "Unity.Burst.Editor:Unity.Burst.Editor:BurstPlatformAotSettings",
"DisableOptimisations": false,
"DisableSafetyChecks": true,
"DisableBurstCompilation": true
}
}
"MonoBehaviour": {
"Version": 2,
"EnableBurstCompilation": true,
"EnableOptimisations": true,
"EnableSafetyChecks": false,
"UsePlatformSDKLinker": false,
"CpuMinTargetX64": 3,
"CpuMaxTargetX64": 4
}
}
Loading