Created
November 2, 2018 14:25
-
-
Save tsubaki/3850511d3ea096e4230c9ab087e0e126 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.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(); | |
} | |
} | |
} |
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; | |
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