UnityEditor编辑器扩展-判断文件类型

Posted avi9111

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UnityEditor编辑器扩展-判断文件类型相关的知识,希望对你有一定的参考价值。

获取大多数类型

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;

//  上面是国外友人写的,其实可以这么写的:
// if (Selection.activeObject is DefaultAsset)
      

判断是否.dll

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)

查找某一个类型

但请谨慎使用,如果在Update调用,而且你的项目很大,崩溃(内存爆掉)是很容易的事情

public static string GetSelectedPathOrFallback()
    
        string path = "Assets";
        
        foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
        
            path = AssetDatabase.GetAssetPath(obj);
            if ( !string.IsNullOrEmpty(path) && File.Exists(path) ) 
            
                path = Path.GetDirectoryName(path);
                break;
            
        
        return path;
    

以上是关于UnityEditor编辑器扩展-判断文件类型的主要内容,如果未能解决你的问题,请参考以下文章

UnityEditor编辑器扩展-表格功能

UnityEditor编辑器扩展开发之一些有用的Gizmos扩展方法

UnityEditor编辑器扩展开发-自定义Shader入门

UnityEditor编辑器扩展开发-自定义Shader入门

UnityEditor编辑器扩展快速导出包用的脚本剔除的代码分享

UnityEditor编辑器扩展开发如何快速修改字体颜色