Significant performance problem with Sprite.Create after upgrade to v5.6.1f1

I apologize if this has been reported or discussed before, I did searching and browsing and I couldn’t find anything.

After upgrading to 5.6.1f1 from 5.4.1f1, I am seeing performance drop significantly when I use Sprite.Create. I have created some sample code to reproduce this problem. In this code, I exaggerate the problem for effect. I create 500 game objects with a sprite component. In version 5.4.1f1, my BehaviourUpdate runs in 207ms, but this same code in version 5.6.1f1 takes a whopping 5122ms. Furthermore, in version 5.6.1f1, GameObject.Activate and GameObject.Deactivate are responsible for nearly all of this time, where in the older version, these functions account for only .5ms.

Here are the profiler screenshots:
Here is version 5.4.1f1:

Here is version 5.6.1f1:

Any thoughts on what is going on here?

How to replicate: Create an empty project. Create a game object. Attach this script to that game object. Note that I have a 2 second delay before I attempt the process so you can see the spike clearly on the profiler. You will also need an image in the Assets/Resources folder named ‘samepleImage’.

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Main : MonoBehaviour {

   DateTime startTime;
   bool isDone = false;

   void Awake()
   {
      GameObject eventSystem = new GameObject("EventSystem");

      gameObject.AddComponent<EventSystem>();
      gameObject.AddComponent<StandaloneInputModule>();
      RectTransform rectThis = this.gameObject.AddComponent<RectTransform>();
      rectThis.pivot = new Vector2(.5f, .5f);
      Canvas canvas = this.gameObject.AddComponent<Canvas>();
      canvas.renderMode = RenderMode.ScreenSpaceOverlay;
      startTime = DateTime.Now;
   }


   void Update() {

      //Delay test for a couple of seconds so that it is separate from startup code in the profiler.
      if (startTime.AddSeconds(2) > DateTime.Now)
         return;
      //Only do one pass of the creation.
      if (isDone == true)
         return;

      string filenameAndPath = "sampleImage";
      Texture2D sampleTexture = Resources.Load(filenameAndPath) as Texture2D;

      for (int i = 0; i < 500; i++)
      {
         GameObject go = new GameObject(i.ToString());
         go.transform.SetParent(transform);
         Sprite sprite = Sprite.Create(sampleTexture, new Rect(0, 0, sampleTexture.width, sampleTexture.height), new Vector2(0, 0));

         Image imgProfile = go.AddComponent<Image>();
         imgProfile.sprite = sprite;
      }
      isDone = true;
   }
}
1 Like

UPDATE:
I implemented object pooling in my game, so I’m not entirely convinced that it’s the call to Sprite.Create that is the problem. But there is still a significant performance problem relating to handling of game objects.

I ended up downloading and trying this code on every version of Unity from 5.4.1f1 to 5.61f1 with my game.The problem starts happening at V 5.6.0f3. Without changing my code at all, anything before V 5.6.0f3 will run at an average of 83ms. Anything on or after V 5.6.0f3 takes 245ms. The code above still demonstrates the problem.

Anyone have any ideas? I don’t really want to have to roll my game back to a previous version permanently. Should I be reporting this as a bug? Is there anything else anyone would like to see to get to the bottom of this?