Skip to content

Instantly share code, notes, and snippets.

@tsubaki tsubaki/Component.cs
Last active Apr 5, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.