assetPath | The Asset's path. |
localIdentifierInFile | The object's local file identifier. |
Type The object's type.
Gets an object's type from an Asset path and a local file identifier.
The following script example shows how to get the types of a nested asset. A material asset is added to a scriptable object asset (in this case called DummyObject), and the types of these two assets are extracted by calling 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"; }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.