• 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 · assettypeassetdatabaseguid

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

45 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

Related Questions

What is GUID vs AssetPath? 2 Answers

How to allow Undo on project assets created with an editor script? 0 Answers

"Save As..." Saving level assets at Runtime 0 Answers

Why AssetDatabase.LoadAssetAtPath does not trows exception? 0 Answers

When looping through assetbundle assetdatabase how do you reference a material object 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges