• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by PsychoDuckArcade · May 09, 2016 at 12:17 AM · editortexturespriteeditor-scriptingselection

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.

Comment
HowlingMoonSoftware
dCalle
ilovebread
Buzzrick_Runaway
GambaDeveloper

People who like this

5 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
Best Answer

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.

Comment
HowlingMoonSoftware
ilovebread
Buzzrick_Runaway
GambaDeveloper

People who like this

4 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GambaDeveloper · Nov 24, 2020 at 09:30 PM 0
Share

you're a god

avatar image

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.

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PsychoDuckArcade · May 09, 2016 at 02:50 PM 0
Share

Ok so I AM 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 (instead 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? -.-

avatar image PsychoDuckArcade · May 09, 2016 at 03:19 PM 0
Share

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 instead 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.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom Inspector Element on Texture Importer? 2 Answers

get folder from selection array 4 Answers

Texture parameter preview. 0 Answers

Editor class "Texture Importer" question (applying settings to multiple texture assets). 2 Answers

EditorWindow texture effected by Playmode Color Tint 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges