Unity parameter type reference type

Insert image description here

reference type

In Unity, reference types refer to those data types that store object references in memory. The following is an introduction to common reference types in Unity:

  1. Node (GameObject):

    • In Unity, GameObject is the most basic reference type. Game objects are entities in the scene and can contain various components and data to represent objects, characters, props, etc. in the game.
    • Game objects are reference types, so you can share a reference to the same game object by assigning one game object to another variable in your script.

    Example:

    public GameObject player; // 游戏对象引用
    
    void Start()
    {
          
          
        GameObject enemy = player; // 共享 player 游戏对象的引用
        enemy.transform.Translate(Vector3.forward);
    }
    
  2. Component:

    • In Unity, components are modules attached to game objects that add different functionality and behavior. For example, the Renderer component controls the rendering of objects, and the Rigidbody component controls the physical behavior of objects.
    • Components are reference types, so you can share a reference to the same component by assigning it to another variable.

    Example:

    public Renderer objectRenderer; // 渲染器组件引用
    
    void Start()
    {
          
          
        Renderer enemyRenderer = objectRenderer; // 共享 objectRenderer 渲染器组件的引用
        enemyRenderer.material.color = Color.red;
    }
    
  3. Asset:

    • In Unity, resources are reusable items in the project, such as materials, textures, models, audio, etc. Assets are created and managed in one location within the project and can then be used in the scene.
    • Resources are reference types. You can assign a resource reference to a variable in a script and then use the same resource in multiple locations in the scene.

    Example:

    public Material brickMaterial; // 材质资源引用
    
    void Start()
    {
          
          
        Renderer renderer = GetComponent<Renderer>();
        renderer.material = brickMaterial; // 使用共享的 brickMaterial 材质资源
    }
    

In Unity, using reference types can achieve object sharing and avoid repeated object creation, thereby improving performance and efficiency. At the same time, it also allows the same object to be operated in different places, ensuring consistency and synchronization.

The difference between reference types and value types

There are some important differences between reference types and value types in terms of assignment. These differences are mainly reflected in data copying, reference sharing and memory management:

  1. Data copy:

    • Assignment of value types copies the data itself, not a reference to the data. Therefore, modifying the value of one variable does not affect the value of another variable.
    • Assignment of reference types is a copy reference, that is, the variable stores a reference to the object rather than the object itself. Multiple variables can reference the same object, so modifying one variable may affect other variables that reference the same object.
  2. Quote sharing:

    • Assignment of value types copies data, and each variable has its own independent memory storage. Modifying one variable does not affect other variables.
    • Assignment of reference types is a copy reference, and multiple variables may refer to the same object. Modifying a reference type variable may affect other variables that reference the same object.
  3. Memory management:

    • Memory allocation and deallocation of value types is performed on the stack. As the scope of the variables ends, their memory will be automatically deallocated.
    • Memory allocation of reference types is performed on the heap, and the garbage collector (Garbage Collector) is responsible for reclaiming the memory of objects that are no longer used. This introduces some performance overhead.
  4. Pass parameters:

    • When you pass a value type to a function, the function gets a copy of the value. Modifications to parameters do not affect the original values.
    • When you pass a reference type to a function, the function gets a copy of the reference, which means the function can modify the original object.

To sum up, the difference between value types and reference types in terms of assignment mainly lies in the mechanism of data copying and reference sharing. Value types operate on data directly, while reference types operate on references to objects. These differences require careful handling in programming to ensure correct data manipulation and memory management.

Guess you like

Origin blog.csdn.net/weixin_74850661/article/details/132598271
Recommended