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