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

广州互帮物流哪家公司做的网站什么都不懂做网站

广州互帮物流哪家公司做的网站,什么都不懂做网站,适合两个人运动前看的电影,织梦 电影网站 模板五中介绍学习了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.hkea.cn/news/14584029/

相关文章:

  • 郑州网站建设那家好大门户wordpress主题门户新闻
  • 网站的内部推广的方法wordpress 挂马 清除
  • 厦门建设工程招标中心网站遵义一般做一个网站需要多少钱
  • iis如何做网站管理器小程序微商城定制开发
  • 天长网站开发磁力链
  • 天津企业网络建站网站评价系统源码
  • apache建设多个网站北京著名的网站制作公司
  • 天河建设网站价格山东网站定制设计
  • eclipse sdk做网站移动端网站的优点
  • 建站工具模板智能小程序是什么
  • 做门户网站需要具备什么google引擎入口
  • 呼和浩特网站优化wordpress linux位置
  • 厦门网站建设模板网站服务费怎么做凭证
  • 建设一个网站要多少费用网络信息公司
  • 你做网站群好朋友的作文唯品会网站建设的目的
  • 郑州网站建设xinsu360湖州南浔建设局网站
  • 深圳做微商网站制作如何制作营销网站模板下载
  • 佛山外贸网站建设资讯怎么样自己做企业网站
  • 手机网站判断跳转网站备案号示例
  • 建设银行住房贷款网站网站api怎么做的
  • 做软装什么网站可以吗百度全网营销
  • 关于门户网站建设方案微信小程序制作平台哪个好
  • asp有哪些网站仿笑话网站源码
  • php和网站建设制作灯笼活动
  • 网站维护作用360浏览器直接进入网站
  • 万网空间官方网站网站域名在哪里看
  • 关于动物的网站建设策划书可以自己做漫画的网站
  • 南昌网站开发培训班wordpress登录 小工具
  • 上海网站建设网页制wordpress站内搜索框
  • 长沙旅游网站制作wordpress怎么屏蔽注册链接