Created
November 2, 2018 15:09
-
-
Save tsubaki/b09733b520fed366ee83435af5654b0a to your computer and use it in GitHub Desktop.
非同期でEntityの読込
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 Unity.Entities; | |
using Unity.Entities.Serialization; | |
using UnityEngine; | |
using System.Threading.Tasks; | |
using UnityEngine.Profiling; | |
public class LoadWorldAsync : MonoBehaviour | |
{ | |
[SerializeField] string fileName = "save.sav"; | |
[SerializeField] GameObject sharedData = null; | |
private World localWorld; | |
private const string worldName = "local world"; | |
private void OnEnable() | |
{ | |
localWorld = new World(worldName); | |
localWorld.GetOrCreateManager<EntityManager>(); | |
} | |
private void OnDisable() | |
{ | |
if (localWorld.IsCreated) | |
localWorld.Dispose(); | |
} | |
public async void Load() | |
{ | |
var entityManager = localWorld.GetExistingManager<EntityManager>(); | |
var num = SerializeUtilityHybrid.DeserializeSharedComponents(entityManager, sharedData, null); | |
var transaction = entityManager.BeginExclusiveEntityTransaction(); | |
await Task.Run(() => | |
{ | |
using (var reader = new StreamBinaryReader(fileName)) | |
{ | |
SerializeUtility.DeserializeWorld(transaction, reader, num); | |
SerializeUtilityHybrid.ReleaseSharedComponents(transaction, num); | |
} | |
}); | |
entityManager.EndExclusiveEntityTransaction(); | |
World.Active.GetExistingManager<EntityManager>().MoveEntitiesFrom(entityManager); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment