12个基本的Unity或Unity3D面试问题 *
Toptal sourced essential questions that the best Unity or Unity3D developers and engineers can answer. Driven from our community, we encourage experts to submit questions and offer feedback.
聘请顶级Unity或Unity3D开发人员Interview Questions
回答以下关于穿线的问题. Explain your answers:
- 线程可以用来修改纹理在运行时?
- 线程可以用来在场景中移动游戏对象吗?
- 考虑下面的代码片段:
类RandomGenerator: MonoBehaviour
{
public float[] randomList;
void Start()
{
randomList = new float[1000000];
}
void Generate()
{
System.Random rnd = new System.Random();
for(int i=0;i
使用线程改进这段代码, so the 1000000 random number generation runs without spoiling performance.
- No. Texture and Meshes are examples of elements stored in GPU memory and Unity doesn’t allow other threads, besides the main one, 对这些数据进行修改.
- No. 在Unity中获取Transform引用不是线程安全的.
- When using threads, we must avoid using native Unity structures like the
Mathf
andRandom
classes:
类RandomGenerator: MonoBehaviour
{
public float[] randomList;
void Start()
{
randomList = new float[1000000];
线程t = new Thread(delegate())
{
while(true)
{
Generate();
Thread.Sleep(16); // trigger the loop to run roughly every 60th of a second
}
});
t.Start();
}
void Generate()
{
System.Random rnd = new System.Random();
for(int i=0;i
顶点着色器是一个脚本,运行在每个顶点的网格, 允许开发人员应用转换矩阵, and other operations, 来控制这个顶点在3D空间中的位置, 以及它将如何投影到屏幕上.
Pixel shader is a script that runs for each fragment (pixel candidate to be rendered) after three vertexes are processed in a mesh’s triangle. 开发人员可以使用像 UV
/ TextureCoords
and sample textures in order to control the final color that will be rendered on screen.
During rendering, each pixel is calculated whether it should be illuminated and receive lightning influence, 每一盏灯都重复这个过程. After approximately eight repeated calculations for different lights in the scene, 开销变得非常大.
For large scenes, the number of pixels rendered is usually bigger than the number of pixels in the screen itself.
Deferred Lighting makes the scene render all pixels without illumination (which is fast), 并且具有额外的信息(以低开销为代价), it calculates the illumination step only for the pixels of the screen buffer (which is less than all pixels processed for each element). 这种技术允许在项目中有更多的光照实例.
申请加入Toptal的发展网络
并享受可靠、稳定、远程 自由Unity或Unity3D开发工作
实时应用程序(如游戏)具有可变的FPS. They sometimes run at 60FPS, or when suffering slowdowns, they will run on 40FPS or less.
如果要更改 A
to B
in 1.0秒你不能简单地增加 A
by B-A
between two frames because frames can run fast or slow, so one frame can have different durations.
纠正这种情况的方法是测量从帧中取走的时间 X
to X+1
and increment A
,利用帧持续时间的变化 deltaTime
by doing A += (B-A) * DeltaTime
.
When the accumulated DeltaTime
reaches 1.0 second, A
will have assumed B
value.
归一化使向量成为单位长度. 它的意思是,例如,如果你想以20的速度移动.0, multiplying speed * vector
will result in a precise 20.0 units per step. If the vector had a random length, the step would be different than 20.0 units.
考虑下面的代码片段:
class Mover : MonoBehaviour
{
Vector3 target;
float speed;
void Update()
{
}
}
Finish this code so the GameObject
包含此脚本的常量移动 speed
towards target
,当达到1时停止移动.0或更小的距离单位.
class Mover : MonoBehaviour
{
Vector3 target;
float speed;
void Update()
{
float distance = Vector3.Distance(target,transform.position);
//只有当距离大于1时才会移动.0 units
if(distance > 1.0f)
{
Vector3 dir =目标-变换.position;
dir.Normalize(); // normalization is obligatory
transform.position += dir * speed * Time.deltaTime; // using deltaTime and speed is obligatory
}
}
}
Can two GameObjects
, each with only an SphereCollider
, both set as trigger
and raise OnTrigger
events? Explain your answer.
No. Collision events between two objects can only be raised when one of them has a RigidBody
attached to it. This is a common error when implementing applications that use “physics.”
下面哪个示例运行得更快?
- 1000
GameObjects
, each with aMonoBehaviour
implementing theUpdate
callback. - One
GameObject
with oneMonoBehaviour
一个包含1000个类的数组,每个类实现一个自定义Update()
callback.
Explain your answer.
The correct answer is 2.
The Update
使用c#反射调用回调, 哪个比直接调用函数要慢得多. In our example, 1000 GameObjects
each with a MonoBehaviour
意味着每帧有1000个反射调用.
Creating one MonoBehaviour
with one Update
,并使用这个回调来 Update
a given number of elements, is a lot faster, due to the direct access to the method.
Explain, in a few words, what roles the inspector, Unity编辑器中的项目和层次面板. Which is responsible for referencing the content that will be included in the build process?
The inspector panel allows users to modify numeric values (such as position, rotation and scale), 拖放场景对象的引用(如预制件), Materials and Game Objects), and others. Also it can show a custom-made UI, created by the user, by using Editor scripts.
The project panel contains files from the file system of the assets folder in the project’s root folder. It shows all the available scripts, textures, materials and shaders available for use in the project.
The hierarchy panel shows the current scene structure, with its GameObjects and its children. It also helps users organize them by name and order relative to the GameObject’s siblings. Order dependent features, such as UI, make use of this categorization.
The panel responsible for referencing content in the build process is the hierarchy panel. 面板包含对现有对象的引用, or will exist, 当应用程序执行时. When building the project, Unity searches for them in the project panel, and adds them to the bundle.
Arrange the event functions listed below in the order in which they will be invoked when an application is closed:
Update()
OnGUI()
Awake()
OnDisable()
Start()
LateUpdate()
OnEnable()
OnApplicationQuit()
OnDestroy()
The correct execution order 当应用程序关闭时,这些事件函数如下:
Awake()
OnEnable()
Start()
Update()
LateUpdate()
OnGUI()
OnApplicationQuit()
OnDisable()
OnDestroy()
注意:您可能不同意的位置 OnApplicationQuit()
in the above list, but it is correct which can be verified by logging the order in which call occurs when your application closes.
Explain the issue with the code below and provide an alternative implementation that would correct the problem.
using UnityEngine;
using System.Collections;
公共类TEST: MonoBehaviour {
void Start () {
transform.position.x = 10;
}
}
The issue is that 你不能直接从变换中修改位置. 这是因为这个位置实际上是a property (not a field). Therefore, when a getter is called, it invokes a method which returns a Vector3
copy 它把哪个放到堆栈中.
So basically what you are doing in the code above is assigning a member of the struct a value that is in the stack and that is later removed.
Instead, the proper solution is to replace the whole property; e.g.:
using UnityEngine;
using System.Collections;
公共类TEST: MonoBehaviour {
void Start () {
Vector3 newPos =新Vector3(10,变换.position.y, transform.position.z);
transform.position = newPos;
}
}
What are the benefits of having a visualization mode for rendering optimization, 如下图所示?
The “overdrawn” mode helps the user to profile the number of pixels being rendered in the same “area”. Yellow to white areas are “hot” areas where too many pixels are being rendered.
Developers can use this information to adjust their materials and make better use of the Z-Test
and optimize the rendering.
面试不仅仅是棘手的技术问题, 所以这些只是作为一个指南. Not every “A” candidate worth hiring will be able to answer them all, 回答所有问题也不能保证成为A级考生. At the end of the day, 招聘仍然是一门艺术,一门科学,需要大量的工作.
Why Toptal
提出面试问题
提交的问题和答案将被审查和编辑, 并可能会或可能不会选择张贴, 由Toptal全权决定, LLC.
寻找Unity或Unity3D开发人员?
Looking for Unity or Unity3D Developers? 看看Toptal的Unity或Unity3D开发者.
Jean Simonet
Jean has been working professionally in games and interactive media for 15 years, 最初是在大型工作室,现在是独立开发者. Jean is an expert Unity developer with many projects under his belt ranging from mobile games to VR/AR to physical devices. He is just as comfortable writing fancy shaders as he is writing AI algorithms or designing fun and engaging puzzle games.
Show MoreDavid Draper
David is a software and video game developer who specializes in Unity development. 他参与过各种各样的项目, including game development, web development, augmented reality, and virtual reality. David developed shaders and post-process effects using ShaderLab, Cg, and HLSL. 他对图形和着色器开发充满热情.
Show MoreChristopher Ellis
Christopher is a Unity developer with over a decade of experience in working on a wide range of projects, 从游戏到商业应用, including mobile, PC, 以及HoloLens和Vuforia等混合现实解决方案. 他的两个应用程序已经进入了各自类别的前十名, and one of them, Yumi Story Dice, 也得到了苹果的推荐. Christopher is looking for part-time freelance work in any field using the Unity engine.
Show MoreToptal Connects the Top 3% 世界各地的自由职业人才.
Join the Toptal community.