Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active April 5, 2019 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/9144deb0378e4ec269b22d0ab24bd1d9 to your computer and use it in GitHub Desktop.
Save tsubaki/9144deb0378e4ec269b22d0ab24bd1d9 to your computer and use it in GitHub Desktop.
ISystemStateComponentData Sample
using Unity.Entities;
using System;
public struct Life : IComponentData{}
[Serializable]
public struct LifeState : ISystemStateComponentData
{
public Place lastPlace;
}
public enum Place
{
Office,
Home
}
using UnityEngine;
using Unity.Entities;
public class LifeSpawner : MonoBehaviour
{
void Start()
{
Debug.Log("Entity生成");
World.Active.EntityManager.CreateEntity(
ComponentType.ReadWrite<Life>(),
ComponentType.ReadWrite<LifeState>());
}
}
using UnityEngine;
using Unity.Entities;
using Unity.Jobs;
/// <summary>
/// Lifeの居る場所を切り替えるだけ
///
/// Zキー:家に移動
/// Xキー:オフィスに移動
/// </summary>
public class LifeSystem : ComponentSystem
{
protected override void OnUpdate()
{
if (Input.GetKeyDown(KeyCode.Z))
{
Entities.ForEach((ref LifeState state) => state.lastPlace = Place.Home);
Debug.Log($"今は{ Place.Home }に居ます");
}
if (Input.GetKeyDown(KeyCode.X))
{
Entities.ForEach((ref LifeState state) => state.lastPlace = Place.Office);
Debug.Log($"今は{ Place.Office }に居ます");
}
}
}
/// <summary>
/// Lifeを全て殺す
///
/// Aキー:殺す
/// </summary>
public class MurderSystem : ComponentSystem
{
protected override void OnUpdate()
{
if (Input.GetKeyDown(KeyCode.A))
{
Debug.Log("死ね! Entity!!!");
Entities.WithAll<Life>().ForEach((Entity entity) => PostUpdateCommands.DestroyEntity(entity));
}
}
}
/// <summary>
/// 遺言を表示
/// Lifeが最後に居た場所を表示する
/// </summary>
public class EndLifeSystem : ComponentSystem
{
protected override void OnUpdate()
{
Entities.WithNone<Life>().ForEach((Entity entity, ref LifeState state) =>
{
Debug.Log($"死ぬ前に居た場所は{state.lastPlace}です");
PostUpdateCommands.RemoveComponent<LifeState>(entity);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment