【无标题】

国内2023年6月10日更新了Unity2022的LTS版本,ECS 也发布到1.0.10版本,之前的海浪demo需要升级一下API;
旧地址

【无标题】_第1张图片

安装

manifest.json添加下面三行

"com.unity.entities.graphics": "1.0.10",

注意

  1. 部分注意事项在旧地址有提及,不再赘述;

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

Baker

public class MyAuthoring : MonoBehaviour
    {
        // [SerializeField] public int count = 100;
        public Mesh unitMesh;
        public Material unitMaterial;
    }
public class MyBaker : Baker<MyAuthoring>
    {
        public int count = 2;
        public float noiseRange = 20f;
        public float noiseValue = 2f;
        public float speed = 4f;
        public float maxHeight = 1f;
        public Mesh unitMesh;
        public Material unitMaterial;

        private EntitiesGraphicsSystem m_RendererSystem;
        private EntityManager _entityManager;

        private MaterialMeshInfo materialMeshInfo;

        public override void Bake(MyAuthoring authoring)
        {
            DefaultWorldInitialization.Initialize("TestWorld");
            _entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
            m_RendererSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged<EntitiesGraphicsSystem>();
            var archetype = _entityManager.CreateArchetype(
                typeof(LocalToWorld)
            );
            materialMeshInfo = new MaterialMeshInfo()
            {
                MeshID = m_RendererSystem.RegisterMesh(unitMesh),
                MaterialID = m_RendererSystem.RegisterMaterial(unitMaterial),
            };

            for (int i = 0; i < count; i++)
            {
                for (int j = 0; j < count; j++)
                {
                    var pos = new float3(i * 1, 0, j * 1);
                    CreateEntity(archetype, pos);
                }
            }
        }

        private void CreateEntity(EntityArchetype archetype, float3 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);

            CreateGameObject(ref entity, pos);

            _entityManager.AddComponentData(entity, new HeightComponent()
            {
                InitiateHeight = f,
                MaxHeight = maxHeight,
            });
            _entityManager.AddComponentData(entity, new SpeedComponent()
            {
                Speed = speed
            });

            _entityManager.AddComponentData(entity, new MyOwnColor()
            {
                Value = new float4(),
                Value2 = 1,
            });
        }

        private void CreateGameObject(ref Entity entity, float3 pos)
        {
            LocalToWorld toWorldTransform = _entityManager.GetComponentData<LocalToWorld>(entity);
            toWorldTransform.Value = float4x4.Translate(pos);

            _entityManager.SetComponentData(entity, toWorldTransform);

            _entityManager.AddComponentData(entity, materialMeshInfo);

            var renderMeshDescription = new RenderMeshDescription()
            {
                FilterSettings = new RenderFilterSettings()
                {
                    RenderingLayerMask = 1,
                    ShadowCastingMode = ShadowCastingMode.On,
                    ReceiveShadows = true,
                }
            };

            var renderMeshArray = new RenderMeshArray(new []{unitMaterial}, new []{unitMesh});

            RenderMeshUtility.AddComponents(entity, _entityManager, renderMeshDescription, renderMeshArray, materialMeshInfo);
        }
    }

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()
    {
        Entities.ForEach((ref LocalToWorld localToWorldTransform, in HeightComponent heightComponent,
            in SpeedComponent speedComponent) =>
        {
            var elapsedTime = SystemAPI.Time.ElapsedTime;
            var heightComponentMaxHeight =
                (float) math.sin((elapsedTime + heightComponent.InitiateHeight) * speedComponent.Speed) *
                heightComponent.MaxHeight;
            var valuePosition = localToWorldTransform.Position;
            valuePosition.y = heightComponentMaxHeight;
            localToWorldTransform.Value = float4x4.Translate(valuePosition);
        }).WithoutBurst().Run();
    }
}

StartBehaviour.cs

using UnityEngine;

public class StartBehaviour : MonoBehaviour
{
    public MyAuthoring Obj;

        [SerializeField] public int count = 2;
        [SerializeField] public float noiseRange = 20f;
        [SerializeField] public float noiseValue = 2f;
        [SerializeField] public float speed = 4f;
        [SerializeField] public float maxHeight = 1f;
        [SerializeField] public Mesh unitMesh;
        [SerializeField] public Material unitMaterial;

        private void Start()
        {
            MyBaker baker = new MyBaker()
            {
                count = count,
                noiseRange = noiseRange,
                noiseValue = noiseValue,
                speed = speed,
                maxHeight = maxHeight,
                unitMesh = unitMesh,
                unitMaterial = unitMaterial,
            };
            baker.Bake(Obj);
        }
}

你可能感兴趣的:(DOTS,unity,游戏引擎,DOTS,ECS)