unity 场景优化_Unity优化:您的场景层次正在抢劫您

unity 场景优化

You've optimized all of the low-hanging fruits of your game. Except that you didn't. You missed a sneaky, non-so-obvious spot: optimizing your Unity Scene Hierarchy.

您已经优化了游戏中所有低调的成果。 除了你没有。 您错过了一个偷偷摸摸,不太明显的地方:优化Unity场景层次结构。

What's the crack with the hierarchy now?

现在,层次结构有何缺陷?

I want to show you something.

我给你看个东西。

[The original entry with its formatting can be found at Unity Optimization: Your Scene Hierarchy is Robbing you Performance]

[其格式的原始条目可以在Unity Optimization中找到:您的场景层次结构正在抢占您的性能 ]

Launch Unity and open your game project. Then, run your game on your target device and attach the Unity Profiler to it. Record a few frames during gameplay.

启动Unity并打开您的游戏项目。 然后,在目标设备上运行游戏,并将Unity Profiler附加到该设备上。 在游戏过程中记录几帧。

In the Unity Profiler, look for the following unpleasant profiler markers:

在Unity Profiler中,查找以下令人讨厌的Profiler标记

You didn't find any?

没找到吗?

Keep looking. I'm sure you'll find at least one.

继续寻找。 我相信您至少会找到一个。

They'll spawn when you're about to give up. As soon as you mouse over the close button of the Profiler, they'll show up.

当您要放弃时,它们会生成。 只要将鼠标悬停在Profiler的关闭按钮上,它们就会显示出来。

Found them already?

已经找到他们了吗?

If so, it's your lucky day.

如果是这样,那是您的幸运日。

I discovered those in my previous project as well and I learned how to land a final blow to them.

我也在之前的项目中发现了这些,并且学会了如何对它们进行最后的打击

I know the evil behind them...

我知道他们身后的邪恶...

Do you want to know the performance secrets of Unity Scene Hierarchies?

您是否想知道Unity场景层次结构性能秘诀

Quick Navigation (opens in a new tab)

快速导航(在新标签中打开)

Level 1 Developer: A Typical Unity Scene Hierarchy

1级开发人员:典型的Unity场景层次结构

Level 2 Developer: An Optimized Unity Scene Hierarchy

2级开发人员:优化的Unity场景层次结构

  The Gamedev Guru's Golden Rules of a Clean Unity Scene Hierarchy

Gamedev Guru的干净Unity场景层次结构的黄金法则

Level 3 Developer: The FAP Hierarchy Tool

3级开发人员:FAP层次结构工具

  The Gamedev Guru's Hierarchy Score

Gamedev Guru的等级分数

1级开发人员:典型的Unity场景层次结构 (Level 1 Developer: A Typical Unity Scene Hierarchy)

I still remember the first Unity project I worked on, just slightly over a decade ago.

我仍然记得十年前刚开始从事的第一个Unity项目。

I was not sure what the difference between game objects and components were.

我不确定游戏对象和组件之间有什么区别。

But that didn't matter. I created games anyway.

但这没关系。 我还是创造了游戏。

Deep Unity Scene Hierarchy

Deep Unity Scene Hierarchy

深度统一场景层次结构

Was it uncommon to see a messy unity scene hierarchy like this?

看到这样一个混乱的统一场景层次结构并不罕见吗?

Actually, yes. Because it used to be worse than that.

其实,是。 因为它曾经比那更糟。

Interestingly enough, you can get away with this kind of hierarchies most of the time.

有趣的是,大多数时候您可以摆脱这种层次结构。

But you'll suffer. You'll pay the price later on.

但是你会受苦的。 稍后您将支付价格。

Luckily, before I started developing games professionally, I learned to appreciate more structured hierarchies.

幸运的是,在我开始专业开发游戏之前,我学会了欣赏更加结构化的层次结构。

A great scene hierarchy structure can be your best friend. Structure makes development and collaboration much easier.

一个好的场景层次结构可以成为您最好的朋友。 结构使开发和协作变得更加容易。

It doesn't have to be the best, whatever that means. It's enough for it to be good.

不一定意味着它是最好的。 做好就足够了。

But bear with me here. This is important.

但是在这里忍受我。 这个很重要。

Having a cluttered unity scene hierarchy is dangerous and will get you into trouble. This is so because it's highly unlikely you'll directly notice its side effects.

混乱的统一场景层次结构很危险,并且会给您带来麻烦。 之所以如此,是因为您极不可能直接注意到它的副作用。

There'll be a creepy ghost chasing you, pulling you back, slowing you down. Sometimes you think you hear something, but when you look back, there's nothing. So you get used to walking slower.

会有一个令人毛骨悚然的幽灵追着你,把你拉回来,让你减速。 有时您认为自己听到了一些声音,但是当您回头看时,什么也没有。 因此,您习惯了慢走。

I learned to diagnose these side effects over the years. And the main problem is that the side effects of a messy unity scene hierarchy are not obvious to spot.

这些年来,我学会了诊断这些副作用。 而且主要的问题是,混乱的统一场景层次结构的副作用并不明显。

You'll see the profiler and wonder what some markers mean. Their names are often cryptic… what secrets do they hide?

您将看到分析器,并想知道一些标记的含义。 他们的名字通常是神秘的……他们隐藏了什么秘密?

The only thing you can be sure of is this:

您唯一可以确定的是:

Mysterious markers tell you something's worth investigating

神秘的标记告诉您值得研究的东西

When you and I talk about structuring your scenes better, we usually discuss about increasing clarity and reducing maintenance cost.

当您和我谈论更好地构建场景时,我们通常会讨论提高清晰度和降低维护成本。

But performance? Just like my cat gitignores all the toys she has around my flat, everybody forgets about performance when it comes to the Unity Scene Hierarchy.

但是性能呢? 就像我的猫不顾她的公寓周围拥有的所有玩具一样,当涉及到Unity Scene Hierarchy时,每个人都会忘记性能。

Let's analyze the previously shown unity scene hierarchy. It contains 2,000 rotating spheres nested on top of each other.

让我们分析先前显示的统一场景层次。 它包含2,000个相互嵌套的旋转球体

What does the profiler say about it? Look, don't be scared.

探查者对此有何评论? 看,不要害怕。

Unity Scene Hierarchy Optimization-Deep-Hierarchy Profiler Overview

Unity场景层次优化-深度层次分析器概述

That's pretty bad for a simple prefab and a rotation script applied to a built-in sphere.

对于一个简单的预制件和应用于内置球体的旋转脚本来说,这是非常糟糕的。

As you can see, Physics.SyncColliderTransform and UpdateRendererBoundingVolumes are taking a huge portion of your frame budget.

如您所见, Physics.SyncColliderTransformUpdateRendererBoundingVolumes占用了框架预算的很大一部分。

But let's not stop analyzing there, because I'm quite curious about this case.

但是,我们不要停止在那里的分析,因为我对这种情况非常好奇。

Let's dig further. I hope you saved some 4G data this month for this impressive image.

让我们进一步探讨。 我希望您本月能为这个令人印象深刻的图像保存一些4G数据。

Unity Scene Hierarchy Optimization-Deep-Hierarchy Profiler Details

Unity场景层次优化-深度层次分析器详细信息

Here lies the key of the article.

这就是本文的重点。

Unoptimized hierarchies causes two BIG problems in Unity.

未优化的层次结构在Unity中导致两个BIG问题。

  • Deep hierarchies do not allow the job system to work efficiently in parallel.

    深层次结构不允许作业系统并行高效地工作。
  • Nested hierarchies cause bottlenecks in several subsystems within Unity.

    嵌套层次结构会导致Unity内多个子系统出现瓶颈。

Remember my words...

记住我的话...

Because this applies to all types of dynamic objects and especially user interfaces.

因为这适用于所有类型的动态对象 ,尤其是用户界面。

In case you're wondering, I own a Threadripper with 16 physical cores and 32 threads. Do you know how many am I using effectively in this project? Just one.

如果您想知道,我拥有一个拥有16个物理核心和32个线程的Threadripper。 您知道我在这个项目中有效使用了多少个吗? 只有一个。

There're so many ways to say the same: the performance of your game will suffer. And by trying to fix this mess by following your gut feeling based on those mysterious names, you'll start to age blazingly faster.

有很多方法可以这样说:游戏的性能会受到影响。 通过尝试根据那些神秘的名字来追踪自己的直觉,以解决这一问题,您将开始更快地衰老。

What would a Level 2 Developer do with 2,000 objects?

2级开发人员将如何处理2,000个对象?

2级开发人员:优化的Unity场景层次结构 (Level 2 Developer: An Optimized Unity Scene Hierarchy)

A Level 2 Unity Developer knows that parenting transforms comes at a price.

2级Unity开发人员知道育儿转换是有代价的。

The Level 2 Developer also knows this price is paid in milliseconds of CPU time. Or, similarly put, it is paid in hours of overtime spent by the developer chasing this up.

2级开发人员还知道此价格是以毫秒为单位的CPU时间支付的。 或者,类似地,它是由开发者追逐它所花费的加班数小时支付的。

Ask yourself: do you have enough CPU budget to pay for this expensive hierarchy?

问问自己:您是否有足够的CPU预算来支付此昂贵的层次结构?

From which area of your game are you subtracting those milliseconds from?

您要从游戏的哪个区域减去这些毫秒?

Will your game bombard the player with less and less flashy particles? Or will you cut the number of city invaders your player will have to defend from?

您的游戏会用越来越少的浮华粒子轰击玩家吗? 还是您会减少玩家必须防御的城市入侵者的数量?

Hopefully, you'll do nothing of these two things., but rather...

希望您不会在这两件事上做任何事情,而是...

Rubén Torres Bonet 鲁本·托雷斯·博内特

The reason complicated parenting steals resources from your game is simple:

复杂的育儿行为会从游戏中窃取资源的原因很简单:

Each transform modification affects its children and potentially its parent and siblings

每个转换修改都会影响其子级,并可能影响其父级和同级

Unity recalculates a lot of thingies for you when you change an object's position, rotation or scale.

更改对象的位置,旋转或比例时,Unity会为您重新计算很多东西。

This process is transparently done for you, so you can focus on developing your game. That is great, but the price to pay for it becomes astronomical if you're caught off-guard.

该过程为您透明地完成,因此您可以专注于开发游戏。 很棒,但是如果措手不及,要付出的代价将是天文数字。

The solution?

解决方案?

Keep it simple, adhere to...

保持简单,坚持...

Gamedev Guru的干净Unity场景层次结构的黄金法则 ( The Gamedev Guru's Golden Rules of a Clean Unity Scene Hierarchy)

  • No more than 50 total child elements per game object

    每个游戏对象的子元素总数不超过50

  • No more than 4 levels of depth in any game object

    任何游戏对象中的深度不得超过4级

Isn't that simple?

这不是很简单吗?

A Level 2 Developer will keep their unity scene hierarchies as flat as possible.

2级开发人员将使他们的统一场景层次结构尽可能平坦。

Look below. Same amount of objects, this time structured under a flat hierarchy.

往下看。 相同数量的对象,这次的结构是扁平的。

Savings are huge, I see.

我知道节省了很多。

But doing this is easier said than done, right? Detaching and destroying relation(ships) between objects is a laborious, error-prone and time-intensive task.

但这说起来容易做起来难,对吧? 分离和破坏对象之间的关系是一项费力,容易出错且耗时的工作。

What would a Level 3 developer do?

3级开发人员会做什么?

3级开发人员:FAP层次结构工具 (Level 3 Developer: The FAP Hierarchy Tool)

A good Unity developer gets things done.

一个优秀的Unity开发人员可以完成工作。

great Unity developer gets things done faster, more accurately.

出色的 Unity开发人员可以更快,更准确地完成工作。

But how is that?

那怎么了

By using tools.

通过使用工具。

Because the only cheap hierarchy is the empty one, there's one key answer you need to answer at all points from your game...

因为唯一便宜的层次结构是空的层次结构,所以您需要在游戏的所有点上回答一个关键的答案...

How complex are your hierarchies?

您的层次结构有多复杂?

You can answer this by methodically analyzing your hierarchies and giving it a score.

您可以通过有条不紊地分析您的层次结构并给它打分来回答。

So what are the math formulas I need to calculate the score?

那么,计算分数需要什么数学公式?

Luckily, none. I've done this for you.

幸运的是,没有。 我为你做了。

I prototyped a tool to automatically analyze and detect bottlenecks in your hierarchy based on The Gamedev Guru's Golden Rules of a Clean Hierarchy.

我设计了一个工具原型,该工具可以根据Gamedev Guru的“清洁层次结构的黄金规则”自动分析和检测层次结构中的瓶颈。

I call this tool...

我称这个工具...

The Fabulous Advanced Profiling Hierarchy Tool

神话般的高级概要分析层次结构工具

Run your game. Get to a point of interest. Open the tool and let it do the work for you.

运行游戏。 到达兴趣点。 打开工具,让它为您完成工作。

The Gamedev Guru's FAP Hierarchy Tool

Gamedev Guru的FAP层次结构工具

Let the tool whisper in your ear the magical melody of an integer that we will call… score.

让该工具在您的耳边悄悄说出一个我们称之为的整数的神奇旋律……得分。

Once you know what your score is, you know what to do… Or not?

一旦知道分数是多少,就知道该怎么做……还是不?

Because I'm not giving you only that tool, but also a bonus script to boost your hierarchy problem-solving techniques.

因为我不仅为您提供该工具,还为您提供了奖金脚本,以增强您的层次结构解决问题的技术。

I'm giving you the extraordinary power of the simple DetachGameObject.cs Demigod script.

我为您提供了简单的DetachGameObject.cs Demigod脚本的非凡功能。

You're not likely to encounter many 27-line scripts that will help you improving performance so much in that small time frame.

您不太可能遇到许多27行脚本,这些脚本可以在这么短的时间内帮助您极大地提高性能。

What this does is simple: it'll move your heavy and nested game objects to the root of the scene hierarchy in run-time. That way, you'll profit from having structured hierarchies during development and gain an insane performance advantage during run-time.

这样做很简单:它将在运行时将沉重且嵌套的游戏对象移动到场景层次结构的根。 这样,您将受益于开发过程结构化的层次结构,在运行时获得了疯狂的性能优势

Chances are, you can do this for the great part of your hierarchy that is heavily nested for structure's sake.

很有可能,您可以在层次结构的很大一部分中这样做,这是为了结构的缘故而大量嵌套的。

Claim your free bonuses before they become a 404 error (opens in a new tab)

领取免费红利,直到它们变成404错误 (在新标签中打开)

And after that? Comment below and share your score.

在那之后? 在下面发表评论并分享您的分数

翻译自: https://habr.com/en/post/486636/

unity 场景优化

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值