Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created September 2, 2018 12:53
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/3a556832c99c486c717eb44c3135a0ef to your computer and use it in GitHub Desktop.
Save tsubaki/3a556832c99c486c717eb44c3135a0ef to your computer and use it in GitHub Desktop.
AsyncでExclusiveEntityTransactionを作る
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Unity.Entities;
using Unity.Jobs;
using UnityEngine;
using UnityEngine.Profiling;
public class Loader2 : MonoBehaviour {
World myWorld;
EntityManager myEntityManager;
private void Awake()
{
myWorld = new World("my world");
myEntityManager = myWorld.GetOrCreateManager<EntityManager>();
World.Active.GetExistingManager<EntityManager>().MoveEntitiesFrom(myEntityManager);
}
private void OnDestroy()
{
if( myWorld.IsCreated)
myWorld.Dispose();
}
async void Start()
{
var samplerThread = CustomSampler.Create("thread");
var entity = myEntityManager.CreateEntity(typeof(Sample));
var commands = myEntityManager.BeginExclusiveEntityTransaction();
await Task.Run(() =>
{
Profiler.BeginThreadProfiling("creater", "spawnInstance");
samplerThread.Begin();
for (int i = 0; i < 10000; i++)
{
var e = commands.Instantiate(entity);
var c = new Sample() { value = i };
commands.SetComponentData(e, c);
}
samplerThread.End();
Profiler.EndThreadProfiling();
});
myEntityManager.EndExclusiveEntityTransaction();
World.Active.GetExistingManager<EntityManager>().MoveEntitiesFrom(myEntityManager);
}
struct Sample : IComponentData
{
public int value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment