当前位置: 首页 > news >正文

佛山 技术支持 骏域网站建设湖南营销型网站建设

佛山 技术支持 骏域网站建设,湖南营销型网站建设,做vlogger的网站有哪些,帮客户做网站平台犯法吗五中介绍学习了Component的使用#xff0c;此篇介绍System和Job常用Api 概述#xff1a; system是游戏在运行时Unity会自动运行#xff0c;分成如下2个接口 SystemBase#xff1a; 是 ECS 0.x 版本#xff08;旧版 ECS#xff09;的主要系统实现方式#xff0c;基于 C…五中介绍学习了Component的使用此篇介绍System和Job常用Api 概述 system是游戏在运行时Unity会自动运行分成如下2个接口 SystemBase 是 ECS 0.x 版本旧版 ECS的主要系统实现方式基于 C# 类继承机制使用传统的面向对象编程风格。它是 Unity 早期推广 ECS 时的核心组件适合熟悉传统 Unity 编程的开发者快速上手。 ISystem 是 ECS 1.x 版本新版 ECS也称为 Burst Systems引入的系统接口基于 C# 接口和ref struct实现专为高性能和内存效率优化。它利用了 C# 8.0 的接口默认方法和ref struct特性是 Unity 未来主推的 ECS 系统实现方式。 适用场景 使用SystemBase的情况 项目仍在使用旧版 ECS0.x或需要兼容旧代码。对性能要求不极端希望快速实现功能。需要与传统 Unity 组件或 MonoBehaviour 交互。 使用ISystem的情况 新项目或追求极致性能的场景如 AAA 游戏、高并发模拟。需要充分利用 Burst 编译器和 DOTS 框架的新特性。愿意学习更底层的 API 和函数式编程风格。 简单理解下SystemBase中可以使用托管对象。但ISystem的性能更高。 示例 SystemBase public partial class EnemySystem: SystemBase {private Transform player;protected override void OnUpdate(){if (player null){player GameObject.Find(Player).transform;return;}float3 p_Pos player.transform.position;var entityMng World.EntityManager;var entities entityMng.GetAllEntities(Allocator.Temp);foreach (var entity in entities){if (entityMng.HasComponentAgentComponent(entity)){var ac entityMng.GetComponentDataAgentComponent(entity);if (ac.state 0){//主角附近var lt entityMng.GetComponentDataLocalTransform(entity);lt.Position p_Pos new float3(UnityEngine.Random.Range(-25,25), -0.4f, UnityEngine.Random.Range(-25,25));entityMng.SetComponentData(entity, lt);ac.state 1;entityMng.SetComponentData(entity, ac);}}}} } ISystem: [UpdateBefore(typeof(ActiveAnimationSystem))] partial struct ChangeAnimationSystem : ISystem {[BurstCompile]public void OnCreate(ref SystemState state) {state.RequireForUpdateAnimationDataHolder();}[BurstCompile]public void OnUpdate(ref SystemState state) {AnimationDataHolder animationDataHolder SystemAPI.GetSingletonAnimationDataHolder();ChangeAnimationJob changeAnimationJob new ChangeAnimationJob {animationDataBlobArrayBlobAssetReference animationDataHolder.animationDataBlobArrayBlobAssetReference,};changeAnimationJob.ScheduleParallel();}} Job 是可并行执行的自包含计算单元通常处理一组独立的数据如组件数组。 Job可以在System内部创建并且分配到不同的线程进行调用从而实现CPU的高效利用。 Job在System下的示例 [BurstCompile] public partial struct UnitMoverJob : IJobEntity {public float deltaTime;public void Execute(ref LocalTransform localTransform, ref UnitMover unitMover, ref PhysicsVelocity physicsVelocity) {float3 moveDirection unitMover.targetPosition - localTransform.Position;float reachedTargetDistanceSq UnitMoverSystem.REACHED_TARGET_POSITION_DISTANCE_SQ;if (math.lengthsq(moveDirection) reachedTargetDistanceSq) {// Reached the target positionphysicsVelocity.Linear float3.zero;physicsVelocity.Angular float3.zero;unitMover.isMoving false;return;}unitMover.isMoving true;moveDirection math.normalize(moveDirection);localTransform.Rotation math.slerp(localTransform.Rotation,quaternion.LookRotation(moveDirection, math.up()),deltaTime * unitMover.rotationSpeed);physicsVelocity.Linear moveDirection * unitMover.moveSpeed;physicsVelocity.Angular float3.zero;}} 在System里运行 [BurstCompile]public void OnUpdate(ref SystemState state) {UnitMoverJob unitMoverJob new UnitMoverJob {deltaTime SystemAPI.Time.DeltaTime,};unitMoverJob.Run();//单线程unitMoverJob.ScheduleParallel();//多线程 } 同时运行10000个单位截图所示运行速度 那问题来了什么情况下我们要使用IJob什么情况下我们在System内部写逻辑即可。 1.直接在 System 内部写逻辑 实现方式 在SystemBase.OnUpdate()或ISystem.OnUpdate()中直接编写代码使用Entities.ForEach或循环遍历实体。 性能瓶颈 通常在主线程执行阻塞渲染和输入处理。 即使使用ScheduleParallel()也依赖于传统的Job接口存在一定的调度开销。 使用 IJobEntity 实现方式 定义独立的IJobEntity结构体通过ScheduleParallel()并行执行自动处理实体遍历。 性能优势 并行效率更高基于Chunk级并行减少缓存未命中。 内存访问优化直接操作ArchetypeChunk数据局部性更好。 更低的调度开销Unity 自动生成高效的调度代码。 Burst 兼容性更好结构体形式更易被 Burst 编译为优化的本地代码。 2. 适用场景 优先使用 IJobEntity 的情况 处理大量实体如粒子系统、寻路单元。 计算密集型逻辑如物理模拟、复杂 AI。 需要充分利用多核 CPU避免主线程阻塞。 追求极致性能愿意编写更结构化的代码。 可直接在 System 内写逻辑的情况 少量实体如 UI 相关实体或简单初始化逻辑。 需要频繁与主线程数据交互如读取输入或 Unity API。 原型开发阶段快速验证功能。 逻辑简单且无需并行如单例组件处理。 3. 性能优化建议 结合 Burst 编译 为IJobEntity和System都添加[BurstCompile]但IJobEntity的结构体形式更易被 Burst 优化。 减少主线程同步 避免在System中频繁调用JobHandle.Complete()而IJobEntity的Run()方法仅用于简单场景。 数据布局优化 IJobEntity默认基于Chunk处理天然符合 ECS 的数据布局减少缓存未命中。 避免托管代码 IJobEntity内部应使用NativeContainer和Unity.Mathematics避免托管对象如ListT。 4. 总结 特性 直接在 System 内部写逻辑 使用 IJobEntity 执行线程 主线程或工作线程 工作线程池并行 性能上限 中等依赖传统 Job 高专为 ECS 优化 代码复杂度 低直观 中需定义独立结构体 适用场景 少量实体、简单逻辑 大量实体、计算密集型任务 结论在 Unity ECS 中IJobEntity是处理大量实体的首选方案而直接在System内写逻辑适合快速迭代或简单场景。随着 Unity DOTS 框架的发展IJobEntity的性能优势会愈发明显。 下面的常见API记录比较散都是变学习变遇到的都记录下 1.禁用某个组件 entityManager.SetComponentEnabled 示例需要注意被禁用的组件需要继承IEnableableComponent public struct ExecuteMainThread : IComponentData,IEnableableComponent{}[BurstCompile]public void OnUpdate(ref SystemState state){EntityManager entityManager World.DefaultGameObjectInjectionWorld.EntityManager;EntityQuery entityQuery new EntityQueryBuilder(Allocator.Temp).WithAllExecuteMainThread().Build(entityManager);NativeArrayEntity entityArray entityQuery.ToEntityArray(Allocator.Temp);//NativeArrayExecuteMainThread selectedArray entityQuery.ToComponentDataArrayExecuteMainThread(Allocator.Temp);Debug.Log(ComponentCount: entityArray.Length);if (entityArray.Length 0){for (int i 0; i entityArray.Length; i){entityManager.SetComponentEnabledExecuteMainThread(entityArray[i],false);}}} 2.查询某entity带有附件条件Component WithDisabled某个实体禁用了某个组件 WithPresent只要这个组件存在于某个实体就会被统计 代码示例 public partial struct MyTestSystem: ISystem {[BurstCompile]public void OnCreate(ref SystemState state){state.RequireForUpdateExecuteMainThread();}[BurstCompile]public void OnUpdate(ref SystemState state){foreach (RefRWA a in SystemAPI.QueryRefRWA().WithDisabledB()){Debug.Log(Has A DisableB:);}foreach (RefRWA a in SystemAPI.QueryRefRWA().WithPresentB()){Debug.Log(Has A With Has B:);}} }public struct A : IComponentData {}public struct B : IComponentData, IEnableableComponent {} 3.获取某Component的Entity示例 使用WithEntityAccess 示例 [BurstCompile]public void OnUpdate(ref SystemState state){foreach ((RefRWA a,Entity entity) in SystemAPI.QueryRefRWA().WithDisabledB().WithEntityAccess()){Debug.Log(Has A DisableB: entity.Index);}} 4.查询entity带有某组件二种方式 使用EntityQuery一种是WithAll,另一种CreateEntityQuery 代码示例 private void QueryFunction(){EntityManager entityManager World.DefaultGameObjectInjectionWorld.EntityManager;EntityQuery entityQuery new EntityQueryBuilder(Allocator.Temp).WithAllA().Build(entityManager);NativeArrayEntity entityArray entityQuery.ToEntityArray(Allocator.Temp);EntityQuery entityQuery2 entityManager.CreateEntityQuery(typeof(A),typeof(B));} 特性WithAllCreateEntityQueryAPI 类型查询修饰符链式调用独立方法返回 EntityQuery查询构建方式声明式链式调用命令式显式创建性能优化适合简单查询自动优化适合复杂查询可手动缓存和复用灵活性只能在 Entities.ForEach 中使用可独立使用支持更多查询选项查询条件仅支持 WithAll、WithAny 等支持 All、Any、None 组合适用场景快速编写一次性查询复杂查询、需要频繁复用的查询 5.Dots下获取单例 使用SystemAPI.GetSingleton 代码示例 PhysicsWorldSingleton physicsWorldSingleton SystemAPI.GetSingletonPhysicsWorldSingleton(); CollisionWorld collisionWorld physicsWorldSingleton.CollisionWorld; 6.删除和添加实体 private void AddOrDesEntity(SystemState state){EntityManager entityManager World.DefaultGameObjectInjectionWorld.EntityManager;EntityQuery entityQuery new EntityQueryBuilder(Allocator.Temp).WithAllA().Build(entityManager);NativeArrayEntity entityArray entityQuery.ToEntityArray(Allocator.Temp);//获取CommandBufferEntityCommandBuffer entityCommandBuffer SystemAPI.GetSingletonEndSimulationEntityCommandBufferSystem.Singleton().CreateCommandBuffer(state.WorldUnmanaged);//删除EntityentityCommandBuffer.DestroyEntity(entityArray[0]);//创建EntityEntity entity entityCommandBuffer.CreateEntity();//添加组件entityManager.AddComponentA(entity);//删除组件entityManager.RemoveComponentA(entity);} 7.验证实体的有效性 使用Exsit验证Entity使用HasComponent验证实体的组件 8.某System运行OnUpdate需要条件 用RequireForUpdateXX来判定 [BurstCompile]public void OnCreate(ref SystemState state){state.RequireForUpdateExecuteMainThread();} 伴有条件组件启用 WithOptions(EntityQueryOptions.XX | EntityQueryOptions.XX) namespace Unity.Physics.Authoring {/// summary/// Custom physics proxy baking system/// /summary[UpdateInGroup(typeof(PostBakingSystemGroup))][WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]public partial class CustomPhysicsProxyBakingSystem : SystemBase{protected override void OnUpdate(){var transformFromEntity GetComponentLookupLocalTransform();var physicsMassFromEntity GetComponentLookupPhysicsMass();var physicsColliderFromEntity GetComponentLookupPhysicsCollider();foreach (var (driver, entity) in SystemAPI.QueryRefRWCustomPhysicsProxyDriver().WithEntityAccess().WithOptions(EntityQueryOptions.IncludePrefab | EntityQueryOptions.IncludeDisabledEntities)){transformFromEntity[entity] transformFromEntity[driver.ValueRW.rootEntity];physicsMassFromEntity[entity] PhysicsMass.CreateKinematic(physicsColliderFromEntity[driver.ValueRW.rootEntity].MassProperties);physicsColliderFromEntity[entity] physicsColliderFromEntity[driver.ValueRW.rootEntity];}}} } 9.EntityStorageInfoLookUp 在 Unity DOTSData-Oriented Technology Stack里EntityStorageInfoLookup 是一个非常实用的查找类型其主要功能是获取实体在内存中的存储信息。借助这些存储信息开发者能够深入了解实体的内存布局还可以基于实体所在的块或存储桶来实现高效的分组处理。 核心功能 获取实体块信息可以查询某个实体所在的 ArchetypeChunk。定位存储桶能够确定实体处于哪个存储桶Bucket中。内存布局分析有助于分析实体在内存中的排列方式。优化处理逻辑可以基于实体的存储位置对处理逻辑进行分组优化. 举例在Job里不允许使用系统API如图 平替后 10.ComponentLookUp 在 Unity DOTSData-Oriented Technology Stack中ComponentLookupT 是一种高效的查找类型用于在作业中快速访问和修改组件数据。与直接在查询中声明组件相比它提供了更大的灵活性允许你在运行时动态访问组件甚至可以访问查询中未显式声明的组件。 使用方式跟字典差不多Key为Entity Value为T对用的Component 示例 public partial struct MyTestSystem: ISystem {private ComponentLookupB m_AComponentLookup;[BurstCompile]public void OnCreate(ref SystemState state){//state.RequireForUpdateExecuteMainThread();m_AComponentLookup state.GetComponentLookupB(true);}[BurstCompile]public void OnUpdate(ref SystemState state){m_AComponentLookup.Update(ref state);CheckJob resetTargetJob new CheckJob {m_EntityStorageInfoLookup m_AComponentLookup,};resetTargetJob.ScheduleParallel();foreach ((RefRWA a,Entity entity) in SystemAPI.QueryRefRWA().WithDisabledB().WithEntityAccess()){Debug.Log(Has A DisableB: entity.Index);}}} [BurstCompile] public partial struct CheckJob:IJobEntity {public ComponentLookupB m_EntityStorageInfoLookup;public void Execute(ref A target) {//A的targetEntity需要有B组件if (m_EntityStorageInfoLookup.HasComponent(target.targetEntity)){B bb m_EntityStorageInfoLookup[target.targetEntity];bb.bbValue 100;}} }public struct A : IComponentData {public Entity targetEntity; }public struct B : IComponentData, IEnableableComponent {public int bbValue; } 11.Job内部组件访问限制问题 这么写Unity会报错一个JOB不允许使用2个同样组件 多个Job可以安全访问同一个组件容器 12.骨骼动画 Unity的Dots系统原生不允许使用骨骼动画可以使用平替的插件来弄也可以自己把一些Mesh烘焙到一个序列化文件中。运行时逐帧换Mesh来解决。类似下列这种 13.Dots版的GetComponent 使用SystemApi也可以加RW,RO这样的修饰符 14.Unity烘焙dynamic和none dynamic是Unity会自动帮你添加好组件比如localtrasnformmesh等 none是只有一个空实体可自行添加组件
http://www.dnsts.com.cn/news/176693.html

相关文章:

  • 定制高端网站建设公司杭州优化公司哪家好
  • 国外过期域名查询网站外贸网站建设深圳
  • 比利时网站后缀四种营销模式
  • 南昌网站建设好企业网站月嫂网站建设
  • 单位网站建设的报告母婴网站建设
  • 基于淘宝联盟的返利网站怎么做整站优化昌吉可以吗?
  • 盘锦网站建设多少钱公司网站建设技术的发展
  • 网站建设 重点网站文章要求
  • 烟台网站seo外包牛商网是干啥的
  • 做海报有什么素材网站知乎宜宾seo网站建设
  • 北京清控人居建设集团网站织梦cms首页模板文件是哪个
  • php简易购物网站开发网络哪个公司好
  • 本地做的网站怎么解析到域名接计设做的网站
  • 做一个中英文网站的价格西乡塘区网站建设
  • 东莞网站推广企业苏州建站公司兴田德润i网址多少
  • 枣庄手机网站制作360导航下载
  • 百度网站是怎么建设的免费ppt网站 不要收费的
  • 东莞建站模板重庆今天刚刚发生的新闻事件
  • 潍坊网站开发weifangwangluo搭建网站开发网站环境
  • 自己搭建公司网站成功备案的网站增加域名
  • 网站维护公司广州前端工程师考证报名
  • 网站意见反馈源码无锡网站建设818gx
  • 可以用什么网站做mc官方南京企业免费建站
  • 视频号服务商入驻条件seo软件视频教程
  • 网站开发商最好的做网站的公司
  • 信息部网站建设工作计划武进网站建设怎么样
  • 郑州网站托管公司绍兴seo
  • .net建设网站步骤详解全国卫生计生机构建设管理系统网站
  • 网站生成器下载网页微信版文件传输
  • 怎么可以做网站的网站网页制作培训班课程