NOT WORKING : InvalidOperationException: Structural changes are not allowed during Entities.ForEach. Please use EntityCommandBuffer instead.
public class SubSceneSystem : SystemBase
{
protected override void OnUpdate()
{
Entities
.WithAll()
.WithoutBurst()
.ForEach((Entity entity, ref Translation translation) =>
{
var h = Input.GetAxis(“Horizontal”);
var v = Input.GetAxis(“Vertical”);
translation.Value.x += h;
translation.Value.z += v;
foreach (var val in SubSceneReference._instance._subscenes)
{
//Debug.Log(_sceneSystem.ToString() + val.SceneGUID);
if (math.distance(translation.Value, val.transform.position) < 15)
{
//_sceneSystem.UnloadScene(val.SceneGUID);
if (!val.IsLoaded)
{
_sceneSystem.LoadSceneAsync(val.SceneGUID);
Debug.Log(“LoadSceneAsync”);
}
}
else
{
//if (val.IsLoaded)
{
UnLoadScene(val);
Debug.Log(“UnLoadScene”);
}
}
}
}).Run();
}}
WORKING
public class SubSceneSystem : ComponentSystem
{
//struct SceneLoadJob : IJobParallelFor
//{
// public NativeArray _subScenes;
// public void Execute(int index)
// {
// }
//}
SceneSystem _sceneSystem;
EndSimulationEntityCommandBufferSystem m_EndSimulationEcbSystem;
protected override void OnCreate()
{
_sceneSystem = World.GetOrCreateSystem();
//m_EndSimulationEcbSystem = World
// .GetOrCreateSystem();
}
protected override void OnUpdate()
{
Entities
.WithAll()
.ForEach((Entity entity, ref Translation translation) =>
{
var h = Input.GetAxis(“Horizontal”);
var v = Input.GetAxis(“Vertical”);
translation.Value.x += h;
translation.Value.z += v;
foreach (var val in SubSceneReference._instance._subscenes)
{
//Debug.Log(_sceneSystem.ToString() + val.SceneGUID);
if (math.distance(translation.Value, val.transform.position) < 15)
{
//_sceneSystem.UnloadScene(val.SceneGUID);
if (!val.IsLoaded)
{
_sceneSystem.LoadSceneAsync(val.SceneGUID);
Debug.Log(“LoadSceneAsync”);
}
}
else
{
//if (val.IsLoaded)
{
UnLoadScene(val);
Debug.Log(“UnLoadScene”);
}
}
}
});
}