unity 判断资源是什么类型
时间: 2023-12-11 15:01:58 浏览: 39
在Unity中,你可以使用AssetDatabase API来判断资源的类型。以下是一个示例代码,展示了如何判断资源是否是纹理(Texture)类型:
```csharp
using UnityEditor;
using UnityEngine;
public class ResourceTypeInfo : EditorWindow
{
[MenuItem("Window/Resource Type Info")]
public static void ShowWindow()
{
GetWindow<ResourceTypeInfo>("Resource Type Info");
}
private void OnGUI()
{
GUILayout.Label("Select a resource to check its type:", EditorStyles.boldLabel);
Object resource = EditorGUILayout.ObjectField("Resource:", null, typeof(Object), false);
if (resource != null)
{
string resourcePath = AssetDatabase.GetAssetPath(resource);
Object resourceObject = AssetDatabase.LoadAssetAtPath<Object>(resourcePath);
if (resourceObject is Texture)
{
GUILayout.Label("Selected resource is a Texture.", EditorStyles.boldLabel);
}
else
{
GUILayout.Label("Selected resource is not a Texture.", EditorStyles.boldLabel);
}
}
}
}
```
在Unity编辑器中,你可以通过选择一个资源文件,然后在菜单栏选择 "Window -> Resource Type Info" 来打开 "Resource Type Info" 窗口。在窗口中,你可以选择一个资源文件,并且它会显示该资源是否是纹理(Texture)类型。
你可以根据需要修改代码来判断其他类型的资源,例如判断音频(Audio)类型、模型(Model)类型等等。