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开发人员
Toptal logo是顶级自由软件开发人员的专属网络吗, designers, finance experts, product managers, 和世界上的项目经理. Top companies hire Toptal freelancers for their most important projects.

Interview Questions

1.

回答以下关于穿线的问题. Explain your answers:

  1. 线程可以用来修改纹理在运行时?
  2. 线程可以用来在场景中移动游戏对象吗?
  3. 考虑下面的代码片段:
类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.

View answer
  1. No. Texture and Meshes are examples of elements stored in GPU memory and Unity doesn’t allow other threads, besides the main one, 对这些数据进行修改.
  2. No. 在Unity中获取Transform引用不是线程安全的.
  3. When using threads, we must avoid using native Unity structures like the Mathf and Random 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
2.

解释什么是顶点着色器,什么是像素着色器.

View answer

顶点着色器是一个脚本,运行在每个顶点的网格, 允许开发人员应用转换矩阵, 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.

3.

Explain why deferred lighting optimizes scenes with a lot of lights and elements.

View answer

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开发工作

Apply as a Freelancer
4.

Explain why Time.deltaTime 应该用来使依赖于时间的东西正确运行吗.

View answer

实时应用程序(如游戏)具有可变的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.

5.

解释为什么矢量在用于移动物体时应该规范化.

View answer

归一化使向量成为单位长度. 它的意思是,例如,如果你想以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.

6.

考虑下面的代码片段:

class Mover : MonoBehaviour
{
  Vector3 target;
  float speed;

  void Update()
  {
  
  }
}

Finish this code so the GameObject 包含此脚本的常量移动 speed towards target,当达到1时停止移动.0或更小的距离单位.

View answer
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
      }     
  }
}
7.

Can two GameObjects, each with only an SphereCollider, both set as trigger and raise OnTrigger events? Explain your answer.

View 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.”

8.

下面哪个示例运行得更快?

  1. 1000 GameObjects, each with a MonoBehaviour implementing the Update callback.
  2. One GameObject with one MonoBehaviour 一个包含1000个类的数组,每个类实现一个自定义 Update() callback.

Explain your answer.

View 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.

9.

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?

View answer

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.

10.

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()
View answer

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.

11.

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;
    }
}
View answer

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;
    }
}
12.

What are the benefits of having a visualization mode for rendering optimization, 如下图所示?

View answer

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为你找到最合适的人.

聘请顶级Unity或Unity3D开发人员

我们的独家网络Unity或Unity3D开发人员

希望找到一份Unity或Unity3D开发人员的工作?

让Toptal为你找到合适的工作.

作为Unity或Unity3D开发者应用

工作机会从我们的网络

提出面试问题

提交的问题和答案将被审查和编辑, 并可能会或可能不会选择张贴, 由Toptal全权决定, LLC.

* All fields are required

寻找Unity或Unity3D开发人员?

Looking for Unity or Unity3D Developers? 看看Toptal的Unity或Unity3D开发者.

Jean Simonet

自由Unity或Unity3D开发人员
United StatesToptal Member Since April 8, 2019

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 More

David Draper

自由Unity或Unity3D开发人员
United StatesToptal Member Since August 2, 2022

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 More

Christopher Ellis

自由Unity或Unity3D开发人员
United StatesToptal Member Since September 15, 2022

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 More

Toptal Connects the Top 3% 世界各地的自由职业人才.

Join the Toptal community.

Learn more