Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by kalvinlyle · Mar 09, 2012 at 12:17 AM · prefabassetdatabaseeditorscript

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);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kalvinlyle · Mar 10, 2012 at 06:57 PM 1
Share

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.

avatar image yoyo · Dec 11, 2013 at 10:38 PM 1
Share

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.

avatar image frogsbo · Sep 05, 2014 at 02:53 PM 0
Share

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.

avatar image frogsbo · Sep 08, 2014 at 03:47 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges