位置: 编程技术 - 正文

[置顶] (二)Unity5.0新特性------unity内部:内存 和 性能(以及Unity5的升级优化)(置顶怎么折叠起来)

发布时间:2024-02-27
我们的脚本代码里经常会需要访问gameObject引用或者某个组件的引用,最好的方式当然是在脚本Awake的时候就把这些可能访问的东西都缓存下来;如果需要访问临时gameObject实例的某属性或者临时某组件的gameObject实例,在能够确保组件一定存在(可以使用[RequireComponent( typeof(AudioSource ))] 如果没有自动添加移除不了!)的情况下,可以用属性访问,毕竟属性访问比GetComponent要快上一倍,但是如果不能确定组件是否存在,甚至是需要对组件的存在性做判断时,一定不要用对属性访问结果判空的方式,而要用GetComponent,这里面节省的开销不是一点半点。 而在unity5中你是没得选择了,因为没有了属性访问器。 看看下图的 GameObject类的对比。(4.6和5.0版本) unity内部:内存 和 性能Topics• Memory Overview• Garbage Collection• Mesh Internals• Scripting• Job System一、 Memory OverviewMemory Domains内存域• Native (internal)本地 (内部)• Asset Data: Textures, AudioClips, Meshes• Game Objects & Components: Transform, etc..• Engine Internals: Managers, Rendering, Physics, etc..• Managed - Mono• Script objects (Managed dlls)• Wrappers (包装类)for Unity objects: Game objects, assets,components• Native Dlls• User’s dlls and external dlls (for example: DirectX)Native Memory: Internal Allocators本机内存: 内部分配器• Default• GameObject• Gfx 图形和渲染相关• Profiler事件探查器 5.x: 在 Dll 中使用本机的分配器公开的 APIManaged Memory托管的内存• Value types值类型 (bool, int, float, struct, ...) • 在堆栈内存中存在。当从堆栈中移除时取消分配。没有GC。• Reference types (classes) 引用类型 (类)• 在堆上存在,当长时间不再被引用时都由mono/.net GC删除。• 包装为 Unity Objects :• GameObject• Assets : Texture2D, AudioClip, Mesh, …• Components : MeshRenderer, Transform, MonoBehaviourMono Memory Internals• Allocates system heap blocks for internal allocator(为内部分配器分配系统堆块)• Will allocate new heap blocks when needed( 在需要时将分配新的堆块)• Heap blocks are kept in Mono for later use( 堆块保持在Mono中以后使用)• Memory can be given back to the system after a while( 当过一段时间内存可以被交还给系统)•......但是它取决于平台è ,别指望它• Garbage collector cleans up(垃圾回收器清理)• Fragmentation can cause new heap blocks even though memory is not exhausted(碎片可以导致新的堆块,即使内存不耗尽)二、Garbage CollectionUnity Object wrapper(封装)• Some Objects used in scripts have large native backing memory in unity(在unity中在脚本中被使用的一些对象有大批的本机支持内存)• 直到运行Finalizers终结器内存才会释放Mono Garbage Collection•  GC.Collect• Runs on the main thread when(运行在主线程上当:• Mono exhausts the heap space( Mono耗尽堆空间)• Or user calls System.GC.Collect()(或用户调用 System.GC.Collect())•  Finalizers终结器 • Run on a separate thread(运行在一个单独的线程上当:• Controlled by mono(由mono控制• Can have several seconds delay(可以有几秒钟的延迟•  Unity native memory(unity本机内存• Dispose() cleans up internal memory(Dispose () 清理内部内存• Eventually called from finalizer( 最终从finalizer终结器调用• Manually call Dispose() to cleanup(手动调用 Dispose() 清理Garbage Collection• Roots are not collected in a GC.Collect(在GC.Collect上Roots不会被收集)• Thread stacks(线程堆栈)• CPU Registers(CPU 寄存器)• GC Handles (used by Unity to hold onto managed objects) GC 句柄 (使用Unity来守住托管对象)• 静态变量 !!• Collection的时间尺度与托管的堆的大小• 您分配的越多,gets变得越慢GC: Best Practices 最佳做法• Reuse objects . Use object pools(重用对象,使用对象池)• 更喜欢基于堆栈的分配。使用结构而不是类• System.GC.Collect 可以用于触发collection• 手动调用 Dispose立即清理Avoid temp allocations避免临时分配• Don’t use FindObjects or LINQ• Use StringBuilder for string concatenation• Reuse large temporary work buffers(重用大量临时工作缓冲区)• ToString()• .tag 改用 CompareTag() 方法Unity API Temporary Allocations(临时分配)一些例子:• GetComponents<T>• Vector3[] Mesh.vertices• Camera[] Camera.allCameras• foreach•  does not allocate by definition(定义不会分配)•  However, there can be a small allocation, depending on the implementation of .GetEnumerator()• 然而,根据具体的.GetEnumerator()实现可能会有 small有小的分配5.x: 正在研究新的非分配non-allocating版本Memory fragmentation内存碎片• Memory fragmentation is hard to account for(内存碎片很难解释)• Fully unload dynamically allocated content(完全卸载动态分配内容)• 切换到一个空白场景,在next level之前• This scene could have a hook where you may pause the game long enough to sample if there is anything significant in memory• Ensure you clear out variables so GC.Collect will remove as much as possible• 确保你clear变量,GC.Collect将删除尽可能多地• 尽可能避免分配• 重用对象尽可能在scene play下• Clear them out for map load to clean the memory(把它们清理干净memory的地图加载)Unloading Unused Assets卸载未使用的资产• Resources.UnloadUnusedAssets 将触发资产垃圾回收•他是搜索所有unreferenced assets 和卸载他们•他是一个async operation异步操作的 • 它loading 一个 level后在内部被调用• Resources.UnloadAsset 是最好的• 您需要知道到底您需要Unload卸载什么•Unity而不必全部扫描• Unity 5.0: 多线程的asset垃圾回收三、Mesh InternalsMesh Read/Write Option• 它允许您在运行时修改mesh• 如果启用enabled, Mesh的系统副本会保留在内存中• enabled是默认• 在某些情况下,禁用此选项不会减少内存使用量• Skinned meshes皮肤网格• iOSUnity 5.0: disable by default默认情况下是禁用 – under considerationNon-Uniform scaled Meshes非均匀缩放网格我们需要正确转换的顶点法线• Unity 4.x:• 在 CPU 上变换transform网格mesh• 创建数据的额外副本• Unity 5.0• 在 GPU 上缩放Scaled• 不再需要额外的内存Static Batching这是什么?• 它是优化,减少了draw calls 和状态改变的数量他如何使用?• 在player settings + Tag对象为static。如下:它内部如何工作?• Build-time 生成时间: Vertices are transformed to world- space顶点转换为世界空间• Run-time运行时间: Index buffer is created with indices of visible objects索引缓冲区创建与指数的可见对象Unity 5.0:• Re-implemented static batching without copying of index buffers重新实施静态配料而不复制索引缓冲区Dynamic Batching这是什么?• 类似于静态配料,在运行时对象是non-static他如何使用?• 在player settings• no need to tag. it auto-magically works…无需标记。它自动神奇地工作......它内部如何工作?• objects are transformed to world space on the对象转换到世界空间上CPU• 创建临时的 VB & IB• 在一个draw call 中呈现Rendered Unity 5.x: 我们正在考虑使每个平台参数Mesh Skinning根据平台的不同实现:• x: SSE• iOS/Android/WP8: Neon optimizations• D3D/XBoxOne/GLES3.0: GPU• XBox, WiiU: GPU (memexport)• PS3: SPU• WiiU: GPU w/ stream outUnity 5.0: Skinned meshes 通过共享实例之间的索引缓冲区使用较少的内存四、Scripting(重要!)Unity 5.0: Mono• No upgrade不能升级• Mainly bug fixes主要 bug 修复• New tech in WebGL: IL2CPP新科技在 WebGL: IL2CPP• 敬请: 会有关于它的博客文章GetComponent<T> 它要求游戏物体,得到一个指定类型的组件:• The GO contains a list of Components• GO包含Components组件的列表• 每个组件类型被比作 T• 第一个组件的类型 T (或从 T 派生),将返回给调用方• 不太多的开销,但它仍然需要调入本机代码Unity 5.0: Property Accessors属性的访问器• 大多数accessors将在Unity 5.0中被移除• 目的是减少依赖,因此提高模块化• Transform将仍然被保留• 现有的脚本将被转换。示例:in 5.0:Transform Component• this.transform 是和 GetComponent<Transform>() 一样•  transform.position/rotation 需要:• find Transform component• Traverse hierarchy to calculate absolute position遍历层次结构来计算绝对位置• Apply translation/rotation•  transform internally stores the position relative to the parent变换在内部存储的位置是相对于父级• transform.localPosition = new Vector(…) → 简单的赋值• transform.position = new Vector(…)→ costs the same if no father, otherwise it will need to traverse the hierarchy up to transform the abs position into local• transform.position = 新的 Vector(...)→ 如果没有父亲代价是一样的,否则它将需要本地遍历hierarchy得到transform的位置绝对值• 最后,将通过messages通知其他组件 (对撞机、 刚体、 轻型、 相机、)。InstantiateAPI:• Object Instantiate(Object, Vector3, Quaternion);• Object Instantiate(Object);Implementation执行:• 克隆GameObject Hierarchy and Components• Copy Properties复制属性• Awake• Apply new Transform (if provided)适用新的变换 (如果提供)Instantiate cont..ed• Awake 可以是昂贵的• AwakeFromLoad (主线程)• clear states清理状态• internal state caching内部状态缓存• pre-compute预先计算Unity 5.0:• Allocations have been reduced分配已被减少• 一些内部的循环,用于复制的数据进行了优化JIT Compilation(JIT 编译)这是什么?• 在进程中从 CIL 代码生成机器代码,在应用程序运行期间Pros:优点:• 为当前平台,它生成优化的代码Cons:缺点:•  Each time a method is called for the first time, the application will suffer a certain performance penalty because of the compilation• 每次方法被第一次调用时,应用程序因为汇编将受到某些性能处罚 JIT compilation spikespre-JITting怎么样?• RuntimeHelpers.PrepareMethod 是行不通的:....MethodHandle.GetFunctionPointer() 比较好五、Job SystemUnity 5.0: Job System (internal)job system的目标:• 基于多线程的代码 可以很容易地写得很高效率的工作• The jobs should be able to run safely in parallel to script code• jobs工作应该能够安全地平行的运行脚本代码Job System: 它是什么 ?• 它是一个Framework框架,我们打算在现有的和新的子系统中使用的• 我们想要Animation, NavMesh, Occlusion, Rendering,等等尽可能多的并行运行• 这最终将导致更好的性能

推荐整理分享[置顶] (二)Unity5.0新特性------unity内部:内存 和 性能(以及Unity5的升级优化)(置顶怎么折叠起来),希望有所帮助,仅作参考,欢迎阅读内容。

[置顶]
        (二)Unity5.0新特性------unity内部:内存 和 性能(以及Unity5的升级优化)(置顶怎么折叠起来)

文章相关热门搜索词:置顶聊天的人怎么不见了,置顶的朋友圈是所有人可见吗,置顶的朋友圈是所有人可见吗,置顶语录怎么设置,置顶朋友圈,置顶的朋友圈是所有人可见吗,置顶的朋友圈是所有人可见吗,置顶通知要开启吗,内容如对您有帮助,希望把文章链接给更多的朋友!

Unity 实现模拟按键 一直在想,使用模拟按键,也可以实现一些AI操作,具体还没做过,这里使用user.dll在unity里写的一个简单demousingUnityEngine;usingSystem.Collections;usingSystem.Run

Dll的编写 在unity中加载 1.在VS中新建Dll项目2.在头文件中对函数进行声明externCint_declspec(dllexport)testunity();3.在源文件中写函数体inttestunity(){return;}4.生成后成功后,在Debug文件中

【Unity3D基础教程】给初学者看的Unity教程(一):GameObject,Compoent,Time,Input,Physics 作者:王选易,出处:

标签: 置顶怎么折叠起来

本文链接地址:https://www.jiuchutong.com/biancheng/383524.html 转载请保留说明!

上一篇:unity脚本自带函数执行顺序(unity脚本api)

下一篇:Unity 实现模拟按键(unitymol)

  • 详述关税的种类和征收方法
  • 金税盘是什么时候开始使用的
  • 季报需要注意什么
  • 所得税 报表
  • 客票服务主要包括
  • 已经认证的发票可以作废吗
  • 个税申报系统累计收入怎么算
  • 工资没走公账走的私人账户合法吗
  • 个人所得税差额20%政策
  • 房产企业因资金问题退市
  • 研发领用原材料的去向
  • 非税收入一般缴款书可以抵扣吗
  • 代开专票作废退税怎么做账?
  • 委托加工合同如何标注多个地址
  • 企业发票入账冲销流程
  • 物业费增值税是什么意思
  • 农产品初加工免征企业所得税
  • 电信报表系统
  • 一般纳税人注销库存需要补交税吗
  • 销售百分比法计算步骤
  • 固定资产提前报废
  • 餐饮服务行业会计分析
  • 企业销售费用支出怎么算
  • 每月结转本年利润会计分录
  • 哪些行业需要缴纳水利建设基金
  • 发工资时忘记扣个税了怎么做账
  • win11 zen2
  • 怎么在bios设置启动项
  • 没有取得发票的支出可以做成本吗
  • Content-Disposition使用方法和注意事项
  • 作废的普通发票,顾客联拿不回来
  • 会计账簿登记错误怎么办
  • enter an integer
  • vue引入高德地图绘制多边形,编辑多边形
  • laravel批量insert
  • php imagettftext()函数
  • phpweb框架
  • 请假扣款会计分录
  • css给div加边框
  • springboot+chatgpt+chatUI Pro开发智能聊天工具
  • 金税盘白盘怎么分发发票
  • 税控盘总是连接服务器失败
  • 建筑业委托加工合同范本
  • 美元利息结汇是属于外汇
  • 中国移动发票抬头开错了可以重开吗
  • 劳动报酬所得收入额大于4000
  • 在建工程和预付账款怎么转化
  • 固定资产的定义是什么?
  • 企业所得税汇算清缴时间
  • 房租付了没有及时取得发票怎么入账
  • 持有至到期投资减值准备
  • 实收资本印花税最新政策2023年
  • 入账价值有
  • 退回货款给客户怎么做会计分录
  • 小规模纳税人去年免税,红冲后今年开多少税率的发票
  • 预提费用汇算清缴如何调整
  • 其他权益工具投资公允价值变动计入什么科目
  • 税控设备全额抵减
  • win7系统远程
  • win7系统打开计算机的管理出现正在向控制台添加属性
  • vs显示进程已退出
  • 使用移动硬盘快速启动
  • windows怎么安装
  • 如何重设苹果手机的ID密码
  • win8粘贴板
  • css条件语句
  • python基本用法
  • OpenGL图元管理
  • bat文件加密bat解密脚本
  • js表单验证实例怎么写
  • Python中str is not callable问题详解及解决办法
  • android源码分析
  • Unity Batch 对 Vertex Shader 产生影响
  • 沈阳税务局兑奖中心在哪
  • 江苏无锡2023年GDP
  • 税务代收是什么意思
  • app平台怎么投诉商家
  • 十堰市税务大厅
  • 2021税务稽查重点方向
  • 山东省地方税务局历任局长
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号