Home > Software design >  Is it possible to detect what type of Default Asset is selected in Unity?
Is it possible to detect what type of Default Asset is selected in Unity?

Time:05-05

i'm trying to make a custom editor and i was wondering if there's any way to detect what exact type is an asset when GetType() returns UnityEditor.DefaultAsset.

I can detect most of the basic items(Like script, scene, shader, gameobject) but i haven't been able to determine what type is a DefaultAsset. For folders I use the IsValidFolder method, but i don't know if there's a way to get more information for other objects, like a .DLL

This is actually my code:

if (Selection.activeObject == null) return;
switch (Selection.activeObject.GetType().Name)      
{
     case "SceneAsset":
          //IsAScene
     break;
     case "DefaultAsset":
          if (AssetDatabase.IsValidFolder(AssetDatabase.GetAssetPath(Selection.activeObject)))
          //IsAFolder
          else
          //UnknownItem
     break;
     case "MonoScript":
          //IsAScript
     break;
     case "GameObject":
          //IsAGameObject
          if (PrefabUtility.GetPrefabAssetType(Selection.activeGameObject) != PrefabAssetType.NotAPrefab)
               //IsAPrefab
     break;
     case "Shader":
          //IsAShader
     break;
     case "AudioMixerController":
          //IsAnAudioMixer
     break;
     default:     
     //Other
     break;
}

Thank you

CodePudding user response:

Try something like:

if (Selection.activeObject.GetType() == typeof(DefaultAsset))
{
    // ...
}

I'll leave it to you to adopt it into your switch statement. Also as mentioned, don't use magic strings. Use typeof instead.

CodePudding user response:

There is no general method to get type of DefaultAsset in Unity (yet).

You have to use specific code to detect a specific type.

To detect dll files you can check its extension or importer.

var path = AssetDatabase.GetAssetPath(Selection.activeObject);

var extension = Path.GetExtension(path).ToLowerInvariant();
if(extension == ".dll" || extension == ".so"){}

var importer = AssetImporter.GetAtPath(path) as PluginImporter;
if(importer){}
  • Related