Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

About AssetDatabase.GetAssetPath can not find the right path

Discussion in 'Asset Database' started by suntabu, Oct 6, 2014.

  1. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    76
    As saying on title, I use GetAssetPath API for finding the path of a texture..
    Sometimes can get the right path, but sometimes not. I've searched long time and get nothing useful.
    Here is my code:
    Code (CSharp):
    1.         var texAssets = new List<Texture2D>();
    2.         var textures = Resources.FindObjectsOfTypeAll<Texture2D>();
    3.         foreach (var texture2D in textures)
    4.         {
    5.             var path = AssetDatabase.GetAssetPath(texture2D);
    6.             if (!string.IsNullOrEmpty(path))
    7.             {
    8.                 texAssets.Add(texture2D);
    9.             }
    10.         }
    using this code, I can get the right texture asset list by sometimes...don't know the reason~
     
  2. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    FindObjectsOfTypeAll returns only loaded objects, so I assume you're trying to find textures that were not loaded instead.
     
  3. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Since Unity 4.5 there is a new method which allows you to find assets in your project without actually loading them. Of course, you can then load them if you want:
    Code (csharp):
    1. foreach (string assetGuid in AssetDatabase.FindAssets("t:Texture2D")) {
    2.     string assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
    3.     var texture = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Texture2D)) as Texture2D;
    4.  
    5.     // Do what you want with the texture...
    6. }
    http://docs.unity3d.com/ScriptReference/AssetDatabase.FindAssets.html

    I hope this helps :)
     
    Autarkis and suntabu like this.
  4. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    76
    Thanks for replying,but Not as you say, this methods return all the texture including the assets, I got about thousands textures by using this method. the problem is GetAssetPath sometimes do not work properly
     
  5. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    76
    Thanks , I'll try this...From the API description, I think that's it...thanks again~~
     
  6. Bezzy

    Bezzy

    Joined:
    Apr 1, 2009
    Posts:
    75
    Last edited: Jan 7, 2015