- Home /
How can I get the full path to an object in an ObjectField?
I have an editor script in which you can choose textures and assign them to EditorGUILayout.ObjectFields. After the user has dragged textures(objects) into the object fields I'd like to know the paths of where they came from so I can perform other functions on them.
How is that done?
Answer by Paulius-Liekis · Feb 22, 2013 at 04:17 PM
AssetDatabase.GetAssetPath
Looks like you were the first to answer correctly. You win a prize! Thanks everyone!
Answer by numberkruncher · Feb 22, 2013 at 04:26 PM
Path relative to project folder can be found using the following:
Object selectedObject = EditorGUILayout.ObjectField(selectedObject, typeof(Object));
if (selectedObject != null) {
string assetPath = AssetDatabase.GetAssetPath(selectedObject);
}
Which will produce a string like:
"Assets/Some/Path/YourObject.prefab" (if you select a prefab)
You can then get the absolute path (if needed) using:
using System.IO;
...
string filePath = Path.Combine(Directory.GetCurrentDirectory(), assetPath);
// i.e. "/Your Unity Project/Assets/Some/Path/YourObject.prefab"
// The following is needed if you are using Windows :-)
filePath = filePath.Replace("/", "\\");
// i.e. "C:\Your Unity Project\Assets\Some\Path\YourObject.prefab"
you may know this,
http://answers.unity3d.com/questions/1133078/recover-path-information-for-an-asset-but-at-runti.html
@numberkruncher .. thanks!
For others reading along: I used Path.Combine() until that failed on a file inside a package. The filename for that was not exactly what was stored on disk. Turned out that using path=Path.GetFullPath(path) seems to do some magic behind the scenes to resolve a virtual package filename into an absolute disk file.
Answer by appearance · Feb 22, 2013 at 04:18 PM
You can use :
string path = AssetDatabase.GetAssetPath (myObject);
Note: path will be the full path to the object that has been dropped onto "myObject" objectfield.
Your answer
Follow this Question
Related Questions
EditorGuiLayout - Objectfield 1 Answer
EditorGUILayout.ObjectField cannot be changed. 1 Answer
why I Cant Add Multiple Objects to the "Object Field" in Editor Window? 3 Answers
EditorGUILayout.ObjectField does not allow me to select a script. 1 Answer
How can I get a list of specific game objects in the object picker opened from an ObjectField()? 0 Answers