Skip to content

Instantly share code, notes, and snippets.

@kuma360
Last active May 19, 2018 06:08
Show Gist options
  • Save kuma360/c19727f854cfb60ba66dd252878b12cb to your computer and use it in GitHub Desktop.
Save kuma360/c19727f854cfb60ba66dd252878b12cb to your computer and use it in GitHub Desktop.
ゲームオブジェクトのプレハブをインスタンス化するのと似た感覚で、エンティティをインスタンス化できる。
//Unity2018.2.0b4 [EntityComponentSystem(ECS) ver0.0.12-preview1]
using UnityEngine;
using Unity.Entities;
using Unity.Collections;
using System.Linq;
using System.Collections.Generic;
//IComponentDataを継承した構造体がECSの部品として使える。
struct DATA : IComponentData
{
public int val;
}
//ComponentSystemを継承したクラスは、Unity内部から自動的に呼び出される
class DATA_SYSTEM : ComponentSystem
{
//ComponentSystemの中で扱うデータを定義する。
struct GROUP
{
public ComponentDataArray<DATA> _data;
}
//[Inject]の記述によって、EntityManagerが持っている実体のデータ群から、適切なデータを埋め込んでくれる。
//「実体のデータ群」は、下記GAME_OBJECTクラスの中で自分で用意する。
[Inject] GROUP group;
//毎フレーム呼ばれる
protected override void OnUpdate()
{
for (int I = 0; I < group._data.Length; ++I)
{
var P = group._data[I];
P.val += 1;
group._data[I] = P;
}
}
}
class ECS_SCRIPT : MonoBehaviour
{
EntityManager EM;
List<Entity> entityList;
private void Start()
{
EM = World.Active.GetOrCreateManager<EntityManager>();
var archeType = EM.CreateArchetype(typeof(DATA));
var entity = EM.CreateEntity(archeType);
//ゲームオブジェクトのプレハブをインスタンス化するのと似た感覚で、エンティティをインスタンス化できる。
var instance = new NativeArray<Entity>(500, Allocator.Temp);
EM.Instantiate(entity,instance);
instance.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment