版本:Unity 6(6000.0)
语言英语
  • C#

AssetDatabase.GetTypeFromPathAndFileID

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。尽管我们无法接受所有提交,但我们确实会阅读用户提出的每项建议的更改,并会在适用的情况下进行更新。

关闭

提交失败

由于某些原因,您建议的更改无法提交。请在几分钟后<a>重试</a>。感谢您花时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static Type GetTypeFromPathAndFileID(string assetPath, long localIdentifierInFile);

参数

assetPath 资源的路径。
localIdentifierInFile 对象的本地文件标识符。

返回

Type对象的类型。

描述

从资源路径和本地文件标识符获取对象的类型。

以下脚本示例演示如何获取嵌套资源的类型。将材质资源添加到脚本对象资源(在此示例中称为 DummyObject),并通过调用 GetTypeFromPathAndFileID 提取这两个资源的类型。

using UnityEngine;
using UnityEditor;

public class GetTypeFromPathAndFileIDExample {

// Create a menu item under the GameObject menu. [MenuItem("AssetDatabase/GetTypeFromPathAndFileIDExample")] static void GetNestedType() { // Create a simple material object. Material material = new Material(Shader.Find("Specular")); material.name = "My material";

// Create an instance of a simple scriptable object DummyObject scriptableObject = ScriptableObject.CreateInstance<DummyObject>(); scriptableObject.name = "My scriptable object";

// Create the scriptable object asset AssetDatabase.CreateAsset(scriptableObject, "Assets/myScriptableObject.asset");

// Add the material to the scriptable object asset AssetDatabase.AddObjectToAsset(material, scriptableObject); AssetDatabase.SaveAssets();

// Get the path of the created asset string scriptableObjectPath = AssetDatabase.GetAssetPath(scriptableObject);

// Get the GUID and the local file identifier of the scriptable object and the material. string materialGUID, scriptableObjectGUID; long materialFileID, scriptableObjectFileID; AssetDatabase.TryGetGUIDAndLocalFileIdentifier(scriptableObject, out scriptableObjectGUID, out scriptableObjectFileID); AssetDatabase.TryGetGUIDAndLocalFileIdentifier(material, out materialGUID, out materialFileID);

// Print type, local file identifier and path of the two assets. // Notice that the nested assets has the same path as the parent but different local file identifier. Debug.Log(scriptableObject.name+" type: "+ AssetDatabase.GetTypeFromPathAndFileID(scriptableObjectPath, scriptableObjectFileID) + ", fileID: "+scriptableObjectFileID+", path: " + scriptableObjectPath); Debug.Log(material.name+" type: "+ AssetDatabase.GetTypeFromPathAndFileID(scriptableObjectPath, materialFileID) + ", fileID: " + materialFileID + ", path: " + scriptableObjectPath); } }

public class DummyObject : ScriptableObject { public string objectName = "dummy"; }