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;
}
}

