How to get the get dependencies for a scene file

How to get the get dependencies for a scene file

https://forum.unity.com/threads/how-to-get-the-get-dependencies-for-a-scene-file.145862/

as the title, I tried AssetDatabase.GetDependencies and EditorUtility.CollectDependencies, both can not work, any idea?

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. /// <summary>

  3. ///     We can't load the scene to collect dependencies, so let's get dirty and

  4. ///     parse the file for GUIDs.

  5. /// </summary>

  6. /// <param name="path">Path.</param>

  7. /// <param name="output">Output.</param>

  8. public static void GetSceneDependencies(string path, List<string> output)

  9. {

  10.     using (StreamReader reader = new StreamReader(path))

  11.     {

  12.         while (reader.Peek() >= 0)

  13.         {

  14.             string line = reader.ReadLine();

  15.             if (!line.Contains(GUID_PREFIX))

  16.                 continue;

  17.             string guidPart = line.Split(',')[1];

  18.             string guid = RemoveSubstring(guidPart, GUID_PREFIX).Trim();

  19.             output.Add(guid);

  20.         }

  21.     }

  22. }

  23. /// <summary>

  24. ///    Removes all instances of the given substring.

  25. /// </summary>

  26. /// <returns>The cleaned string.</returns>

  27. /// <param name="source">Source.</param>

  28. /// <param name="remove">Remove.</param>

  29. public static string RemoveSubstring(string source, string remove)

  30. {

  31.    int index = source.IndexOf(remove);

  32.    string clean = (index < 0) ? source : source.Remove(index, remove.Length);

  33.    

  34.    if (clean.Length != source.Length)

  35.      return RemoveSubstring(clean, remove);

  36.    return clean;

  37. }

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?

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:

发布了64 篇原创文章 · 获赞 36 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/kuangben2000/article/details/104043983
get