- Home /
Get UnityEngine.Object reference to folder in project from folder path
I've looked all over in the documentation and online resources for a way to get the Object reference to a folder in my project when I have the path of the folder (relative to the project). I'm attempting to automate the building of several asset bundles, some of which are based on folder hierarchy rather than being in a scene. Building an asset bundle for a folder is easy enough to do, if based on manually selecting the folder within the project window. However, I can't seem to find a way to take the relative path (e.g. "Assets/Bundles/Shared Files") and get a reference to an Object (like that return from Selection.activeObject) using the Unity API.
Does anyone know of a way to get such a reference? Or even a way to set the selection based on the path within the project rather than already having a reference to an Object?
Answer by Jessy · Jan 16, 2012 at 10:14 PM
http://unity3d.com/support/documentation/ScriptReference/AssetDatabase.LoadAssetAtPath.html
You mentioned a folder, not a file, so...? http://msdn.microsoft.com/en-us/library/system.io.directory_methods.aspx
Now I feel silly. I thought I had already tried using those methods on AssetDatabase and failed to get a Unity Object back for folders. I ended up using AssetDatabase.Load$$anonymous$$ainAssetAtPath. Thanks! :)
Aleron - great question. This is exactly what I'm trying to do. I've been trying to use Load$$anonymous$$ainAssetAtPath, but have not been successful. Would you by chance have some sample code that you got working to generate multiple asset bundles based on directories?
LoadAssetAtPath(string assetPath, Type type);
What value for "type" do I pass to this function if the object in question is a folder?
I never was in need to get an object reference for a folder in Unity. However if it's still possible you might want to try typeof(UnityEngine.Object)
.
Answer by idbrii · Aug 23, 2018 at 06:30 PM
Jessy's answer and comments collapsed into a solution:
AssetDatabase.LoadAssetAtPath<UnityEngine.Object>( "Assets/Scripts/Game"));
Your answer
Follow this Question
Related Questions
select all objects in the hierarchy of an object 1 Answer
Selecting Random Object and Setting Variable to True 2 Answers
Move selected object. 1 Answer
Make objects follow main objects exact path. 1 Answer
AssetDatabase current folder? 2 Answers