Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active July 5, 2023 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsubaki/329409cfa439b747adaedbb0a797f5fa to your computer and use it in GitHub Desktop.
Save tsubaki/329409cfa439b747adaedbb0a797f5fa to your computer and use it in GitHub Desktop.
ExclusiveEntityTransactionサンプル
using System.Collections;
using UnityEngine;
using Unity.Entities;
using Unity.Jobs;
using Unity.Collections;
public class Loader : MonoBehaviour
{
World myWorld;
EntityManager myEntityManager;
private void Awake()
{
// WorldとWorld用EntityManagerを作成
// 初期世界以外は最初から全てのシステムが登録されていない
myWorld = new World("my world");
myEntityManager = myWorld.GetOrCreateManager<EntityManager>();
}
private void OnDestroy()
{
// worldの停止は非常に高い負荷になる
myWorld.Dispose();
}
IEnumerator Start ()
{
// 事前にEntityを一つ作っておく。コレがないとワールド移行時にすごい負荷になる模様
myEntityManager.CreateEntity(typeof(DummyData));
World.Active.GetExistingManager<EntityManager>().MoveEntitiesFrom(myEntityManager);
// マウスクリックでロードを開始
yield return new WaitUntil(() => Input.GetMouseButtonDown(0));
// Entityを事前に作成しておく。
// 排他モードになるとEntityManagerが使えなくなるので、作るものは事前に作成
var entity = myEntityManager.CreateEntity(typeof(DummyData));
// 排他モードを開始してジョブを実行する
var commands = myEntityManager.BeginExclusiveEntityTransaction();
myEntityManager.ExclusiveEntityTransactionDependency = new CreateEntityJob()
{
commands = commands,
entity = entity,
count = 38000
}.Schedule(myEntityManager.ExclusiveEntityTransactionDependency);
JobHandle.ScheduleBatchedJobs();
// 処理が完了するまで待つ
yield return new WaitUntil(() => myEntityManager.ExclusiveEntityTransactionDependency.IsCompleted);
// 処理が完了したら排他モードを停止。
// 現在アクティブなワールドへ、作ったEntityを全て移動する(個別に移動する機能や、コピーする機能はまだない)
myEntityManager.EndExclusiveEntityTransaction();
World.Active.GetOrCreateManager<EntityManager>().MoveEntitiesFrom(myEntityManager);
}
}
/// <summary>
/// 任意のEntityをCount個作成するジョブ
/// </summary>
struct CreateEntityJob : IJob
{
public ExclusiveEntityTransaction commands; // コマンドを実行するEntityManagerのExclusiveEntityTransaction
public Entity entity; // 生成するEntity。複数種類作りたいならジョブを繋げる
public int count;
public void Execute()
{
for (int i = 0; i < count; i++)
{
var e = commands.Instantiate(entity);
var c = new DummyData() { value = i };
commands.SetComponentData(e, c);
}
}
}
struct DummyData : IComponentData
{
public int value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment