Skip to content

Instantly share code, notes, and snippets.

@JohnnyTurbo
Created May 11, 2021 17:10
Show Gist options
  • Select an option

  • Save JohnnyTurbo/4c87e4a20ab28da2957709b05106e045 to your computer and use it in GitHub Desktop.

Select an option

Save JohnnyTurbo/4c87e4a20ab28da2957709b05106e045 to your computer and use it in GitHub Desktop.
Singletons in Unity ECS - Code from tutorial video: https://youtu.be/oSj6w8AeeAg
using Unity.Entities;
namespace TMG.ECS_Singletons
{
public class ApplyRadiationSystem : SystemBase
{
private EndSimulationEntityCommandBufferSystem _endSimulationEntityCommandBufferSystem;
protected override void OnCreate()
{
RequireSingletonForUpdate<RadiationTag>();
_endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var ecb = _endSimulationEntityCommandBufferSystem.CreateCommandBuffer().AsParallelWriter();
var deltaTime = Time.DeltaTime;
Entities.ForEach((Entity e, int entityInQueryIndex, ref TimeToLiveData timeToLive) =>
{
timeToLive.Value -= deltaTime;
if (timeToLive.Value <= 0)
{
ecb.DestroyEntity(entityInQueryIndex, e);
}
}).ScheduleParallel();
Dependency.Complete();
}
}
}
using Unity.Entities;
namespace TMG.ECS_Singletons
{
[GenerateAuthoringComponent]
public struct RadiationTag : IComponentData {}
}
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
using Random = Unity.Mathematics.Random;
namespace TMG.ECS_Singletons
{
public class SpawnCapsuleAuthoring : MonoBehaviour, IDeclareReferencedPrefabs, IConvertGameObjectToEntity
{
public float SpawnInterval;
public GameObject CapsulePrefab;
public Vector3 MinSpawnPosition;
public Vector3 MaxSpawnPosition;
public KeyCode FunKey;
public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
{
referencedPrefabs.Add(CapsulePrefab);
}
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
var spawnCapsuleData = new SpawnCapsuleData
{
SpawnInterval = SpawnInterval,
SpawnTimer = SpawnInterval,
EntityPrefab = conversionSystem.GetPrimaryEntity(CapsulePrefab),
FunKey = FunKey,
Random = Random.CreateFromIndex(0),
MinSpawnPosition = MinSpawnPosition,
MaxSpawnPosition = MaxSpawnPosition
};
dstManager.AddComponentData(entity, spawnCapsuleData);
}
}
}
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using Random = Unity.Mathematics.Random;
namespace TMG.ECS_Singletons
{
public struct SpawnCapsuleData : IComponentData
{
public float SpawnInterval;
public float SpawnTimer;
public Entity EntityPrefab;
public KeyCode FunKey;
public float3 MinSpawnPosition;
public float3 MaxSpawnPosition;
public Random Random;
public float3 RandomSpawnPos => Random.NextFloat3(MinSpawnPosition, MaxSpawnPosition);
}
}
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using Random = Unity.Mathematics.Random;
namespace TMG.ECS_Singletons
{
public class SpawnCapsuleSystem : SystemBase
{
private SpawnCapsuleData _spawnCapsuleData;
protected override void OnStartRunning()
{
_spawnCapsuleData = GetSingleton<SpawnCapsuleData>();
}
protected override void OnUpdate()
{
var spawnTimer = _spawnCapsuleData.SpawnTimer -= Time.DeltaTime;
if (spawnTimer <= 0)
{
var newEntity = EntityManager.Instantiate(_spawnCapsuleData.EntityPrefab);
var newTranslation = new Translation()
{
Value = _spawnCapsuleData.RandomSpawnPos
};
EntityManager.SetComponentData(newEntity, newTranslation);
_spawnCapsuleData.SpawnTimer = _spawnCapsuleData.SpawnInterval;
}
if (Input.GetKeyDown(_spawnCapsuleData.FunKey))
{
var newSpawnData = new SpawnCapsuleData()
{
EntityPrefab = _spawnCapsuleData.EntityPrefab,
FunKey = _spawnCapsuleData.FunKey,
MinSpawnPosition = new float3(12.5f, 0.8f, 12.5f),
MaxSpawnPosition = new float3(12.5f, 0.8f, 12.5f),
Random = Random.CreateFromIndex(1),
SpawnInterval = 0.01f,
SpawnTimer = 0.01f,
};
SetSingleton(newSpawnData);
_spawnCapsuleData = GetSingleton<SpawnCapsuleData>();
}
}
}
}
using Unity.Entities;
namespace TMG.ECS_Singletons
{
[GenerateAuthoringComponent]
public struct TimeToLiveData : IComponentData
{
public float Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment