Search Unity

Question Instantiate.Copy in profiler

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

  1. IntarsC

    IntarsC

    Joined:
    Nov 11, 2020
    Posts:
    11
    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

    Joined:
    Jul 2, 2014
    Posts:
    507
    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:
    11
    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

    Joined:
    Jul 2, 2014
    Posts:
    507
    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 
    replicates 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.
     
    Last edited: Oct 10, 2023
    tinyant, Pr0x1d and IntarsC like this.