Unity DOTS ECS 0.50版本,海浪Demo
生活随笔
收集整理的這篇文章主要介紹了
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",注意
詳情查看文檔:
https://docs.unity3d.com/Packages/com.unity.rendering.hybrid@0.50/manual/creating-a-new-hybrid-renderer-project.html
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;elseDestroy(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 ECS 0.50版本,海浪Demo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Backend TkAgg is int
- 下一篇: PHP 导出数据到Excel