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 Stanislav Pugach · Jun 28, 2010 at 02:06 PM · editorassetdatabase

How do I access the list of all assets in a project?

Suppose I have a project containing a number of assets. Is it possible to iterate through the whole set of assets that are shown in the Project panel?

In short, I'd like to write code similar to

foreach ( Object o in AssetDatabase.AllAssets )
{
  if ( o is GameObject )    // Prefab
  ...
}

but there is no AssetDatabase.AllAssets to be found anywhere, or is there?

It is possible to just use Directory.GetFiles() and then load them individually with AssetDatabase.LoadAssetAtPath(), but there are some drawbacks to this approach:

  1. Speed, obviously. Supposing I need to perform some actions on 5% of the assets...
  2. When using Unity with SVN, extra files are created, and need to be filtered out. True, this can be done by simply filtering out "*.meta", but still.
  3. The whole idea of doing this by hand when the assets are sitting there in the Project panel, right before my eyes, with types and icons, seems unnatural.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by jonas-echterhoff · Jun 28, 2010 at 04:12 PM

The project panel uses internal APIs for it's work, so, you'd probably have to use Directory.GetFiles(). It's not that bad really, I can see your points 2. and 3. though both don't seem to be big issues. Why would you think speed would be a problem here?

Actually an AssetDatabase.AllAssets property as you suggest would also not really be feasible, because it would require all objects to be loaded, which can mean Gigabytes of memory on big projects.

Comment
Add comment · 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
2

Answer by roberto_sc · Apr 13, 2015 at 11:13 PM

         foreach(string s in AssetDatabase.GetAllAssetPaths()
             .Where(s => s.EndsWith(".psd", StringComparison.OrdinalIgnoreCase)
                 || s.EndsWith(".tiff", StringComparison.OrdinalIgnoreCase)))
         {
             Texture2D texture = (Texture2D) AssetDatabase.LoadAssetAtPath(s, typeof(Texture2D));
Comment
Add comment · 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
0

Answer by Tetrad · Jun 28, 2010 at 03:39 PM

I'm not entirely sure about editor scripting, but Unity limits what files you can access programmaticly intentionally. The scene is the parent for the resource manager.

The only way that I know of to get access to assets via script is to use the Resources class. Unfortunately, that requires your files to be under a directory named /Resources/, as well as implies that all of those assets are loaded all the time.

Comment
Add comment · 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
0

Answer by MattMaker · Sep 09, 2011 at 11:35 PM

I wrote an Editor extension here: http://www.unifycommunity.com/wiki/index.php?title=UnityAssetXrefs which contains code to do what you suggest, and it works exactly the way you mention. Also, I am glad you mentioned .meta files; I think I forgot to exclude those in the version I have on the wiki. I have an update to post soon that does.

@jonas it's true what you say about memory, but doing an UnloadUnusedAssets immediately afterward seems to make it survivable. :) In some cases you can also use the strategy of returning all the pathnames first and filtering on those before loading actual assets.

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 aeroson · Sep 28, 2013 at 08:49 PM 0
Share

thank you

avatar image eskivor · Feb 19, 2018 at 01:59 PM 0
Share

the link is dead

avatar image Ziplock9000 · Aug 22, 2019 at 04:17 PM 0
Share

This is why answers should never be links but the actual answer.

avatar image BrainSlugs83 · Apr 03, 2020 at 06:57 PM 0
Share

The link is malware now. :(

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

Is there a way to get a unique hash for an asset? 1 Answer

Determining which Asset is connected to a GameObject. 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Getting direct dependencies for an asset 1 Answer

Who owns NetworkViews placed in the editor? 1 Answer


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