Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Glurth · Jul 05, 2016 at 09:59 PM · assetassetdatabasetypeguid

Get asset's Type given GUID

Given an asset's GUID, how to I determine it's type (e.g. Mesh, Image, Material, PreFabGameObject, MyCustomScriptableObject, etc..)

I tried the following code, but asset.GetType() only returns "DefaultAssetType".

My guess is this is because I'm passing typeof(Object) to the LoadAssetAtPath function, but I have no ideas on how to get the type without instantiating it. Am I just missing some obvious AssetDatabase function?

  string assetPath = AssetDatabase.GUIDToAssetPath(guid);
  Object asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Object));
  if (asset == null) return;
  System.Type assetType = asset.GetType();

This is a simpler variant of my previous (untouched) question: http://answers.unity3d.com/questions/1204365/how-do-i-instantiate-the-appropriate-editor-for-an.html

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jul 05, 2016 at 11:51 PM

If GetType returns "DefaultAsset" as type then there's most likely something wrong with the way you get your asset. The "DefaultAsset" class only have two internal readonly properties:

  • isWarning (bool)

  • message (string)

Try using this method and see what it prints for your asset:

 using UnityEngine;
 using UnityEditor;
 using System.Reflection;
 //[...]
 
 void PrintDefaultAssetMessage(Object obj)
 {
     var type = obj.GetType();
     Debug.Log("object type: " + type.Name);
     if (type == typeof(DefaultAsset))
     {
         var message = type.GetProperty("message", BindingFlags.NonPublic | BindingFlags.Instance);
         var isWarning = type.GetProperty("isWarning", BindingFlags.NonPublic | BindingFlags.Instance);
         string s = (string)message.GetValue(obj);
         bool w = (bool)isWarning.GetValue(obj);
         Debug.Log("IsWarning: " + w + " message: " + s);
     }
 }

I'm not sure if you can use "LoadAssetAtPath" with type "Object". An assetPath / GUID does not necessarily represent a single asset. The same assetpath can reference multiple assets. That may be the reason why using "Object" as type is not possible.

You might want to use AssetDatabase.LoadAllAssetsAtPath instead and iterate through the resulting array.

Comment
Add comment · 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 Glurth · Jul 06, 2016 at 01:47 AM 1
Share

the LoadAllAssetsAtPath suggestion did the trick! I had to check if it was a SceneAsset first, or Unity Popped up an error in the console: "Do not use readobjectthreaded on scene objects" when LoadAllAssetsAtPath was called.

 [DidReloadScripts]
 static void ProjectItemOnGUISetup()
 {
     EditorApplication.projectWindowItemOnGUI += ProjectItemOnGUI;
 }
 
 static void ProjectItemOnGUI(string guid, Rect rect)
 {
     string assetPath = AssetDatabase.GUIDToAssetPath(guid);
 
     Object baseObj = AssetDatabase.LoadAssetAtPath(assetPath, typeof(SceneAsset));
     if (baseObj != null) return;
 
     Object[] assetArray = AssetDatabase.LoadAllAssetsAtPath(assetPath);
 
     if (assetArray == null || assetArray.Length == 0)
     {
         Debug.Log("nothing at path " + assetPath);
         return;
     }
     foreach (Object obj in assetArray)
     {
         Debug.Log("path " + assetPath + " Type: " + obj.GetType().ToString());
      }
 }


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

44 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

Related Questions

What is GUID vs AssetPath? 2 Answers

Custom assets give Missing (Mono Script) 0 Answers

Load an asset right as it is imported? 0 Answers

Override asset on import 0 Answers

How to change an object's name inside an asset file? 0 Answers


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