- Home /
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
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.
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
Follow this Question
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