Instantly share code, notes, and snippets.

Embed
What would you like to do?
Entityの生成と保存
using Unity.Entities;
using Unity.Transforms;
using UnityEngine;
using Unity.Collections;
public class CreateEntities : MonoBehaviour
{
[SerializeField] GameObject[] spawnObjects;
public void Spawn()
{
var random = new Unity.Mathematics.Random((uint)Time.frameCount);
var entityManager = World.Active.GetExistingManager<EntityManager>();
foreach( var obj in spawnObjects)
{
var count = random.NextInt(100, 1000);
var entities = new NativeArray<Entity>(count, Allocator.Temp);
entityManager.Instantiate(obj, entities);
for (int i=0; i<count; i++)
{
var newPos = new Position() { Value = random.NextFloat3(-1, 1) * 10 };
entityManager.SetComponentData(entities[i], newPos);
}
entities.Dispose();
}
}
}
using Unity.Entities;
using Unity.Entities.Serialization;
using UnityEngine;
public class SaveWorld : MonoBehaviour
{
[SerializeField] string fileName = "save.sav";
public void Save()
{
var entityManager = World.Active.GetExistingManager<EntityManager>();
using (var writer = new StreamBinaryWriter(fileName))
{
SerializeUtilityHybrid.Serialize(entityManager, writer, out GameObject sharedDataResult);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment