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

Trouble with AssetDatabase.FindAssets

Discussion in 'Asset Database' started by Xelnath, Mar 19, 2015.

  1. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    400
    I have a function which invokes:

    Code (csharp):
    1.  
    2. string[] guids2 = AssetDatabase.FindAssets ("SomeAnimationName.anim");
    3.  
    Which works great - it returns a list of GUIDs which locates all of the assets in the database which match that name.
    Then, I want to filter so that *only* select assets from a specific PreFab. Unfortunately,

    Code (csharp):
    1.  
    2. foreach (var guid in guids2)
    3.     string path = AssetDatabase.GUIDToAssetPath(guid));
    4.  
    Returns the name of the prefab without any mechanism to fetch the AnimationClip's name. What obvious step am I missing?
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    Yet another victim of YAGNI principle and Unity3d team blunders.

    I do it like this and still don't know if it's a final solution.
    Code (csharp):
    1.  
    2.   AnimationClip[] GetAnimationClips(){
    3.      HashSet<AnimationClip> animationClipsUnique = new HashSet<AnimationClip>(GetAssets<AnimationClip>("t:AnimationClip"));
    4.  
    5.      GameObject[] models = GetAssets<GameObject>("t:Model");
    6.      foreach(GameObject model in models){
    7.        AnimationClip[] modelAnimationClips = AnimationUtility.GetAnimationClips(model);
    8.        foreach(AnimationClip animationClip in modelAnimationClips){
    9.          animationClipsUnique.Add(animationClip);
    10.        }
    11.      }
    12.  
    13.      AnimationClip[] animationClips = new AnimationClip[animationClipsUnique.Count];
    14.      animationClipsUnique.CopyTo(animationClips);
    15.  
    16.      return animationClips;
    17.    }
    18.  
    19.    T[] GetAssets<T>(string filter) where T:UnityEngine.Object{
    20.  
    21.      string[] assetsGUIDs = AssetDatabase.FindAssets(filter);
    22.  
    23.      return assetsGUIDs.Convert<string,T>(x=> AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(x),typeof(T)) as T);
    24.    }
    25.  
     
  3. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    This should return only materials, but it's returning fonts. Help!

    Code (CSharp):
    1.    String[] guids = AssetDatabase.FindAssets("t:material", null);
    2.         foreach (string guid in guids)
    3.         {
    4.             string path = AssetDatabase.GUIDToAssetPath(guid);
    5.             Debug.Log(path);
    6.         }
     
  4. CoughE

    CoughE

    Joined:
    Oct 26, 2015
    Posts:
    39
    I know this is from long ago, but for anyone looking, fonts contain materials. You could get the object at the path and then GetComponent to see if a font is present, if so skip it
     
    Ziplock9000 likes this.