- Home /
Sprite in Asset Bundles returned a Texture2D
Using the Bundle Manager I bundled sprites (images with sprite as their texture type). When I load the sprites at run time and I inspect (by loading all the objects in a Object array) the assets in my asset bundle, all my images are there but in a Texture2D format. I know how to create a sprite from a texture2d, but I wish the asset bundle simply gave me back sprites like I put in initially.
Is this working as intended or am I doing something wrong ?
Answer by Bystander333 · Jun 28, 2018 at 02:36 PM
Waking this up with an actual answer (and Ignoring the slightly patronising one above.) I had the same issue, the solution is to use LoadAssetWithSubAssets or the async version. Same applies when using Resources - the method there is called LoadAllAssetsAtPath. Sprites are considered sub assets of the main asset (png, jpg etc), Unity encodes the details in the meta file - it does this for all assets that arrive in native format. Note, to be clear AssetBundle.LoadAllAssets() is different and probably not what you want, that loads all assets in the bundle into memory. LoadAssetWithSubAssets just loads the single asset you've requested (and any embedded sub assets).
To extract the sprite from the returned array use a helper function, e.g ...
public static T GetSubAsset<T>(UnityEngine.Object[] allAssets) where T : class
{
for (int i=0;i<allAssets.Length;i++)
{
if (allAssets[i].GetType() == typeof(T))
{
return allAssets[i] as T;
}
}
return null;
}
public static T[] GetSubAssets<T>(UnityEngine.Object[] allAssets) where T : class
{
List<T> ret = new List<T>();
for (int i = 0; i < allAssets.Length; i++)
{
if (allAssets[i].GetType() == typeof(T))
{
ret.Add(allAssets[i] as T);
}
}
return ret.ToArray();
}
and call it like
Sprite mySprite = GetSubAsset<Sprite>(subAssetArray);
Answer by wildtapirapps · Jul 23, 2016 at 07:44 PM
@Mogrinnar I am seeing the same with Unity 5.3.5: I have put sprites (made of jpg files) into AssetBundle. When I inspect it I can see for each sprite corresponding Texture2D type, for example:
loaded asset cat of type UnityEngine.Texture2D
loaded asset cat of type UnityEngine.Sprite
I check this with following code:
foreach (Object asset in assets)
{
Debug.Log("loaded asset " + asset.name + " of type " + asset.GetType());
}
Answer by FireBoltStudios · Sep 11, 2017 at 11:01 AM
This is because a 'Sprite' is essentially just bunch of settings to tell Unity how to handle them. If you are able to modify the 'Sprite' to produce sprite sheets and supply borders there must be a source texture aswell, just like materials. For the way sprites are used in unity it would be near impossible to create one file for this without sacrificing functionality. If you think about how splitting a sprite works into multiple elements it would be counter-intuative to take one texture and split it up into how many textures you wanted in the sheet, so unity stores that one texture2d and splits it up at for you using your defined 'Sprite' parameters. This is how Sprites should work!
It's pretty easy to overlook something like this when nearly all other resources are dealt with on a 'what you see is what you get' basis.
Sprite(API) - https://docs.unity3d.com/ScriptReference/Sprite.html
Your answer
Follow this Question
Related Questions
Load Unity 4.3 Sprites with AssetBundles 5 Answers
Multiple draw calls from a single Sprite atlas when loading from Asset bundle 0 Answers
How do I fill a Texture2D with pixels and display a sprite with that texture using script? 1 Answer
Unity UpdateExternalTexture from MovieTexture windows crash 0 Answers
Resize a sprite by giving it more transparent pixels 0 Answers