前言:我爲什麼來學習的初衷有一點就是爲了這個,假如我一次性生成一千個物體你說會卡不卡,如果用了DOTS可想而知(我還沒試。嘿嘿)
首先我們創建一個空物體掛載一個創建的腳本
* Scene----GameObject 首先GameObject是存在Scene場景下的
* World--Entity Entity是存在世界座標下的,兩者雖然有不同但是是混合在一起的
首先調用創建方法我們先測試一下是否創建出來了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
public class CreateNewEntity : MonoBehaviour
{
/*
* Scene----GameObject 首先GameObject是存在Scene場景下的
* World--Entity Entity是存在世界座標下的,兩者雖然有不同但是是混合在一起的
*
*/
private EntityManager em;
void Start()
{
em = World.DefaultGameObjectInjectionWorld.EntityManager;
CreatyMyEntity();
}
private void CreatyMyEntity()
{
em.CreateEntity();
}
}
現在根據打印輸出來看物體是創建出來了,但是隻是一個空物體,最右側面板也沒有任何組件 沒有任何材質和Mesh,我們現在要做的就是給他先添加上組件試一試
添加組件腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Rendering;
public class CreateNewEntity : MonoBehaviour
{
/*
* Scene----GameObject 首先GameObject是存在Scene場景下的
* World--Entity Entity是存在世界座標下的,兩者雖然有不同但是是混合在一起的
*
*/
private EntityManager em;
[SerializeField]
private Mesh myMesh;
[SerializeField]
private Material myMaterial;
void Start()
{
em = World.DefaultGameObjectInjectionWorld.EntityManager;
CreatyMyEntity();
}
private void CreatyMyEntity()
{
//原型
EntityArchetype ea = em.CreateArchetype(
typeof(Translation),
typeof(RenderMesh),
typeof(RenderBounds),
typeof(LocalToWorld)
);
Entity myEntity = em.CreateEntity(ea);
em.AddSharedComponentData(myEntity, new RenderMesh
{
mesh = myMesh,
material = myMaterial
}
);
}
}
然後把mesh 和材質掛上 噹噹噹當!!
是不是這樣就出來了那,人頭娃娃
小夥伴們可以生成一千個或者一萬個試一試哦!!!