ECS1.0.10海浪demo更新
国内2022-3-17更新了ECS 0.50版本,目前unity2020.3版本可以用,2021会报错;
manifest.json添加下面三行
"com.unity.entities": "0.50.0-preview.24",
"com.unity.rendering.hybrid": "0.50.0-preview.24",
"com.havok.physics": "0.50.0-preview.24",
新建场景,挂载StartBehaviour.cs,直接运行
using Unity.Entities;
namespace Component
{
public struct HeightComponent : IComponentData
{
public float InitiateHeight;
public float MaxHeight;
}
}
using Unity.Entities;
namespace Component
{
public struct SpeedComponent : IComponentData
{
public float Speed;
}
}
using Component;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
public partial class RotationSystem : SystemBase
{
protected override void OnUpdate()
{
var elapsedTime = Time.ElapsedTime;
Entities.ForEach((Entity entity,
ref Translation translation,
in Rotation rotation,
in HeightComponent heightComponent,
in SpeedComponent speedComponent) =>
{
heightComponent.MaxHeight;
translation.Value.y =
(float) math.sin((elapsedTime + heightComponent.InitiateHeight) * StartBehaviour.Instance.speed) *
StartBehaviour.Instance.maxHeight;
// }).WithoutBurst().Run();
// }).Schedule();
}).ScheduleParallel();
}
}
using System;
using Component;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
public class StartBehaviour : MonoBehaviour
{
public static StartBehaviour Instance { get; private set; }
[SerializeField] public int count = 100;
[SerializeField] public float noiseRange = 20f;
[SerializeField] public float noiseValue = 2f;
[SerializeField] public float speed = 4f;
[SerializeField] public float maxHeight = 1f;
[SerializeField] private Mesh unitMesh;
[SerializeField] private Material unitMaterial;
private EntityManager _entityManager;
private void Awake()
{
if (null == Instance)
Instance = this;
else
Destroy(gameObject);
DontDestroyOnLoad(this);
}
void Start()
{
_entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var archetype = _entityManager.CreateArchetype(
typeof(Rotation),
typeof(Translation),
typeof(HeightComponent),
typeof(SpeedComponent),
typeof(RenderBounds),
typeof(RenderMesh),
typeof(LocalToWorld)
);
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count; j++)
{
CreateEntity(archetype, new Vector3(i * 1, 0, j * 1));
}
}
}
void CreateEntity(EntityArchetype archetype, Vector3 pos)
{
float2 float2 = new float2(pos.x / noiseRange, pos.z / noiseRange);
float f = noise.snoise(float2) / noiseValue;
pos.y = f;
Entity entity = _entityManager.CreateEntity(archetype);
_entityManager.AddSharedComponentData(entity, new RenderMesh
{
mesh = unitMesh,
material = unitMaterial,
layerMask = 1,
});
_entityManager.AddComponentData(entity, new Translation()
{
Value = pos
});
_entityManager.AddComponentData(entity, new HeightComponent()
{
InitiateHeight = f,
MaxHeight = maxHeight,
});
_entityManager.AddComponentData(entity, new SpeedComponent()
{
Speed = speed
});
}
}