Skip to content

Instantly share code, notes, and snippets.

@JohnnyTurbo
Created October 31, 2021 13:29
Show Gist options
  • Save JohnnyTurbo/95a7a761e4979e091f9700016f302e00 to your computer and use it in GitHub Desktop.
Save JohnnyTurbo/95a7a761e4979e091f9700016f302e00 to your computer and use it in GitHub Desktop.
Code used in System Lifecycle tutorial video on the Turbo Makes Games YouTube Channel - https://youtu.be/pViD_h8NXf0
using Unity.Entities;
namespace TMG.SystemLifecycle
{
[GenerateAuthoringComponent]
public struct CapsuleTag : IComponentData {}
}
using Unity.Entities;
using UnityEngine;
namespace TMG.SystemLifecycle
{
[DisableAutoCreation]
public class IntroSystem : SystemBase
{
protected override void OnCreate()
{
Debug.Log("OnCreate()");
}
protected override void OnStartRunning()
{
Debug.Log("OnStartRunning()");
}
protected override void OnUpdate()
{
Debug.Log("OnUpdate()");
Entities.WithAll<CapsuleTag>().ForEach((Entity e) =>
{
}).Run();
}
protected override void OnStopRunning()
{
Debug.Log("OnStopRunning()");
}
protected override void OnDestroy()
{
Debug.Log("OnDestroy()");
}
}
}
using Unity.Entities;
namespace TMG.SystemLifecycle
{
[GenerateAuthoringComponent]
public struct SpawnCapsuleData : IComponentData
{
public Entity CapsulePrefab;
}
}
using Unity.Entities;
using UnityEngine;
namespace TMG.SystemLifecycle
{
public class SpawnCapsuleSystem : SystemBase
{
private SpawnCapsuleData _spawnCapsuleData;
private Entity _spawnedCapsule;
protected override void OnStartRunning()
{
_spawnCapsuleData = GetSingleton<SpawnCapsuleData>();
}
protected override void OnUpdate()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
_spawnedCapsule = EntityManager.Instantiate(_spawnCapsuleData.CapsulePrefab);
}
if(Input.GetKeyDown(KeyCode.Alpha2))
{
EntityManager.DestroyEntity(_spawnedCapsule);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment