Detailed explanation of Unity common interview questions (continuously updated...)


 1. Declaration, definition, instantiation, initialization

1. First, let's discuss the declaration and definition in C/C++..

1) Let's start with the function declaration and definition...

Generally, we define a function first in C++, and then declare the function before the Main function, for example:

//函数声明
int Add(int);


int Main
{

}
//函数定义
int Add(int)
{
}

The statement is to tell the compiler that we have a function called Add here, and the return value and parameters need int, so I will tell you

The definition is to expand such a space in the memory to allocate memory for the function

So you see, the definition can only be defined once. If you change something a little, this function will not be itself, but you can declare whatever you want. I don’t have memory, so there is no call, just It's equivalent to you shouting in the air, no one pays attention to you...

2) Let's discuss variable declaration and definition again...

In C++, we declare and define like this

//声明
extren int var;
typeof int INT;
struct Node;

//定义
extern int ble = 10;

So the declaration does not open up memory space, but the definition must occupy memory space...

Therefore, in C++, functions and variables are commonly used to define and declare. . .

3) Instantiation and initialization

In object-oriented programming, the process of creating objects with classes is usually called instantiation. Note that it is generally used on classes

Initialization can be understood as the process of assigning values ​​to declared objects, which is equivalent to defining

2: Definition and declaration in C#

        In C#, it is actually about the same as C++, but in C#, after you define a function, you can use it directly without declaring the function above the main function body. As for the definition and declaration of variables, it is the same as C++. If it is int a ; is just a declaration, not defined, use int a = 10; so that the variable is created.


2. Properties (Get accessor and Set) and fields, static keyword

First look at the members of C#

 1: field (field)

A field is a variable that is associated with an object or type, also called a member variable

Looking at the following code, you can see how fields become attributes step by step

using UnityEngine;
using System;

public class GetSetProperty : MonoBehaviour
{
    
    private void Start()
    {
        //一、未使用Get/Set属性
        Student stu1 = new();
        Student stu2 = new();
        Student stu3 = new();

        stu1.Age = 20;
        stu2.Age = 20;
        stu3.Age = 20;

        //此时如果有一个年龄被不小心修改了,比如第三个学生年龄修改成200,这种字段被污染很难发现
        int avgAge = (stu1.Age + stu2.Age + stu3.Age) / 3;
        //Debug.Log(avgAge);


        //二、使用Get/Set方法
        Student1 stu4 = new();
        Student1 stu5 = new();
        Student1 stu6 = new();

        //此时如果不小心将年龄修改成200,程序就会报错
        //可以使用try catch,程序就不会崩溃

        stu4.SetAge(20);
        stu5.SetAge(20);
        //stu6.SetAge(200);
        int avgAge1 = (stu4.GetAge() + stu5.GetAge() + stu6.GetAge())/3;
        try
        {
            stu6.SetAge(200);
            Debug.Log(avgAge1);
        }
        catch (Exception ex)
        {
            Debug.Log(ex.Message);
        }

        //三、使用Get/Set访问器
        Student2 stu7 = new Student2();
        Student2 stu8 = new Student2();
        Student2 stu9 = new Student2();

        stu7.Age = 20;
        stu8.Age = 20;
        stu8.Age = 20;
        int avgAge2 = (stu7.Age + stu8.Age + stu9.Age) / 3;

        Debug.Log(avgAge2);
    }
}
class Student
{
    public int Age;
    public int Score;
}

/// <summary>
/// 二、此时使用Get,Set函数,可以对字段进行约束
/// </summary>
class Student1
{
    private int age;
    public int GetAge()
    {
        return this.age;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    public void SetAge(int value)
    {
        if (value >= 0 && value <= 120)
        {
            this.age = value;
        }
        else
            throw new System.Exception("value has error");
    }
}

    /// <summary>
    /// 三、使用微软的Get/Set访问器
    /// </summary>
class Student2
{
        private int age;
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                if (value >= 0 && value <= 120)
                {
                    this.age = value;
                }
                else
                {
                    throw new Exception("Age value has error");
                }
            }
        }
}

At first we used fields to store data

But considering the exposure, we introduced the Get and Set functions to constrain the fields

Microsoft simplified the Get/Set accessor to form the Get/Set accessor

PropFull can be used for quick property generation

2: Attribute (Prooerty)

A property is a member used to access characteristics of an object or type, a property is a natural extension of a field

3: The relationship between attributes and fields

Both properties and fields are used to represent the state of an entity

Properties are mostly wrappers for fields

Suggestion: Always use attributes to expose data, that is, fields are always private or protected

4: static keyword

The static keyword can be used in class definitions, variable declarations, and method definitions

Add Static before the variable, the variable becomes a static variable , and the memory of the static variable is distributed in the global static area, which is equivalent to it being a global variable, and Time.timescale is a static variable

Add static before the function, and the function becomes a static function , which can be called directly by the class name + function name, such as Debug, or Find functions are all static functions

Add static in front of the class name, the class becomes a static class, the static class does not need to be instantiated , it cannot be inherited, the members in the static class need to bring the static keyword , but the static function does not need to be in in static class

In general, tool functions or tool classes are made into static classes or static functions.


3. The difference between CPU and GPU

From the name of CPU and GPU, one is Central Processing Unit and the other is Graphic Processing Unit

The latter is dedicated to image processing, why use a dedicated processing unit for image processing?

GPU (graphics processing unit) is an important component of the graphics system structure and the link between the computer and the display terminal

That is to say, if you want to display the image on the monitor, you must use the GPU, and how does the GPU work? See the video below:

GPU working principle_哔哩哔哩_bilibili

Let's take a look at the internal structure of CPU and GPU

 As can be seen from the figure, our GPU has a large number of computing units, while the CPU has only 4 simple ALUs, and my GPU has hundreds of computing units. We only talk about the computing power of a single line. For example, if you put a math If the complex calculations are handed over to the CPU, the efficiency will become particularly high, and it can certainly complete the task, but in terms of graphics calculations, we do not have so many advanced calculations, but a large number of matrix calculation transformations, which is the so-called Vertex calculationrasterization calculation→texture map→pixel processing→final output

In such calculations, each pixel is processed. For a high-pixel screen, there are a lot of pixels, and a large number of calculations have to be performed for each pixel, so the efficiency of using the CPU is too low. , at this time, if the GPU is used, the efficiency will become very high, but it should be noted that:

The light and shadow are all calculated by the CPU, and the GPU only has 2 jobs, and 1 polygon is generated. 2 to color the polygon.

The following video can simply tell you the difference between CPU and GPU...

NVIDIA's live image shows the difference in the working principle of CPU and GPU_哔哩哔哩_bilibili

This is a simple introduction to the difference between CPU and GPU


Fourth, the difference between the heap and the stack

The logic operation of the computer is carried out in the CPU, and there are ALU (Arithmetic/Logic Unit) and CU (Control Unit) in the CPU

ALU is an arithmetic logic unit composed of "And Gate" (AND gate) and "Or Gate" (OR gate), and its main function is to perform binary arithmetic operations

The CU is responsible for the process management of the program. Just like the logistics distribution department of a factory, the control unit is the command and control center of the entire CPU, consisting of three components: the instruction register IR (Instruction Register), the instruction decoder ID (Instruction Decoder) and the operation controller OC (Operation Controller)

The problem is that when the computer performs calculations, it does not put all the data in the CPU, but in the memory, and the CPU calls it, and puts the data in the memory in the register, so the CPU calculates according to the calculation needs. Make calls to memory, and the heap and stack we are talking about are two areas in memory

Stack memory (Stack) : The stack stores references to objects and values ​​of parameters in object methods, which are automatically allocated and released by the operating system

                               Stack memory is a combination of dynamic allocation and static allocation, and is local

                                Stacking and popping follow the principle of cartridges, first in first out, last in first out

                                The growth direction of the stack is downward, and the memory address is from high to low

Heap memory (Heap): The heap stores the values ​​of instance objects and member variables (values ​​of attributes). The heap is allocated and released by the developer. If the developer does not release it, it will be reclaimed by the OS at the end of the program

                                The heap is allocated dynamically and is global

                                A heap can be viewed as a tree, such as: heap sort

                                The growth direction of the heap is upward, and the memory address is from low to high


Five, value types and reference types / boxing and unboxing

Value Types: All value types in C# are implicitly derived from System.ValueType. Value types store the value directly

              byte, short, int, long, float, double, decimal, char, bool, and  struct  are collectively referred to as value types.

              After the value type is declared, regardless of whether it is assigned or not, memory has been allocated

Reference type: A reference type is a reference that stores the type

              string and class, array, interface, and delegate are collectively referred to as reference types.

               After the reference type is declared, only a small piece of memory is allocated on the stack to hold an address, but no memory space on the heap is allocated for it at this time; when an instance of a class is created using new, space on the heap is allocated.

Boxing and unboxing:

After knowing the value type and reference type, there is one thing involved, which needs to be converted between these two types, such as

ArrayList arraylist = new ArrayList();
arraylist[0].add(1);
arraylist[1].add('a');
...

At this point, we need to put the value type 1 and the character type a in the array list of the reference type. We know that the base class of all data types is object, so we want to put the object of the value type in the object of the reference type , the value type needs to be converted to object, this conversion is carried out implicitly, at this time the element 0 in the above array is the reference type

look at the next paragraph

int a  = (int)arraylist[0];

At this point, we are converting the reference type in the above array to the value type int, and at this time we have performed a strong conversion


6. List/ArrayList/List, Dictionary, Queue, Hash Table, Stack

1: The difference and relationship between List/ArrayList/List

2: The use and examples of dictionaries

3: Use and examples of queues

4: Usage and examples of hash table

5: Stack (stack)


7. Generics


8. When and how to use the AB package in Unity


9. How many data loading methods are there in Unity?


10. Ray detection, collision detection


11. What is the rendering process of graphics? What are the common rendering algorithms


12. Container/Iterator

Guess you like

Origin blog.csdn.net/leikang111/article/details/127995010