[Introduction to Unity] 14. Value types and reference types

[Getting Started with Unity] Value Types and Reference Types

    Hello everyone, I am Lampard~~

    Welcome to the Unity Getting Started series of blogs. The knowledge I have learned comes from Teacher Afa at Station B~Thanks

 

(1) Value type

(1) The value type of C#

1. Boolean type (bool)

2. Character type (char)

3. Integer types (byte, sbyte, short, ushort, int, uint, long, ulong)

4. Floating point type (float, double, decimal)

5. Enumeration type (enum)

6. Structure type (struct)

    As in the previous lesson, our external speed parameter is a floating point type, that is, a value type

(2) Unity's value type

1. Vector type (Vector2, Vector3, Vector4)

2. Color type (Color, Color32)

3. Matrix type (Matrix4x4)

4. Quaternion

5. Bounding box type (Bounds)

6. Ray type (Ray)

7. Plane type (Plane)

8. Time type (TimeSpan)

    Unity introduces some unique value types based on C# . For example, when we access the position attribute of the transform component, or access the Euler angle, we get a Vector3 type, which is also a value type.

    

(2) The difference between value types and reference types

(1) Reference type

    Reference types will not be elaborated. All objects instantiated from classes are reference types , as follows:

1. Resource type (Resource)

2. Game object type (GameObject)

3. Component type (Component)

4. Material type (Material)

5. Texture type (Texture)

6. Script type (Script)

7. Animation type (Animation/Animator)

8. Sound type (AudioSource)

9. Collider type (Collider)

10. Rigidbody

11. Particle Type (Particle System)

12. Light source type (Light)

13. UI component type (UI)

14. Scene object type (Scene)

15. Array types of reference types (List, Dictionary, Array)

(2) The difference between reference types and value types

    Value types and reference types are two different data types in C#. Their main differences are:

1. Storage method: The value of the value type is stored directly on the stack, while the value of the reference type is stored on the heap. The stack and the heap are two different areas in the computer memory.

2. Copy method: When copying value types, their values ​​will be copied directly; while when copying reference types, only the reference to the object on the heap will be copied, and the entire object will not be copied.

3. Life cycle: The life cycle of a value type is controlled by the scope where it is located. Once it goes out of scope, it will be released automatically; while the life cycle of a reference type is controlled by the garbage collector. Only when there is no reference pointing to the heap The object will only be recycled when the object is deleted.

4. Mutability: Value types are immutable, and once assigned, their value cannot be modified; while reference types are mutable, and their state can be modified at any time.

(3) External reference type parameters

   We had the function of making the car move towards the red flag before. At that time, the way we obtained the red flag was to find the red flag through the static method of GameObject.Find

    void Start()
    {
        flag = GameObject.Find("红旗");
        this.transform.LookAt(flag.transform);
    }

    On the one hand, this approach is prone to errors. For example, if there are multiple red flag objects? On the other hand, it is inconvenient, we may not know the name of the specific object

    Therefore, we can define the red flag as an external GameObject object, and we can assign this object in the compiler.

Well that's all for today, thanks for reading! ! !
Like and follow! ! !

Guess you like

Origin blog.csdn.net/cooclc/article/details/130232308