Skip to content

Instantly share code, notes, and snippets.

@kou-yeung kou-yeung/Manager.cs Secret
Last active Nov 9, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.