Unity3d 纯ECS入门
我试图在Unity 2018.3.6f1中开始使用纯ECS,我只是简单地从一个球体预制件朝一个方向移动开始,但我似乎没有创建任何预制件 我有一个带有RenderMesh的空游戏对象预置,该RenderMesh有一个球体网格和一个简单的材质,我还有一个脚本附加到预置:Unity3d 纯ECS入门,unity3d,Unity3d,我试图在Unity 2018.3.6f1中开始使用纯ECS,我只是简单地从一个球体预制件朝一个方向移动开始,但我似乎没有创建任何预制件 我有一个带有RenderMesh的空游戏对象预置,该RenderMesh有一个球体网格和一个简单的材质,我还有一个脚本附加到预置: using System; using Unity.Entities; using UnityEngine; [Serializable] public struct Position : IComponentData {
using System;
using Unity.Entities;
using UnityEngine;
[Serializable]
public struct Position : IComponentData
{
public Vector3 Value;
}
public class BoidPositionComponent : ComponentDataProxy<Position> { }
使用系统;
使用统一实体;
使用UnityEngine;
[可序列化]
公共结构位置:IComponentData
{
公共向量3值;
}
公共类BoidPositionComponent:ComponentDataProxy{}
然后我有了这个转向系统:
using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using Unity.Jobs;
using Unity.Burst;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
public class SteeringSystem : JobComponentSystem
{
[BurstCompile]
struct SteeringJob : IJobProcessComponentData<Position>
{
public float deltaTime;
public void Execute(ref Position position)
{
Vector3 value = position.Value;
value = new Vector3(value.x + deltaTime + 1.0f, value.y, value.z);
position.Value = value;
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
SteeringJob steeringJob = new SteeringJob
{
deltaTime = Time.deltaTime
};
JobHandle jobHandle = steeringJob.Schedule(this, inputDeps);
return jobHandle;
}
}
使用系统集合;
使用System.Collections.Generic;
使用统一实体;
使用团结。工作;
使用统一。爆破;
运用统一性。数学;
使用统一。转换;
使用UnityEngine;
公共类SteeringSystem:JobComponentSystem
{
[BurstCompile]
结构SteeringJob:IJobProcessComponentData
{
公共浮动三角洲;
公共作废执行(参考位置)
{
Vector3值=位置值;
值=新矢量3(值.x+增量时间+1.0f,值.y,值.z);
位置。值=值;
}
}
受保护的重写JobHandle OnUpdate(JobHandle inputDeps)
{
SteeringJob SteeringJob=新SteeringJob
{
deltaTime=Time.deltaTime
};
JobHandle JobHandle=steeringJob.Schedule(此为inputDeps);
返回作业句柄;
}
}
最后,我的场景中有一个空的游戏对象,上面有这个脚本:
using Unity.Entities;
using Unity.Rendering;
using Unity.Collections;
using UnityEngine;
public class ECSWorld : MonoBehaviour
{
public GameObject boidPrefab;
private static EntityManager entityManager;
private static RenderMesh renderMesh;
private static EntityArchetype entityArchetype;
// Start is called before the first frame update
void Start()
{
entityManager = World.Active.GetOrCreateManager<EntityManager>();
entityArchetype = entityManager.CreateArchetype(typeof(Position));
AddBoids();
}
void AddBoids()
{
int amount = 200;
NativeArray<Entity> entities = new NativeArray<Entity>(amount, Allocator.Temp);
entityManager.Instantiate(boidPrefab, entities);
for (int i = 0; i < amount; i++)
{
// Do stuff, like setting data...
entityManager.SetComponentData(entities[i], new Position { Value = Vector3.zero });
}
entities.Dispose();
}
}
使用统一实体;
使用统一渲染;
使用统一。集合;
使用UnityEngine;
公共类ECSWorld:单一行为
{
公共游戏对象boidPrefab;
私有静态实体管理器实体管理器;
私有静态RenderMesh RenderMesh;
私有静态实体原型;
//在第一帧更新之前调用Start
void Start()
{
entityManager=World.Active.GetOrCreateManager();
entityArchetype=entityManager.CreateArchetype(typeof(Position));
AddBoids();
}
void AddBoids()
{
整数金额=200;
NativeArray实体=新的NativeArray(金额、分配器温度);
实例化(boidPrefab,实体);
对于(int i=0;i
但当我运行游戏时,我什么也看不到。它是否应该实例化我的预制件并让它们在屏幕上移动?我错过了什么
谢谢你瑟伦您需要一个渲染器来实际渲染您的boid。您已经创建了一个自定义的
位置
组件,但没有一个系统真正基于它进行渲染。所以,您所要做的就是创建实体并修改内存中的位置
组件(您应该在实体调试器中看到这一点),但由于您没有渲染器,您将无法在屏幕上看到任何内容
目前,我建议在PackageManager中提供。它使用自己的一组组件:
用于三维空间中的位置平移
用于世界空间中的比例Scale
用于世界空间中的旋转旋转
用于将网格作为渲染器的网格(您已经在使用该渲染器)RenderMesh
平移
、缩放
、旋转
和渲染网格
组件添加到您的ECS实体中