Version: 2023.2

AssetDatabase.IsMainAssetAtPathLoaded

切换到手册
public static bool IsMainAssetAtPathLoaded (string assetPath);

参数

assetPath 要加载的资源的文件系统路径。

描述

如果在内存中加载 assetPath 下的主资源对象,则返回 true。

所有路径均是相对于项目文件夹的路径,例如:"Assets/MyTextures/hello.png"。

Additional resources: AssetDatabase.LoadMainAssetAtPath, Resources.UnloadAsset, EditorUtility.UnloadUnusedAssetsImmediate.

using UnityEditor;
using UnityEngine;

public class AssetDatabaseExamples : MonoBehaviour { [MenuItem("AssetDatabase/Is Main Asset At Path Loaded")] static void IsMainAssetAtPathLoadedExample() { //Create a material and unload it var materialPath = "Assets/Materials/NewMat0.mat"; var material = new Material(Shader.Find("Standard")); AssetDatabase.CreateAsset(material, materialPath); Resources.UnloadAsset(material);

//This will be false Debug.Log(AssetDatabase.IsMainAssetAtPathLoaded(materialPath)); //Load material into memory AssetDatabase.LoadAssetAtPath(materialPath, typeof(Object)); //This will be true Debug.Log(AssetDatabase.IsMainAssetAtPathLoaded(materialPath)); } }