@@ -1718,61 +1718,87 @@ internal static void OnSettingsChanged() {
1718
1718
/// Label a set of assets that should be managed by this plugin.
1719
1719
/// </summary>
1720
1720
/// <param name="assetPaths">Set of assets to label.</param>
1721
+ /// <param name="complete">Called when the operation is complete with the set of assets
1722
+ /// that could not be labeled.</param>
1723
+ /// <param name="synchronous">Whether to block until asset labeling is complete.</param>
1724
+ /// <param name="progressUpdate">Called with the progress (0..1) and message that indicates
1725
+ /// processing progress.</param>
1721
1726
/// <param name="displayWarning">Whether to display a warning if assets can't be
1722
1727
/// labeled.</param>
1723
1728
/// <param name="recursive">Whether to label assets in subdirectories of the specified
1724
1729
/// assetPaths.</param>
1725
- /// <returns>List of assets that could not be labeled.</returns>
1726
- internal static HashSet < string > LabelAssets ( IEnumerable < string > assetPaths ,
1727
- bool displayWarning = true ,
1728
- bool recursive = false ) {
1729
- var assetsWithoutAssetImporter = new HashSet < string > ( assetPaths ) ;
1730
- if ( assetsWithoutAssetImporter . Count == 0 ) return assetsWithoutAssetImporter ;
1730
+ /// <returns></returns>
1731
+ internal static void LabelAssets ( IEnumerable < string > assetPaths ,
1732
+ Action < HashSet < string > > complete = null ,
1733
+ bool synchronous = true ,
1734
+ Action < float , string > progressUpdate = null ,
1735
+ bool displayWarning = true ,
1736
+ bool recursive = false ) {
1737
+ var assetsWithoutAssetImporter = new HashSet < string > ( ) ;
1731
1738
var projectDataFolder = Path . GetFullPath ( Application . dataPath ) ;
1732
- foreach ( var assetPath in new List < string > ( assetsWithoutAssetImporter ) ) {
1733
- // Ignore asset meta files which are used to store the labels and files that
1734
- // are not in the project.
1735
- var fullAssetPath = Path . GetFullPath ( assetPath ) ;
1736
- if ( assetPath . EndsWith ( ".meta" ) || ! fullAssetPath . StartsWith ( projectDataFolder ) ) {
1737
- assetsWithoutAssetImporter . Remove ( assetPath ) ;
1738
- continue ;
1739
- }
1739
+ var assetsToProcess = new List < string > ( assetPaths ) ;
1740
+ int totalAssets = assetsToProcess . Count ;
1741
+ RunOnMainThread . PollOnUpdateUntilComplete ( ( ) => {
1742
+ var remainingAssets = assetsToProcess . Count ;
1743
+ // Display summary of processing and call the completion function.
1744
+ if ( remainingAssets == 0 ) {
1745
+ if ( assetsWithoutAssetImporter . Count > 0 && displayWarning ) {
1746
+ Log ( String . Format (
1747
+ "Failed to add tracking label {0} to some assets.\n \n " +
1748
+ "The following files will not be managed by this module:\n " +
1749
+ "{1}\n " , ManagedAssetLabel ,
1750
+ String . Join (
1751
+ "\n " , new List < string > ( assetsWithoutAssetImporter ) . ToArray ( ) ) ) ,
1752
+ level : LogLevel . Warning ) ;
1753
+ }
1754
+ if ( complete != null ) complete ( assetsWithoutAssetImporter ) ;
1755
+ return true ;
1756
+ }
1757
+ var assetPath = assetsToProcess [ 0 ] ;
1758
+ assetsToProcess . RemoveAt ( 0 ) ;
1759
+ // Ignore asset meta files which are used to store the labels and files that
1760
+ // are not in the project.
1761
+ var fullAssetPath = Path . GetFullPath ( assetPath ) ;
1762
+ if ( assetPath . EndsWith ( ".meta" ) ||
1763
+ ! fullAssetPath . StartsWith ( projectDataFolder ) ) {
1764
+ return false ;
1765
+ }
1740
1766
1741
- // Get the relative path of this asset.
1742
- var relativeAssetPath = Path . Combine (
1743
- Path . GetFileName ( projectDataFolder ) ,
1744
- fullAssetPath . Substring ( projectDataFolder . Length + 1 ) ) ;
1767
+ // Get the relative path of this asset.
1768
+ var relativeAssetPath = Path . Combine (
1769
+ Path . GetFileName ( projectDataFolder ) ,
1770
+ fullAssetPath . Substring ( projectDataFolder . Length + 1 ) ) ;
1745
1771
1746
- // If the asset is a directory, add labels to the contents.
1747
- if ( recursive && Directory . Exists ( relativeAssetPath ) ) {
1748
- assetsWithoutAssetImporter . UnionWith (
1749
- LabelAssets ( Directory . GetFileSystemEntries ( relativeAssetPath ) ,
1750
- displayWarning : false ) ) ;
1751
- }
1772
+ if ( progressUpdate != null ) {
1773
+ progressUpdate ( ( float ) ( totalAssets - remainingAssets ) /
1774
+ ( float ) totalAssets , relativeAssetPath ) ;
1775
+ }
1752
1776
1753
- // It's likely files have been added or removed without using AssetDatabase methods
1754
- // so (re)import the asset to make sure it's in the AssetDatabase.
1755
- AssetDatabase . ImportAsset ( relativeAssetPath ,
1756
- options : ImportAssetOptions . ForceSynchronousImport ) ;
1757
-
1758
- // Add the label to the asset.
1759
- AssetImporter importer = AssetImporter . GetAtPath ( relativeAssetPath ) ;
1760
- if ( importer != null ) {
1761
- var labels = new HashSet < string > ( AssetDatabase . GetLabels ( importer ) ) ;
1762
- labels . Add ( ManagedAssetLabel ) ;
1763
- AssetDatabase . SetLabels ( importer , ( new List < string > ( labels ) ) . ToArray ( ) ) ;
1764
- assetsWithoutAssetImporter . Remove ( assetPath ) ;
1765
- }
1766
- }
1767
- if ( assetsWithoutAssetImporter . Count > 0 && displayWarning ) {
1768
- Log ( String . Format (
1769
- "Failed to add tracking label {0} to some assets.\n \n " +
1770
- "The following files will not be managed by this module:\n " +
1771
- "{1}\n " , ManagedAssetLabel ,
1772
- String . Join ( "\n " , new List < string > ( assetsWithoutAssetImporter ) . ToArray ( ) ) ) ,
1773
- level : LogLevel . Warning ) ;
1774
- }
1775
- return assetsWithoutAssetImporter ;
1777
+ // If the asset is a directory, add labels to the contents.
1778
+ if ( recursive && Directory . Exists ( relativeAssetPath ) ) {
1779
+ var contents = new List < string > (
1780
+ Directory . GetFileSystemEntries ( relativeAssetPath ) ) ;
1781
+ totalAssets += contents . Count ;
1782
+ assetsToProcess . AddRange ( contents ) ;
1783
+ return false ;
1784
+ }
1785
+
1786
+ // It's likely files have been added or removed without using AssetDatabase
1787
+ // methods so (re)import the asset to make sure it's in the AssetDatabase.
1788
+ AssetDatabase . ImportAsset ( relativeAssetPath ,
1789
+ options : ImportAssetOptions . ForceSynchronousImport ) ;
1790
+
1791
+ // Add the label to the asset.
1792
+ AssetImporter importer = AssetImporter . GetAtPath ( relativeAssetPath ) ;
1793
+ if ( importer != null ) {
1794
+ var labels = new HashSet < string > ( AssetDatabase . GetLabels ( importer ) ) ;
1795
+ labels . Add ( ManagedAssetLabel ) ;
1796
+ AssetDatabase . SetLabels ( importer , ( new List < string > ( labels ) ) . ToArray ( ) ) ;
1797
+ } else {
1798
+ assetsWithoutAssetImporter . Add ( assetPath ) ;
1799
+ }
1800
+ return false ;
1801
+ } , synchronous : synchronous ) ;
1776
1802
}
1777
1803
1778
1804
/// <summary>
0 commit comments