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

How to get the get dependencies for a scene file

Discussion in 'Editor & General Support' started by GollumZzz, Aug 1, 2012.

  1. GollumZzz

    GollumZzz

    Joined:
    Aug 1, 2012
    Posts:
    24
    as the title, I tried AssetDatabase.GetDependencies and EditorUtility.CollectDependencies, both can not work, any idea?
     
  2. VesuvianPrime

    VesuvianPrime

    Joined:
    Feb 26, 2013
    Posts:
    135
    3 years late, but you're the top google result and I needed to do this today. Here's my solution:

    Code (CSharp):
    1. public const string GUID_PREFIX = "guid:";
    2.  
    3. /// <summary>
    4. ///     We can't load the scene to collect dependencies, so let's get dirty and
    5. ///     parse the file for GUIDs.
    6. /// </summary>
    7. /// <param name="path">Path.</param>
    8. /// <param name="output">Output.</param>
    9. public static void GetSceneDependencies(string path, List<string> output)
    10. {
    11.     using (StreamReader reader = new StreamReader(path))
    12.     {
    13.         while (reader.Peek() >= 0)
    14.         {
    15.             string line = reader.ReadLine();
    16.             if (!line.Contains(GUID_PREFIX))
    17.                 continue;
    18.  
    19.             string guidPart = line.Split(',')[1];
    20.             string guid = RemoveSubstring(guidPart, GUID_PREFIX).Trim();
    21.  
    22.             output.Add(guid);
    23.         }
    24.     }
    25. }
    26.  
    27. /// <summary>
    28. ///    Removes all instances of the given substring.
    29. /// </summary>
    30. /// <returns>The cleaned string.</returns>
    31. /// <param name="source">Source.</param>
    32. /// <param name="remove">Remove.</param>
    33. public static string RemoveSubstring(string source, string remove)
    34. {
    35.    int index = source.IndexOf(remove);
    36.    string clean = (index < 0) ? source : source.Remove(index, remove.Length);
    37.    
    38.    if (clean.Length != source.Length)
    39.      return RemoveSubstring(clean, remove);
    40.  
    41.    return clean;
    42. }
    43.  
    44.  
     
  3. TagScott

    TagScott

    Joined:
    Oct 17, 2014
    Posts:
    21
    Again this is quite an old thread to be adding to. I've been having issues with AssetDatabase.GetDependencies and had assumed it worked a bit like your script, parsing files for GUIDs. However this doesn't seem to be the case. Does anyone know how GetDependencies actually works?
     
  4. JohnOknelorfDev

    JohnOknelorfDev

    Joined:
    Oct 2, 2017
    Posts:
    20
    Hi everyone,

    Quite old thread, but still can be helpful for someone.

    To get all scene dependencies, all we need to do is to call AssetDatabase.GetDependencies(scenePath, true);

    The important note is that you need to set the full relative path to a scene within the project (including Assets folder)

    Here is an example:
    Code (CSharp):
    1. scenePath = "Assets/ExampleScene.unity";
    2. string[] dependencies = AssetDatabase.GetDependencies(scenePath, true);
    Regards,
    John.
     
    AmazingRuss and liekevUnit040 like this.
  5. JJRivers

    JJRivers

    Joined:
    Oct 16, 2018
    Posts:
    137
    I'll commit one more act of necromancy for those having issues packing their bundles without a gazillion cross bundle dependencies.

    GetDependencies only returns dependencies actually in use by the object you're referring to.

    CollectDependencies returns all assets that the objects depend on AND their dependencies.

    This is a crucial distinction, say you want to bundle by scene, any prefabs with overridden properties will not show the underlying dependency associated with that prefab when using GetDependencies, use CollectDependencies instead if you dont want bundles relying on huge chains of interdependence on other bundles.

    If using Addressables do note that the Group assets themselves will show up as dependencies if you're doing guid filtering etc.
     
    AmazingRuss likes this.