Skip to content

Instantly share code, notes, and snippets.

@kou-yeung
Last active November 9, 2018 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kou-yeung/595a4d4d33d6214e9a6539be9328097d to your computer and use it in GitHub Desktop.
Save kou-yeung/595a4d4d33d6214e9a6539be9328097d to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Linq;
using Unity.Entities; // 追加
using Unity.Rendering; // 追加
using Unity.Transforms; // 追加
public class Manager : MonoBehaviour
{
public GameObject prefab;
public float numOfObject = 10000; // 生成するオブジェクトの数
public float distance = 100; // ランダム生成の範囲(中心からの距離)
void Start()
{
// 既存コード
//for (int i = 0; i < numOfObject; i++)
//{
// var go = Instantiate(prefab);
// // ランダム座標設定
// go.transform.localPosition = Random.insideUnitSphere * distance;
// go.transform.localRotation = Random.rotation;
//}
var manager = World.Active.GetOrCreateManager<EntityManager>();
var archetype = manager.CreateArchetype(new ComponentType[]
{
typeof(Position), // 座標がある
typeof(Rotation), // 角度もある
});
// MeshInstanceRenderer の情報を取得
var look = GetComponent<MeshInstanceRendererComponent>().Value;
for (int i = 0; i < numOfObject; i++)
{
// EntityArchetype を元に Entity を生成する
var entity = manager.CreateEntity(archetype);
// Entity に MeshInstanceRenderer を追加する
manager.AddSharedComponentData(entity, look);
// ランダム座標設定
manager.SetComponentData(entity, new Position { Value = Random.insideUnitSphere * distance });
manager.SetComponentData(entity, new Rotation { Value = Random.rotation });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment