Unity Flashcards

1
Q

one declares an instance, the other references the component shared on all versions. This is used on materials.

A

.mat vs .sharedmat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

used to store data we reference repeatedly that changes infrequently.

A

caching

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

data containers, reduce project memory by avoiding value copies.

A

scriptable object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Scenes, prefabs, and other unity files(assets) reference _____ to uniquely identify files within a single project, and between different projects.

A

GUID(Globally Unique Identifier)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Temporary heap allocations, instantiations, coroutines, passing strings around, using .material or .tag calls are what common pitfalls when using Unity’s API?

A

common garbage collection pitfalls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

is a function that can suspend its execution (yield) until the given Yield-Instruction finishes. Yield where you dictate, acting like jugglers that hand off at rehearsed points. Threads, however, are auto-managed by the operating system.

A

coroutine

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Used in creating a basic asset menu tool that updates certain object components. Like lights, transforms, etc.

A

Editor scripting

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

object interpolates between start and end point, with third value being rate of interpolation.

A

Mathf.lerp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

assets referenced and loaded into the scene take this. Additionally pre-baking or mesh optimization on build can affect this. Static and baked objects, especially lightmap data have this.

A

Load times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Clones the object original and returns the clone.

A

Instantiation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Element that can be used for screen rendering. Elements on the ______ are rendered AFTER Scene rendering, either from an attached camera or using overlay mode.

A

Canvas

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

a process which prevents Unity from performing rendering calculations for GameObjects
that are completely hidden from view by other GameObjects. Every frame, Cameras
perform these operations that examine the Renderers in the Scene
and exclude those that do not need to be drawn.

A

Occlusion culling

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Two colliders can’t have collision Behavior without this?

A

Rigidbody on at least one of the objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why should moving colliders have a rigidbody?

A

when there is no rigidbody, then unity assumes the object is static. Unity doesn’t bother testing collisions between static objects. As a result, this is very efficient for objects in the world the player can bump into. Having kinematic rigidbodies rather than none, is to turn on collision detection between this object and all other colliders in the scene. Effectively you are letting unity know that this object moves around, so unity will then do collision detection between it and everything else.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

character joints, configurable joints, fixed joints, hinge joints, and spring joints are all under this umbrella term?

A

Physics Joints

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

This type of joint is used to simulate chains, and for break-action shotguns.

A

Hinge joints

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

this type of joint can be used for webs shooter slinging.

A

Spring joints

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Used to represent rotations. They are compact, don’t suffer from gimbal lock and can easily be interpolated. Unity internally uses ______ to represent all rotations.

A

Quaternion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

max amount unity allows of these is things is 32 (0-31).

A

physics layers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

is called every frame, if the MonoBehaviour is enabled. Is the most commonly used function to implement any kind of game script. Not every MonoBehaviour script needs ____.

A

Update()

21
Q

Returns the component of Type type if the game object has one attached, null if it doesn’t.

A

GetComponent

22
Q

A mesh collider allows you to do collision detection between meshes and primitives.

A

MeshCollider

23
Q

This method involves disabling normals in import settings, because the physics system doesn’t need them. Additionally going into modeling software and making abstract low-poly representations of the higher tri model.

A

Mesh collider optimization

24
Q

________ shader is a script that runs for each _________ of the mesh, allowing the developer to apply transformation matrixes, and other operations, in order to control where this _________ is in the 3D space, and how it will be projected on the screen.

A

Vertex Shader

25
Q

________ shader is a script that runs for each fragment (_______ candidate to be rendered) after three vertexes are processed in a mesh’s triangle. The developer can use information like the UV / TextureCoords and sample textures in order to control the final color that will be rendered on screen.

A

Pixel shader

26
Q

During rendering, each pixel is calculated whether it should be illuminated and recieve lighting influence, and this is repeated for each light. After approximately eight repeated calculations for different lights in the scene, the overhead becomes significant.

For large scenes, the number of pixels rendered is usually bigger than the number of pixels in the screen itself.

________ Lighting makes the scene render all pixels without illumination (which is fast), and with extra information (at a cost of low overhead) it calculates the illumination step only for the pixels of the screen buffer (which is less than all pixels processed for each element). This technique allows much more light instance in the project.

A

Deferred Lighting

27
Q

Real time applications, such as games, have a variable FPS. They sometimes run at 60fps, or when suffering slowdowns, they will run on 40fps or less.

If you want to change a value from A to B in 1.0 seconds you can’t simply increase A by B-A between two frames because frames can run fast or slow, so one frame can have different durations.

The way to correct this is to measure the time taken from frame X to X+1 and increment A, leveraging the change with the frame duration deltaTime by doing A += (B-A) * DeltaTime.

When the accumulated DeltaTime reaches 1.0 second, A will have assumed B value.

A

Time.deltaTime being used to make things that depend on time operate correctly.

28
Q

Explain why vectors should be normalized when used to move an object?

A

Normalization makes the vector unit length. It means, for instance, that if you want to move with speed 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.

29
Q

Finish this code so the GameObject containing this script moves with constant speed towards target, and stop moving once it reaches 1.0, or less, units of distance.

class Mover : MonoBehaviour
{
  Vector3 target;
  float speed;
  void Update()
  {

}
}

A
class Mover : MonoBehaviour
{

Vector3 target;
float speed;

  void Update()
  {
      float distance = Vector3.Distance(target,transform.position);
      // will only move while the distance is bigger than 1.0 units
      if(distance > 1.0f)
      {
        Vector3 dir = target - transform.position;
        dir.Normalize();                                    // normalization is obligatory
        transform.position += dir * speed * Time.deltaTime; // using deltaTime and speed is obligatory
      }     
  }
}
30
Q

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

A

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

31
Q

Which of the following examples will run faster?

  1. 1000 GameObjects, each with a MonoBehaviour implementing the Update callback.
  2. One GameObject with one MonoBehaviour with an Array of 1000 classes, each implementing a custom Update() callback.
A

The correct answer is 2.

The Update callback is called using a C# Reflection, which is significantly slower than calling a function directly. In our example, 1000 GameObjects each with a MonoBehaviour means 1000 Reflection calls per frame.

Creating one MonoBehaviour with one Update, and using this single callback to Update a given number of elements, is a lot faster, due to the direct access to the method.

32
Q

Explain, in a few words, the role of the inspector is?

A

The inspector panel allows users to modify numeric values (such as position, rotation and scale), drag and drop references of scene objects (like Prefabs, Materials and Game Objects), and others. Also it can show a custom-made UI, created by the user, by using Editor scripts.

33
Q

Explain, in a few words, what the project panel is?

A

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.

34
Q

Explain, in a few words, what role the hierarchy panel has?

A

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.

35
Q

Which panel is responsible for referencing the content that will be included in the build process?

A

The panel responsible for referencing content in the build process is the hierarchy panel. The panel contains references to the objects that exist, or will exist, when the application is executed. When building the project, Unity searches for them in the project panel, and adds them to the bundle.

36
Q

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()
A

The correct execution order of these event functions when an application closes is as follows:

Awake()
OnEnable()
Start()
Update()
LateUpdate()
OnGUI()
OnApplicationQuit()
OnDisable()
OnDestroy()

Note: You might be tempted to disagree with the placement of 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.

37
Q

Explain the issue with the code below and provide an alternative implementation that would correct the problem.

using UnityEngine;
using System.Collections;

public class TEST : MonoBehaviour {
    void Start () {
        transform.position.x = 10;
    }
}
A

The issue is that you can’t modify the position from a transform directly. This is because the position is actually a property (not a field). Therefore, when a getter is called, it invokes a method which returns a Vector3 copy which it places into the stack.

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;

public class TEST : MonoBehaviour {
   void Start () {
        Vector3 newPos = new Vector3(10, transform.position.y, transform.position.z);
        transform.position = newPos;
    }
}
38
Q

What are the benefits of having a visualization mode for rendering optimization, as shown on the picture bellow?

https://uploads.toptal.io/blog/image/91361/toptal-blog-image-1436440710160-c46b314c032ac1ddba5ad073a54adf88.png

A

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.

39
Q

What is a gameobject?

A

Base class for all entities in Unity Scenes.

Note: Many variables in the GameObject class have been removed. To access, for example GameObject.renderer in csharp use GetComponent() instead.

40
Q

What is a component?

A

Base class for everything attached to GameObjects.

Note that your code will never directly create a Component. Instead, you write script code, and attach the script to a GameObject. See Also: ScriptableObject as a way to create scripts that do not attach to any GameObject.

41
Q

What is the difference between a gameobject and an object in c#?

A

Object, in C#, is an instance of a class that is created dynamically. Object is also a keyword that is an alias for the predefined type System. Object in the . NET framework. The unified type system of C# allows objects to be defined. Object is the base class for all objects Unity can reference.

GameObject, besides being a subclass of Object, is the base class for all entities within scenes. It has a Transform component by default, and can have other components attached.

42
Q

What value can I access faster, a boolean from a list, or a list from a dictionary and why?

A

A dictionary is a hash table, so it is really fast to find the keys. So between dictionary vs list speed, the dictionary would be faster.

Note: If you don’t have a value to associate, it is even better to use a set. It is a hash table, without the “table” part.

43
Q

C# has a unified type system, which means that all types inherit, directly or indirectly, from object (________). This includes both reference and value types. It also includes all primitive types, like int and bool, as well as every type provided in the .NET Framework and every custom type that you define.

Because every type inherits from object, you can call any of the _______ methods (e.g. Equals and ToString) on an instance of any type.

A

System.Object

44
Q

Write a class and use a constructor?

class name person
string for first and last name
constructor setting variables
A

public class Person
{
private string last;
private string first;

   public Person(string lastName, string firstName)
   {
      last = lastName;
      first = firstName;
   }
   // Remaining implementation of Person class.
}
45
Q

Whenever a class or struct is created, its ________ is called. A class or struct may have multiple ________ that take different arguments. ________ enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read.

A

Constructors

46
Q

The Canvas is the basic component of Unity UI. It generates meshes that represent the UI Elements placed on it, regenerates the meshes when UI Elements change, and issues draw calls to the GPU so that the UI is actually displayed.

Generating these meshes can be expensive. UI Elements need to be collected into batches so that they’re drawn in as few draw calls as possible. Because batch generation is expensive, we only want to regenerate them when necessary. The issue is that, when one or more elements change on a Canvas, the whole Canvas has to be analyzed once again, to figure out how to optimally draw its elements.

Many users build their entire game’s UI on a single Canvas with thousands of elements. When they change one element, they can experience a CPU spike costing multiple milliseconds. To learn more about why rebuilding is so expensive, go to the 24:55 mark in this Unite session.

A

Solution: Split up your Canvases.

Each Canvas is an island that isolates its elements from those of other Canvases. Take advantage of UGUI’s ability to support multiple Canvases by slicing up your Canvases to solve the batching problems with Unity UI.

You can also nest Canvases, which allows designers to create large hierarchical UIs, without having to think about where different elements are onscreen across Canvases. Child Canvases also isolate content from both their parent and sibling Canvases. They maintain their own geometry and perform their own batching. One way to decide how to split them up is based on how frequently they need to be refreshed. Keep static UI Elements on a separate Canvas, and dynamic Elements that update at the same time on smaller sub-Canvases. Also, ensure that all UI Elements on each Canvas have the same Z value, materials, and textures.

47
Q

What is something you can save on UI performance that involves raycasting?

A

Solution: Remove Graphic Raycasters from non-interactive UI Canvases and turn off the Raycast Target for static or non-interactive elements.

In particular, text on a button turning off the Raycast Target will directly reduce the number of intersection checks that the Graphic Raycaster must perform on each frame.

Problem: Sometimes the Graphic Raycaster does act as a raycaster.

If you set the Render mode on your Canvas to Worldspace Camera or Screen Space Camera, you can add a blocking mask. The blocking mask determines whether the Raycaster will cast rays via 2D or 3D physics, to determine if some physics object is blocking the user’s ability to interact with the UI.

Solution: Casting rays via 2D or 3D physics can be expensive, so use this feature sparingly.

Minimize the number of Graphic Raycasters by excluding them from non-interactive UI Canvases, since – in this case – there is no reason to check for interaction events.

48
Q

Coroutine Misconceptions?

A

Coroutines are not threads. The code you put in a coroutine is executed entirely in serial by the main thread. There is a scheduled time in the script execution order where coroutines are executed, please see this page: http://docs.unity3d.com/Manual/ExecutionOrder.html

Because coroutines are not threads, they are not designed to perform work in parallel. Their primary purpose is to spread work out over multiple frames so it either animates nicely if it’s a visual effect, or prevents stalling the engine and causing a hiccup if it’s a concentrated amount of heavy workload, like multiple object instantiations of heavy meshes.

A coroutine is an object that implements IEnumerator. This object is added to a list of currently active coroutines inside MonoBehaviour when StartCoroutine is called. This list is then traversed once per Update, and the IEnumerator’s MoveNext method is called, which triggers the execution of the next cycle in the coroutine. Therefore, if you kill the MonoBehaviour you used to start a coroutine, the coroutine dies, too. They are not totally fire-and-forget.

Always carefully define a coroutine’s exit-condition(s). They are often used to animate something from A to B because they exit and terminate themselves so beautifully when written right. But if the loop’s exit conditions is ill-defined, it might run forever, and you won’t find out until something starts acting weird.

Beware of starting more coroutines of the same type based on some user-interaction. You could end up having multiple coroutines try to effect changes to the same program state, and thus, they will end up fighting over who gets to do the work. As an example, suppose you have a coroutine open and close a door. Clicking once opens, clicking again closes. If I click twice really fast, two coroutines open and start fighting over opening/closing the door. The result is a door that either twitches, or is frozen in mid-animation.

49
Q

What is a prefab?

A

Unity’s Prefab system allows you to create, configure, and store a GameObject complete with all its components, property values, and child GameObjects as a reusable Asset. The Prefab Asset acts as a template from which you can create new Prefab instances in the Scene.

Any edits that you make to a Prefab Asset are automatically reflected in the instances of that Prefab, allowing you to easily make broad changes across your whole Project without having to repeatedly make the same edit to every copy of the Asset.

You can nest Prefabs inside other Prefabs to create complex hierarchies of objects that are easy to edit at multiple levels.