Version: 5.6

AssetDatabase.LoadAssetAtPath

切换到手册
public static Object LoadAssetAtPath (string assetPath, Type type);

参数

assetPath 要加载的资源的路径。
type 资源的数据类型。

返回

Object 与参数匹配的资源。

描述

返回给定路径 assetPathtype 类型的第一个资源对象。

一些资源文件可能会包含多个对象。(例如,一个 Maya 文件可能会包含多个网格和游戏对象)。 所有路径均是相对于项目文件夹的路径,例如:"Assets/MyTextures/hello.png"。

Note:
The assetPath parameter is not case sensitive.
ALL asset names and paths in Unity use forward slashes, even on Windows.
This returns only an asset object that is visible in the Project view. If the asset is not found LoadAssetAtPath returns Null.

using UnityEngine;
using UnityEditor;

public class MyPlayer : MonoBehaviour { [MenuItem("AssetDatabase/LoadAssetExample")] static void ImportExample() { Texture2D t = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Textures/texture.jpg", typeof(Texture2D)); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961