Instantly share code, notes, and snippets.

Embed
What would you like to do?
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