Created
September 2, 2018 12:53
-
-
Save tsubaki/3a556832c99c486c717eb44c3135a0ef to your computer and use it in GitHub Desktop.
AsyncでExclusiveEntityTransactionを作る
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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