Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Instantiate.Copy in profiler

Discussion in 'Prefabs' started by IntarsC, Jan 17, 2023.

  1. IntarsC

    IntarsC

    Joined:
    Nov 11, 2020
    Posts:
    9
    Hello, I have issues understanding in profiler Instantiate.copy. What does it mean and is there a way to optimise it?
     
    alexeyzakharov likes this.
  2. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    505
    Hi @IntarsC !

    Instantiate.Copy is a part of UnityEngine.Object.Instantiate call. The copy part of the cloning process is where all fields are being copied to a new object. It may be slow if the original object contains large data.

    I would check:
    • if there are other child samples in profiler
    • how heavy is the original object?
    • is it possible to share some data?
     
    IntarsC likes this.
  3. IntarsC

    IntarsC

    Joined:
    Nov 11, 2020
    Posts:
    9
    Thank you for reply, what exact child samples do you mean (some additional functions like ondeserealize/onserialize)? and what sharing data would be as example (referencing same scriptable objects or objects inside prefab)?

    Also would appreciate explanation about Instantiate.produce.
     
  4. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    505
    yes - if you see any other expensive calls inside the Copy

    yes, exactly - e.g. if you clone a GameObject and one of components has an array with some data - that array copy would contribute to the copy costs. In that case if this array can be a shared data it would be best to move it to a scriptable object and then reference the object in the script

    Produce replicated the original object including all child objects and their components.

    Object.Instantiate in a nutshell does the following:
    • Creates a gameobjects/components hierarchy same as original object - Instantiate.Produce.
    • Copies all fields of all objects from the original objects - Instantiate.Copy.
    • Calls Awake on all scripts of new objects - Instantiate.Awake.
     
    Pr0x1d and IntarsC like this.