Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
0
Question by Mambo4 · Feb 17, 2016 at 12:42 PM · editorassetdatabase

Detecting Asset Types

Suppose I have a path to a folder of assets , the types of which are unknown. and I want to determine if each one of the assets is a Texture, Model, or Audio etc. and then do further editor script things to it depending on type.

how would I do this? If I use AssetDatabaseGetAssetAtPath() I must cast it to a certain type.

What if I don't know an assets type ahead of time? Unity assigns correct thumbnail icons to each type, therefore it must have some detection happening. Is that code available to us?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Centripetal · May 21, 2020 at 04:56 PM

To get the type of a ScriptableObject type where the class is called "MyClass" you can do the following:

     [MenuItem("Assets/Do Something Cool")]
     private static void DoSomethingCool()
     {
         Debug.Log("OOOOoooh so COOL!!!!");
     }
 
     [MenuItem("Assets/Do Something Cool", true)]
     private static bool DoSomethingCoolValidate()
     {
         if (!(Selection.activeObject is MonoScript))
         {
             return false;
         }
 
         return ((MonoScript)Selection.activeObject).GetClass() == typeof(MyClass);
     }
 
Comment
Add comment · 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
0

Answer by Vatio · Feb 14, 2017 at 09:00 AM

Since Unity 5.5 there is the function: AssetDatabase.GetMainAssetTypeAtPath(string assetPath);

https://docs.unity3d.com/ScriptReference/AssetDatabase.GetMainAssetTypeAtPath.html

Comment
Add comment · 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 Kor_Skarn · Jun 26, 2017 at 08:33 AM 0
Share

There is however an issue with this function. It is broken for for all asset types derived from ScriptableObject: for these, it will always return $$anonymous$$onoBehaviour ins$$anonymous$$d of the actual type, even though the asset doesn't even derive from $$anonymous$$onoBehaviour!

The issue is fixed in 2017.1.0b9

See issue tracker link below: https://issuetracker.unity3d.com/issues/assetdatabase-dot-getmainassettypeatpath-returns-monobehaviour-for-scriptableobjects

avatar image iyakov · Oct 28, 2021 at 06:18 PM 0
Share

You saved my day! Thanks!

avatar image
0

Answer by Tourist · Feb 14, 2017 at 08:30 AM

Use object as the generic argument.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Help with Missing Monobehaviours and Asset Serialization? 0 Answers

How do I return the path of a sub-asset in an AssetDatabase ? 0 Answers

Adding more image to detect but not updating application Arkit. 0 Answers

How do I get the full path to an asset? 2 Answers

Settting Selection.activeObject to multiple items 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