@@ -99,6 +99,12 @@ private static Dictionary<string, BuildTarget>
99
99
/// </summary>
100
100
public static string ASSET_LABEL = "gvh" ;
101
101
102
+ /// <summary>
103
+ /// Label which flags whether an asset should be disabled by renaming the file
104
+ /// (instead of using the PluginManager).
105
+ /// </summary>
106
+ public static string ASSET_LABEL_RENAME_TO_DISABLE = "gvh_rename_to_disable" ;
107
+
102
108
// Special build target name which enables all platforms / build targets.
103
109
public static string BUILD_TARGET_NAME_ANY = "any" ;
104
110
@@ -267,6 +273,11 @@ public static string ScriptingRuntimeDotNetVersion {
267
273
/// </summary>
268
274
public bool isManifest = false ;
269
275
276
+ /// <summary>
277
+ /// Set if this references an asset which is handled by PluginManager.
278
+ /// </summary>
279
+ public bool isHandledByPluginImporter = false ;
280
+
270
281
/// <summary>
271
282
/// List of compatible .NET versions parsed from this asset.
272
283
/// </summary>
@@ -306,6 +317,8 @@ public FileMetadata(string filename) {
306
317
foreach ( string label in AssetDatabase . GetLabels ( importer ) ) {
307
318
ParseLabel ( label ) ;
308
319
}
320
+
321
+ isHandledByPluginImporter = typeof ( PluginImporter ) . IsInstanceOfType ( importer ) ;
309
322
}
310
323
311
324
// On Windows the AssetDatabase converts native path separators
@@ -515,6 +528,10 @@ public void UpdateAssetLabels() {
515
528
}
516
529
if ( targets != null && targets . Length > 0 ) {
517
530
labels . Add ( CreateLabel ( TOKEN_TARGETS , targets ) ) ;
531
+
532
+ if ( ! isHandledByPluginImporter ) {
533
+ labels . Add ( ASSET_LABEL_RENAME_TO_DISABLE ) ;
534
+ }
518
535
}
519
536
if ( dotNetTargets != null && dotNetTargets . Length > 0 ) {
520
537
labels . Add ( CreateLabel ( TOKEN_DOTNET_TARGETS , dotNetTargets ) ) ;
@@ -1429,12 +1446,15 @@ private string LibraryPrefix {
1429
1446
"Google.VersionHandler.RenameToCanonicalFilenames" ;
1430
1447
private const string PREFERENCE_VERBOSE_LOGGING_ENABLED =
1431
1448
"Google.VersionHandler.VerboseLoggingEnabled" ;
1449
+ private const string PREFERENCE_RENAME_TO_DISABLE_FILES_ENABLED =
1450
+ "Google.VersionHandler.RenameToDisableFilesEnabled" ;
1432
1451
// List of preference keys, used to restore default settings.
1433
1452
private static string [ ] PREFERENCE_KEYS = new [ ] {
1434
1453
PREFERENCE_ENABLED ,
1435
1454
PREFERENCE_CLEANUP_PROMPT_ENABLED ,
1436
1455
PREFERENCE_RENAME_TO_CANONICAL_FILENAMES ,
1437
- PREFERENCE_VERBOSE_LOGGING_ENABLED
1456
+ PREFERENCE_VERBOSE_LOGGING_ENABLED ,
1457
+ PREFERENCE_RENAME_TO_DISABLE_FILES_ENABLED
1438
1458
} ;
1439
1459
1440
1460
// Name of this plugin.
@@ -1466,6 +1486,7 @@ static VersionHandlerImpl() {
1466
1486
static void UpdateVersionedAssetsOnUpdate ( ) {
1467
1487
UpdateVersionedAssets ( ) ;
1468
1488
NotifyWhenCompliationComplete ( false ) ;
1489
+ UpdateAssetsWithBuildTargets ( EditorUserBuildSettings . activeBuildTarget ) ;
1469
1490
}
1470
1491
1471
1492
/// <summary>
@@ -1606,6 +1627,16 @@ public static bool VerboseLoggingEnabled {
1606
1627
set { settings . SetBool ( PREFERENCE_VERBOSE_LOGGING_ENABLED , value ) ; }
1607
1628
}
1608
1629
1630
+ /// <summary>
1631
+ /// Enable / disable verbose logging.
1632
+ /// </summary>
1633
+ public static bool RenameToDisableFilesEnabled {
1634
+ get {
1635
+ return settings . GetBool ( PREFERENCE_RENAME_TO_DISABLE_FILES_ENABLED , defaultValue : true ) ;
1636
+ }
1637
+ set { settings . SetBool ( PREFERENCE_RENAME_TO_DISABLE_FILES_ENABLED , value ) ; }
1638
+ }
1639
+
1609
1640
/// <summary>
1610
1641
/// Set the methods to call when VersionHandler completes an update.
1611
1642
/// Each string in the specified list should have the format
@@ -1747,6 +1778,8 @@ public static void UpdateVersionedAssets(bool forceUpdate = false) {
1747
1778
// If this module is disabled do nothing.
1748
1779
if ( ! forceUpdate && ! Enabled ) return ;
1749
1780
1781
+ UpdateAssetsWithBuildTargets ( EditorUserBuildSettings . activeBuildTarget ) ;
1782
+
1750
1783
var metadataSet = FileMetadataSet . ParseFromFilenames ( FindAllAssets ( ) ) ;
1751
1784
// Rename linux libraries, if any are being tracked.
1752
1785
var linuxLibraries = new LinuxLibraryRenamer ( metadataSet ) ;
@@ -1825,6 +1858,70 @@ public static void UpdateVersionedAssets(bool forceUpdate = false) {
1825
1858
}
1826
1859
}
1827
1860
1861
+ /// <summary>
1862
+ /// Go through all files with build targets that need to be enabled/disabled by renaming
1863
+ /// (can't be handled through PluginHandler) and enable/disable them based on the whether they
1864
+ /// should build on currentBuildTarget.
1865
+ /// </summary>
1866
+ /// <param name="currentBuildTarget">
1867
+ /// The BuildTarget to use to determine which files should be enabled/disabled.
1868
+ /// </param>
1869
+ public static void UpdateAssetsWithBuildTargets ( BuildTarget currentBuildTarget ) {
1870
+ string [ ] assets = SearchAssetDatabase (
1871
+ assetsFilter : "l:" + FileMetadata . ASSET_LABEL_RENAME_TO_DISABLE ) ;
1872
+
1873
+ var metadataSet = FileMetadataSet . ParseFromFilenames ( assets ) ;
1874
+ foreach ( var versionPair in metadataSet . Values ) {
1875
+ // Disable all but the most recent version of the file.
1876
+ // Enable / disable most recent version based on whether its targets contain
1877
+ // the currently selected build target.
1878
+ foreach ( var versionData in versionPair . Values ) {
1879
+ bool enabled = false ;
1880
+ if ( versionData == versionPair . MostRecentVersion ) {
1881
+ if ( versionData . GetBuildTargetsSpecified ( ) ) {
1882
+ var buildTargets = versionData . GetBuildTargets ( ) ;
1883
+ enabled = buildTargets . Contains ( currentBuildTarget ) ;
1884
+ } else {
1885
+ enabled = true ;
1886
+ }
1887
+ }
1888
+
1889
+ SetFileEnabledByRename ( versionData , enabled ) ;
1890
+ }
1891
+ }
1892
+
1893
+ }
1894
+
1895
+ /// <summary>
1896
+ /// Enable or disable a file by renaming it (changing its extension to/from .xxx_DISABLED).
1897
+ /// </summary>
1898
+ /// <param name="metadata"> The metadata for the file which should be changed. </param>
1899
+ /// <param name="enabled"> The new state of the file. </param>
1900
+ private static void SetFileEnabledByRename ( FileMetadata metadata , bool enabled ) {
1901
+ if ( ! RenameToDisableFilesEnabled )
1902
+ return ;
1903
+
1904
+ string disableToken = "_DISABLED" ;
1905
+
1906
+ var filename = metadata . filename ;
1907
+ var newFilename = filename ;
1908
+ if ( enabled && filename . EndsWith ( disableToken ) ) {
1909
+ int tokenIndex = filename . LastIndexOf ( disableToken ) ;
1910
+ if ( tokenIndex > 0 ) {
1911
+ newFilename = filename . Substring ( 0 , tokenIndex ) ;
1912
+ }
1913
+ } else if ( ! enabled && ! filename . EndsWith ( disableToken ) ) {
1914
+ newFilename = filename + disableToken ;
1915
+ }
1916
+
1917
+ if ( filename == newFilename || newFilename == "" ) {
1918
+ return ;
1919
+ }
1920
+
1921
+ Log ( "Renaming file " + filename + " to " + newFilename , verbose : true ) ;
1922
+ AssetDatabase . MoveAsset ( filename , newFilename ) ;
1923
+ }
1924
+
1828
1925
// Returns the major/minor version of the unity environment we are running in
1829
1926
// as a float so it can be compared numerically.
1830
1927
public static float GetUnityVersionMajorMinor ( ) {
@@ -1878,6 +1975,52 @@ internal static string[] OnWillSaveAssets(string[] paths) {
1878
1975
return paths ;
1879
1976
}
1880
1977
}
1978
+
1979
+ /// <summary>
1980
+ /// Update assets when BuildTarget changes.
1981
+ /// </summary>
1982
+ [ InitializeOnLoad ]
1983
+ internal class BuildTargetChecker {
1984
+ private static BuildTarget ? lastKnownBuildTarget = null ;
1985
+ private static int ticksSinceLastCheck = 0 ;
1986
+ private static int ticksBetweenChecks = 60 ;
1987
+
1988
+
1989
+ static BuildTargetChecker ( ) {
1990
+ HandleSettingsChanged ( ) ;
1991
+ }
1992
+
1993
+ public static void HandleSettingsChanged ( ) {
1994
+ RunOnMainThread . OnUpdate -= CheckBuildTarget ;
1995
+ if ( Enabled && RenameToDisableFilesEnabled ) {
1996
+ RunOnMainThread . OnUpdate += CheckBuildTarget ;
1997
+ }
1998
+ }
1999
+
2000
+ private static void CheckBuildTarget ( ) {
2001
+ ticksSinceLastCheck ++ ;
2002
+ if ( ticksSinceLastCheck < ticksBetweenChecks ) {
2003
+ return ;
2004
+ }
2005
+
2006
+ if ( ! Enabled || ! RenameToDisableFilesEnabled ) {
2007
+ RunOnMainThread . OnUpdate -= CheckBuildTarget ;
2008
+ }
2009
+
2010
+ var newBuildTarget = EditorUserBuildSettings . activeBuildTarget ;
2011
+ if ( lastKnownBuildTarget == null || newBuildTarget != lastKnownBuildTarget ) {
2012
+ lastKnownBuildTarget = newBuildTarget ;
2013
+ HandleBuildTargetChanged ( newBuildTarget ) ;
2014
+ }
2015
+
2016
+ ticksSinceLastCheck = 0 ;
2017
+ }
2018
+
2019
+ private static void HandleBuildTargetChanged ( BuildTarget newBuildTarget ) {
2020
+ UpdateAssetsWithBuildTargets ( newBuildTarget ) ;
2021
+ }
2022
+
2023
+ }
1881
2024
}
1882
2025
1883
2026
} // namespace Google
0 commit comments