Selection.GetFiltered(typeof(Sprite), SelectionMode.Unfiltered) is returning no sprites, but Selection.GetFiltered(typeof(Texture2D), SelectionMode.Unfiltered) is returning all sprites but as Texture2Ds instead of Sprites.
The texture type of the asset in the project view IS Sprite (2D and UI).
Getting them as Texture2Ds instead would work fine if I could cast them (not convert, I want the reference to the source asset maintained). I tried Sprite.Create(myTexture, new Rect(0, 0, myTexture.width, myTexture.height), Vector2.one / 2) but for some reason it returns a blank nameless sprite that doesn’t link to anywhere when I select it from the scene.
EDIT: I got Selection.objects to get all objects selected regardless of type, then Logged GetType() to console. The SPRITE i have selected logs GetType() as a Texture2D, even though it’s Texture Type in the inspector is Sprite (2D and UI). It is applied and everything, the sprite is visible, it works if I use it in the scene manually. Only if I try to get it from the selection through code does it think it’s a Texture2D. I’m starting to think this is a bug with unity?
EDIT: I discovered that in the project view the sprites are actually like CHILDED to the texture2Ds. If I select the childed sprites, it works fine. Now to finish my script I gotta figure out how to access the childed Sprite from a sprite type Texture2D asset. It doesn’t have a transform so it’s not like getChild would work, it’s not technically a child. Can’t find any mention of this online anywhere. I’m stumped.
I discovered an alternate way to do what I wanted to do. It kind of feels like a work around but it’s just as simple and very well might be the intended way to do something like this:
AssetDatabase.LoadAssetAtPath<Sprite>(AssetDatabase.GetAssetPath(myTexture2DAsset))
That returns the Sprite asset from the Texture2D (myTexture2DAsset). Passing this result to Image.sprite successfully passes the reference to the Sprite asset. Image.sprite now maintains it’s connection even after making it a prefab.
AssetDatabase is an Editor-only class using the UnityEditor namespace. To get a Texture2D’s Sprite in a build instead of the Editor you’d use Resources or import the Texture2D from an external source instead.
According to the documentation (and reflected in practice), a Sprite inherits from Object. As an example of different inheritance, a Texture2D inherits from Texture.
Additionally, a Sprite contains a texture field, which is a Texture2D.
With that in mind, in the import settings for a texture, your images are always importing under the assumption they can and will be used as a Texture2D. If you define it as a Sprite, you’re simply loading a default property set for the texture to utilize and, in some cases, additional options for functionality.
When you try to load the images using typeof(Sprite)
, you don’t see what you’re expecting because the “Sprite” is not actually the image contained, but is the whole package instead.
Either way, your images are (generally) all Texture2D types and the Sprite import option is simply defining how that texture will be used. The textures aren’t reclassified into a different format. They’re just assigned different defaults and options.