- Home /
Find an Asset by Name and get it's Path?
I have an editor script that finds the Prefab based on the name of the item selected and replaces it. I'm using it to fix broken Prefab connections in my project. It works great but I'm assuming that the Prefab is a Platform and I'd like to make the code more general.
@MenuItem ("Tools/Replace with Prefab %r")
static function Revert() {
var transforms = Selection.transforms;
for (var i : int = 0; i < transforms.Length; i++) {
var localPath : String = "Assets/Prefabs/Platforms/" + transforms[i].gameObject.name + ".prefab";
var newObject : GameObject = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(localPath, Object)) as GameObject;
newObject.transform.position = transforms[i].position;
newObject.transform.localRotation = transforms[i].localRotation;
newObject.transform.localScale = transforms[i].localScale;
DestroyImmediate(transforms[i].gameObject);
}
}
Answer by Kleptomaniac · Mar 09, 2012 at 06:13 AM
I think what you may want is GetAssetPath. Name says it all I think!
Klep
That works for GameObjects that have valid prefab connections, but for prefabs with missing connections it doesn't work.
What I'm looking for is a way to find out what directories exist in the Assets Folder or to be able to recursively search a directory.
You could use System.IO.Directory.GetDirectories(Application.dataPath, "*", System.IO.SearchOption.AllDirectories). Note that in the editor, Application.dataPath is the Assets folder for the project.
thanks!!! just got this:
var path1 : String = Directory.GetDirectories(Application.dataPath, title, System.IO.SearchOption.AllDirectories);
No appropriate version of 'System.IO.Directory.GetDirectories' for the argument list '(String,String, System.IO.SearchOption)' was found.
ok i set build settings to pc, switch platform, it compiles, i have this:
texture1 = GOcube4.renderer.material.mainTexture;
var retrievepath : String = AssetDatabase.GetAssetPath(texture1);
// if (retrievepath == null)
// {
var title : String = Selection.activeGameObject.renderer.materials[1%Selection.activeGameObject.renderer.materials.length].mainTexture.name;
var path1 = System.IO.Directory.GetFiles(Application.dataPath, title+"*", System.IO.SearchOption.AllDirectories);
for (path1i in path1)
{
// if (path1i == Texture2D )
// {
print(" was a texture " + path1i.GetType() );
// }
}
//var retrievepath2 : String = AssetDatabase.GetAssetPath(texture1);
//print(Selection.activeGameObject.renderer.materials[1%Selection.activeGameObject.renderer.materials.length].mainTexture.name);
print(path1.length + " [athj " + path1[2]);
Your answer
Follow this Question
Related Questions
AssetDatabase.AddObjectToAsset is not persistent? 0 Answers
Could not load copied asset via AssetDatabase.LoadAssetAtPath 4 Answers
Prefab Custom Inspector Script - Odd Behaviour 1 Answer
How to check if an object is asset in build runtime (Not in Editor) ? 4 Answers
Get prefab's path in file system 2 Answers