栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity DOTS ECS 0.50版本,海浪Demo

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity DOTS ECS 0.50版本,海浪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/com.unity.rendering.hybrid@0.50/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
        });
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/906516.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号