BuildReportTool.ReportManager.GetAllUnusedAssets C# (CSharp) Method

GetAllUnusedAssets() static private method

static private GetAllUnusedAssets ( BuildReportTool scriptDLLs, string projectAssetsPath, bool includeSvn, bool includeGit, BuildPlatform buildPlatform, bool includeUnusedPrefabs, int fileCountBatchSkip, int fileCountLimit, bool>.Dictionary usedAssetsDict, List inOutAllUsedAssets ) : BuildReportTool.SizePart[]
scriptDLLs BuildReportTool
projectAssetsPath string
includeSvn bool
includeGit bool
buildPlatform BuildPlatform
includeUnusedPrefabs bool
fileCountBatchSkip int
fileCountLimit int
usedAssetsDict bool>.Dictionary
inOutAllUsedAssets List
return BuildReportTool.SizePart[]
	static BuildReportTool.SizePart[] GetAllUnusedAssets(
		BuildReportTool.SizePart[] scriptDLLs,
		string projectAssetsPath,
		bool includeSvn, bool includeGit,
		BuildPlatform buildPlatform,
		bool includeUnusedPrefabs,
		int fileCountBatchSkip, int fileCountLimit,
		Dictionary<string, bool> usedAssetsDict,
		List<BuildReportTool.SizePart> inOutAllUsedAssets)
	{
		List<BuildReportTool.SizePart> unusedAssets = new List<BuildReportTool.SizePart>();


		// now loop through all assets in the whole project,
		// check if that file exists in the usedAssetsDict,
		// if not, include it in the unusedAssets list,
		// then sort by size

		int projectStringLen = projectAssetsPath.Length - "Assets".Length;

		bool has32BitPluginsFolder = Directory.Exists(projectAssetsPath + "/Plugins/x86");
		bool has64BitPluginsFolder = Directory.Exists(projectAssetsPath + "/Plugins/x86_64");

		string currentAsset = "";

		int assetIdx = 0;

		int fileCountOffset = fileCountBatchSkip * fileCountLimit;

		foreach (string fullAssetPath in DldUtil.TraverseDirectory.Do(projectAssetsPath))
		{
			++assetIdx;

			if (assetIdx < fileCountOffset)
			{
				continue;
			}

			BRT_BuildReportWindow.GetValueMessage = "Getting list of used assets " + assetIdx + " ...";

			//Debug.Log(fullAssetPath);

			//string fullAssetPath = allAssets[assetIdx];

			currentAsset = fullAssetPath;
			currentAsset = currentAsset.Substring(projectStringLen, currentAsset.Length - projectStringLen);

			// Unity .meta files are not considered part of the assets
			// Unity .mask (Avatar masks): whether a .mask file is used or not currently cannot be reliably found out, so they are skipped
			// anything in a /Resources/ folder will always be in the build, so don't bother checking for it
			if (Util.IsFileOfType(currentAsset, ".meta") || Util.IsFileOfType(currentAsset, ".mask") || Util.IsFileInAPath(currentAsset, "/resources/"))
			{
				continue;
			}

			// include version control files only if requested to do so
			if (!includeSvn && Util.IsFileInAPath(currentAsset, "/.svn/"))
			{
				continue;
			}
			if (!includeGit && Util.IsFileInAPath(currentAsset, "/.git/"))
			{
				continue;
			}

			// NOTE: if a .dll is present in the Script DLLs list, that means
			// it is a managed DLL, and thus, is always used in the build

			if (Util.IsFileOfType(currentAsset, ".dll"))
			{
				string assetFilenameOnly = Path.GetFileName(currentAsset);
				//Debug.Log(assetFilenameOnly);

				bool foundMatch = false;

				// is current asset found in the script DLLs list?
				for (int mdllIdx = 0; mdllIdx < scriptDLLs.Length; ++mdllIdx)
				{
					if (scriptDLLs[mdllIdx].Name == assetFilenameOnly)
					{
						// it's a managed DLL. Managed DLLs are always included in the build.
						foundMatch = true;
						inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
						break;
					}
				}

				if (foundMatch)
				{
					continue;
				}
			}


			// per platform special cases
			// involving native plugins

			// in windows and linux, the issue gets dicey as we have to check if its a 32 bit, 64 bit, or universal build

			// so for windows/linux 32 bit, if Assets/Plugins/x86 exists, it will include all dll/so in those. if that folder does not exist, all dll/so in Assets/Plugins are included instead.
			//
			// what if there's a 64 bit dll/so in Assets/Plugins? surely it would not get included in a 32 bit build?

			// for windows/linux 64 bit, if Assets/Plugins/x86_64 exists, it will include all dll/so in those. if that folder does not exist, all dll/so in Assets/Plugins are included instead.

			// right now there is no such thing as a windows universal build

			// For linux universal build, any .so in Assets/Plugins/x86 and Assets/Plugins/x86_64 are included. No .so in Assets/Plugins will be included (as it wouldn't be able to determine if such an .so in that folder is 32 or 64 bit) i.e. it relies on the .so being in the x86 or x86_64 subfolder to determine which is the 32 bit and which is the 64 bit version


			// NOTE: in Unity 3.x there is no Linux build target, but there is Windows 32/64 bit

/*
			from http://docs.unity3d.com/Documentation/Manual/PluginsForDesktop.html

			On Windows and Linux, plugins can be managed manually (e.g, before building a 64-bit player, you copy the 64-bit library into the Assets/Plugins folder, and before building a 32-bit player, you copy the 32-bit library into the Assets/Plugins folder)

				OR you can place the 32-bit version of the plugin in Assets/Plugins/x86 and the 64-bit version of the plugin in Assets/Plugins/x86_64.

			By default the editor will look in the architecture-specific sub-directory first, and if that directory does not exist, it will use plugins from the root Assets/Plugins folder instead.

			Note that for the Universal Linux build, you are required to use the architecture-specific sub-directories (when building a Universal Linux build, the Editor will not copy any plugins from the root Assets/Plugins folder).

			For Mac OS X, you should build your plugin as a universal binary that contains both 32-bit and 64-bit architectures.
*/

			switch (buildPlatform)
			{
				case BuildPlatform.Android:
					// .jar files inside /Assets/Plugins/Android/ are always included in the build if built for Android
					if (Util.IsFileInAPath(currentAsset, "assets/plugins/android/") && (Util.IsFileOfType(currentAsset, ".jar") || Util.IsFileOfType(currentAsset, ".so")))
					{
						//Debug.Log(".jar file in android " + currentAsset);
						inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
						continue;
					}
					break;

				case BuildPlatform.iOS:
					if (Util.IsFileOfType(currentAsset, ".a") || Util.IsFileOfType(currentAsset, ".m") || Util.IsFileOfType(currentAsset, ".mm") || Util.IsFileOfType(currentAsset, ".c") || Util.IsFileOfType(currentAsset, ".cpp"))
					{
						// any .a, .m, .mm, .c, or .cpp files inside Assets/Plugins/iOS are automatically symlinked/used
						if (Util.IsFileInAPath(currentAsset, "assets/plugins/ios/"))
						{
							inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
						}
						// if there are any .a, .m, .mm, .c, or .cpp files outside of Assets/Plugins/iOS
						// we can't determine if they are really used or not because the user may manually copy them to the Xcode project, or a post-process .sh script may copy them to the Xcode project.
						// so we don't put them in the unused assets list
						continue;
					}
					break;



				case BuildPlatform.MacOSX32:
					// when in mac build, .bundle files that are in Assets/Plugins are always included
					// supposedly, Unity expects all .bundle files as universal builds (even if this is only a 32-bit build?)
					if (Util.IsFileInAPath(currentAsset, "assets/plugins/") && Util.IsFileOfType(currentAsset, ".bundle"))
					{
						inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
						continue;
					}
					break;
				case BuildPlatform.MacOSX64:
					// when in mac build, .bundle files that are in Assets/Plugins are always included
					// supposedly, Unity expects all .bundle files as universal builds (even if this is only a 64-bit build?)
					if (Util.IsFileInAPath(currentAsset, "assets/plugins/") && Util.IsFileOfType(currentAsset, ".bundle"))
					{
						inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
						continue;
					}
					break;
				case BuildPlatform.MacOSXUniversal:
					// when in mac build, .bundle files that are in Assets/Plugins are always included
					// supposedly, Unity expects all .bundle files as universal builds
					if (Util.IsFileInAPath(currentAsset, "assets/plugins/") && Util.IsFileOfType(currentAsset, ".bundle"))
					{
						inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
						continue;
					}
					break;



				case BuildPlatform.Windows32:
					if (Util.IsFileOfType(currentAsset, ".dll"))
					{
						if (Util.IsFileInAPath(currentAsset, "assets/plugins/x86/"))
						{
							inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
							continue;
						}
						// Unity only makes use of Assets/Plugins/ if Assets/Plugins/x86/ does not exist
						else if (Util.IsFileInAPath(currentAsset, "assets/plugins/") && !has32BitPluginsFolder)
						{
							inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
							continue;
						}
					}
					break;

				case BuildPlatform.Windows64:
					if (Util.IsFileOfType(currentAsset, ".dll"))
					{
						if (Util.IsFileInAPath(currentAsset, "assets/plugins/x86_64/"))
						{
							inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
							continue;
						}
						// Unity only makes use of Assets/Plugins/ if Assets/Plugins/x86_64/ does not exist
						else if (Util.IsFileInAPath(currentAsset, "assets/plugins/") && !has64BitPluginsFolder)
						{
							inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
							continue;
						}
					}
					break;



				case BuildPlatform.Linux32:
					if (Util.IsFileOfType(currentAsset, ".so"))
					{
						if (Util.IsFileInAPath(currentAsset, "assets/plugins/x86/"))
						{
							inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
							continue;
						}
						// Unity only makes use of Assets/Plugins/ if Assets/Plugins/x86/ does not exist
						else if (Util.IsFileInAPath(currentAsset, "assets/plugins/") && !has32BitPluginsFolder)
						{
							inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
							continue;
						}
					}
					break;

				case BuildPlatform.Linux64:
					if (Util.IsFileOfType(currentAsset, ".so"))
					{
						if (Util.IsFileInAPath(currentAsset, "assets/plugins/x86_64/"))
						{
							inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
							continue;
						}
						// Unity only makes use of Assets/Plugins/ if Assets/Plugins/x86_64/ does not exist
						else if (Util.IsFileInAPath(currentAsset, "assets/plugins/") && !has64BitPluginsFolder)
						{
							inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
							continue;
						}
					}
					break;

				case BuildPlatform.LinuxUniversal:
					if (Util.IsFileOfType(currentAsset, ".so"))
					{
						if (Util.IsFileInAPath(currentAsset, "assets/plugins/x86/") || Util.IsFileInAPath(currentAsset, "assets/plugins/x86_64/"))
						{
							inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
							continue;
						}
					}
					break;
			}

			// check prefabs only when requested to do so
			if (Util.IsFileOfType(currentAsset, ".prefab"))
			{
				//Debug.Log("GetAllUnusedAssets: found prefab: " + Path.GetFileName(currentAsset));
				if (!includeUnusedPrefabs)
				{
					continue;
				}
			}

			// assets in StreamingAssets folder are always included
			if (Util.IsFileInAPath(currentAsset, "assets/streamingassets"))
			{
				inOutAllUsedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
				continue;
			}

			// if asset not in used assets list
			if (!usedAssetsDict.ContainsKey(currentAsset))
			{
				// then that simply means this asset is unused
				unusedAssets.Add(BuildReportTool.Util.CreateSizePartFromFile(currentAsset, fullAssetPath));
			}

			if (unusedAssets.Count >= fileCountLimit)
			{
				break;
			}
		}

		// now sort it all by file size
		unusedAssets.Sort(delegate(BuildReportTool.SizePart b1, BuildReportTool.SizePart b2) {
			if (b1.SizeBytes > b2.SizeBytes) return -1;
			if (b1.SizeBytes < b2.SizeBytes) return 1;
			return 0;
		});

		return unusedAssets.ToArray();
	}

Same methods

ReportManager::GetAllUnusedAssets ( string scenesIncludedInProject, BuildReportTool scriptDLLs, string projectAssetsPath, bool includeSvn, bool includeGit, BuildPlatform buildPlatform, bool includeUnusedPrefabs, int fileCountBatchSkip, int fileCountLimit, List inOutAllUsedAssets ) : BuildReportTool.SizePart[]