Managed Sprites

In cases where it is advantageous to have direct control over things like draw-order, or you do not want to rely on dynamic batching to control draw calls, EZ GUI provides the ability to optionally use a SpriteManager to combine sprites together into a single mesh.
Using Managed Sprites
To use managed sprites, you must first create a GameObject in your scene with a SpriteManager script attached:

  1. Create an empty GameObject (click GameObject->Create Empty).
  2. Drag a SpriteManager script onto the GameObject.
  3. Choose a material to be used for all sprites associated with this manager.
  4. Set additional options (see table below).
Winding
Whether sprite polygons should be wound clock-wise or counter clock-wise. Determines which side of the sprite is considered by the renderer to be the "front".
Alloc Block Size
How many sprites to allocate at a time if the sprite pool is used up. This is also the starting pool size.
Auto Update Bounds
When true, will automatically recalculate the bounding box for the mesh whenever sprite vertices change. If the bounding box is never recalculated, then when the camera moves, Unity may think the mesh is out of sight and cull it, even though it is not. On the other hand, if the camera never moves, or the sprite geometry moves within a confined area, disabling this option can yield a performance gain since unnecessary bounds computations are avoided.
Draw Bounding Box
When true, the same yellow box that is drawn around the SpriteManager's area when it is selected will be drawn even when it is not selected. This is very useful for tweaking the positions of objects to solve depth-sorting issues (see section below).

NOTE: For the bounding box to be updated in real-time as you make changes in edit mode, the attached Skinned Mesh Renderer must have its "Update When Offscreen" box checked. It is recommended, however, to uncheck this before final release for performance reasons.
Target Screen Height
The height, in pixels, at which the sprites should appear pixel-perfect. Ex: If you are developing for iPad landscape which is 1024x768, set this value to 768 so that sprites viewed at this resolution will appear pixel perfect.
Render Camera
The camera that will be used when rendering these sprites.

IMPORTANT

When sizing sprites pixel-perfect (either with the Size Sprites Wizard or the Pixel Perfect checkbox in the inspector), the Render Camera must be set to orthographic.
Depth-sorting

Sorting among sprites in the same manager
Unlike unmanaged sprites, managed sprites are not automatically depth-sorted according to their distance from the camera. Instead, you can control the draw order of your sprites by setting their drawLayer value either by setting it in the inspector, or by calling SetDrawLayer() at runtime in script. Sprites with a lower draw layer value will appear to be drawn behind sprites with a higher draw layer value. All of this only holds true within a single sprite manager. Unity will automatically handle depth sorting between sprite managers, as it views each manager as a single mesh (which it is). It is therefore only possible to interleave sprites from different managers when using shaders that read and write to the Z-buffer.

Sorting with other scene objects
There is an additional consideration regarding depth-sorting when using managed sprites: depth-sorting sprites with regard to other objects in the scene. For example, you have may a sprite that is paired with a TextMesh (3D Text) that you want to be drawn on top of the sprite, such as if this is being used as a button control. The problem can arise that even though your text is in front of your sprite, the sprite is being drawn over the text. This is because all sprites in the SpriteManager are a single mesh, and as such, that mesh has a bounding box. The center of that bounding box is what Unity uses to determine the distance of the object to the camera versus other objects in the scene, and if both objects in question are blended, then the nearer of the two objects will get draw last on top of the one that is farther away. So if your text is appearing behind your sprite despite it being positioned in front of it, check to see where the center point of the over all SpriteManager is by selecting it, or by checking its Draw Bounding Box checkbox. This will display a yellow bounding box, at the center of which is a small "star" which indicates the center point. If this center point is nearer to the camera than the text in our example scenario, then ALL sprites in the SpriteManager will be draw on top. To remedy this, make sure you either move some of your sprites back to shift the center point of the bounding box, or move your text or other objects forward to be in front of the SpriteManager's center point. (Also see important note below.)

NOTE

The location of the SpriteManager's GameObject itself can affect the bounding box, and therefore depth-sorting with other objects in the scene. This is because any "unused" vertices in the mesh's pool are centered at the SpriteManager's location. So to minimize depth-sorting issues, it is recommended to position the SpriteManager object itself amidst its sprites so that it does not needlessly enlarge the manager's bounding box.

Managed sprite prefabs


A managed sprite must be associated with a SpriteManager object both to be rendered in the scene, and to be included in the building of an atlas. However, if you have a managed sprite prefab, there is no way to associate the sprite with a manager in the current scene to allow for atlas building. To get around this, create a prefab out of the SpriteManager in your scene that will eventually manage the sprite prefab, once instantiated. This will give you a prefab SpriteManager that references the same material you will be using at runtime. Now drag this SpriteManager prefab object onto the "Manager" slot on your managed sprite prefab. Now when atlases are built, the sprite prefab will know which material to use. When you finally instantiate the managed sprite prefab, you must immediately add it to the desired SpriteManager. For this reason, it is recommended that you instantiate your managed sprite prefabs using SpriteManager.CreateSprite().