- Home /
Editor Script Selection thinks Sprites are Texture2Ds.
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.
Answer by PsychoDuckArcade · May 09, 2016 at 03:52 PM
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.
Answer by Eno-Khaon · May 09, 2016 at 02:25 AM
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.
Ok so I A$$anonymous$$ going to be getting the assets as Texture2Ds. Now what's the right way to pass them by reference to an image? Image.sprite only takes a sprite type, not a Texture2D. And Image.sprite.texture is read-only.
I tried Sprite.Create and passed the resulting sprite to my Image.sprite, which works in the scene (the image now has a nameless sprite that selecting links to nowhere, so I think it might be a copy not a reference), but when I make a prefab out of it (from editor or by code) the image loses it's sprite reference. The Source Image field just becomes empty.
I tried running code on a prefab of my Image object (ins$$anonymous$$d of a scene instance of the Image object) that just passes the Sprite.Create result to the Image.sprite, but then in the inspector the Image's Source Image field says "Type mismatch"
wtf? -.-
Also I thought Sprite WAS an asset type. I thought Texture2Ds are the image files and Sprites are the interface between those files and what you see on screen. For example an entire spritesheet would be a Texture2D, but one frame from it would be a Sprite. It would make more sense to me to set the image asset by Sprite ins$$anonymous$$d of Texture2D, but in the project view the Sprite type version of the image is like childed or something to the Texture2D type version. If I could get those Sprites from the Texture2D's I have selected (with Selection I'd assume) then it would all work perfectly. If I selected those "childed" sprites and run my editor script to set the Image.sprite to those childed sprites, it works perfectly, and maintains the connection when I make it a prefab.