Unity DOTS ECS 0.50版本,海浪Demo

目前更新了新版ECS,移步新文章

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",

注意

  1. 需要urp或hdrp,Hybrid Renderer升级到V2,且不支持内置渲染管线;
    详情查看文档:
    https://docs.unity3d.com/Packages/[email protected]/manual/creating-a-new-hybrid-renderer-project.html
  2. Hybrid Renderer新版本需要设置RenderMesh的layerMask,不然屏幕上不显示物体;
  3. Burst启用Enable Compilation后,不支持单例模式,需要关闭;

Demo脚本

新建场景,挂载StartBehaviour.cs,直接运行

Component

HeightComponent.cs

using Unity.Entities;

namespace Component
{
    public struct HeightComponent : IComponentData
    {
        public float InitiateHeight;
        public float MaxHeight;
    }
}

SpeedComponent.cs

using Unity.Entities;

namespace Component
{
    public struct SpeedComponent : IComponentData
    {
        public float Speed;
    }
}

System

RotationSystem.cs

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();
    }
}

StartBehaviour.cs

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
        });
    }
}

你可能感兴趣的:(unity,DOTS,unity)