@@ -28,9 +28,6 @@ internal sealed class BuildProfileContext : ScriptableObject
28
28
const string k_SharedProfilePath = $ "{ k_BuildProfilePath } /SharedProfile.asset";
29
29
static BuildProfileContext s_Instance ;
30
30
31
- [ SerializeField ]
32
- BuildProfile m_ActiveProfile ;
33
-
34
31
[ SerializeField ]
35
32
string [ ] m_CachedEditorScriptingDefines = Array . Empty < string > ( ) ;
36
33
@@ -64,54 +61,57 @@ internal string[] cachedEditorScriptingDefines
64
61
/// perspective Classic Platform and Build Profiles are different concepts.
65
62
/// </remarks>
66
63
[ VisibleToOtherModules ]
67
- internal BuildProfile activeProfile
64
+ internal static BuildProfile activeProfile
68
65
{
69
66
get
70
67
{
71
68
// Active Build profile may be deleted from the project.
72
- if ( m_ActiveProfile != null && m_ActiveProfile . CanBuildLocally ( ) )
73
- return m_ActiveProfile ;
69
+ var activeProfile = EditorUserBuildSettings . activeBuildProfile ;
70
+ if ( activeProfile != null && activeProfile . CanBuildLocally ( ) )
71
+ return activeProfile ;
74
72
75
- m_ActiveProfile = null ;
76
73
return null ;
77
74
}
78
75
79
76
set
80
77
{
81
- if ( m_ActiveProfile == value )
82
- return ;
78
+ var prev = EditorUserBuildSettings . activeBuildProfile ;
83
79
84
- var prev = m_ActiveProfile ;
85
80
if ( value == null || value . platformBuildProfile == null )
86
81
{
87
- m_ActiveProfile . UpdateGlobalManagerPlayerSettings ( activeWillBeRemoved : true ) ;
88
- m_ActiveProfile = null ;
89
- Save ( ) ;
90
- EditorUserBuildSettings . SetBuildProfilePath ( string . Empty ) ;
91
- activeProfileChanged ? . Invoke ( prev , m_ActiveProfile ) ;
82
+ prev ? . UpdateGlobalManagerPlayerSettings ( activeWillBeRemoved : true ) ;
83
+ EditorUserBuildSettings . activeBuildProfile = null ;
84
+
85
+ activeProfileChanged ? . Invoke ( prev , null ) ;
92
86
OnActiveProfileChangedForSettingExtension ( prev , null ) ;
93
87
BuildProfileModuleUtil . RequestScriptCompilation ( null ) ;
94
88
return ;
95
89
}
96
90
97
- if ( m_PlatformIdToClassicPlatformProfile . TryGetValue (
91
+ // Only compare prev with value after the null check, as
92
+ // EditorUserBuildSettings.activeBuildProfile will return null
93
+ // if the build profile has been destroyed but on native side
94
+ // it's still pointing to a dead pptr.
95
+ if ( ReferenceEquals ( prev , value ) )
96
+ return ;
97
+
98
+ if ( s_Instance != null && s_Instance . m_PlatformIdToClassicPlatformProfile . TryGetValue (
98
99
value . platformId , out var entry ) && entry == value )
99
100
{
100
101
Debug . LogWarning ( "[BuildProfile] Classic Platforms cannot be set as the active build profile." ) ;
101
102
return ;
102
103
}
103
104
104
- m_ActiveProfile = value ;
105
- Save ( ) ;
106
- EditorUserBuildSettings . SetBuildProfilePath ( AssetDatabase . GetAssetPath ( m_ActiveProfile ) ) ;
107
- activeProfileChanged ? . Invoke ( prev , m_ActiveProfile ) ;
108
- OnActiveProfileChangedForSettingExtension ( prev , m_ActiveProfile ) ;
109
- m_ActiveProfile . UpdateGlobalManagerPlayerSettings ( ) ;
110
- BuildProfileModuleUtil . RequestScriptCompilation ( m_ActiveProfile ) ;
105
+ EditorUserBuildSettings . activeBuildProfile = value ;
106
+
107
+ activeProfileChanged ? . Invoke ( prev , value ) ;
108
+ OnActiveProfileChangedForSettingExtension ( prev , value ) ;
109
+ value . UpdateGlobalManagerPlayerSettings ( ) ;
110
+ BuildProfileModuleUtil . RequestScriptCompilation ( value ) ;
111
111
}
112
112
}
113
113
114
- void OnActiveProfileChangedForSettingExtension ( BuildProfile previous , BuildProfile newProfile )
114
+ static void OnActiveProfileChangedForSettingExtension ( BuildProfile previous , BuildProfile newProfile )
115
115
{
116
116
BuildTargetDiscovery . TryGetBuildTarget ( EditorUserBuildSettings . activeBuildTarget , out IBuildTarget iBuildTarget ) ;
117
117
if ( iBuildTarget == null )
@@ -216,7 +216,7 @@ internal static BuildProfile GetActiveOrClassicBuildProfile(
216
216
BuildTarget target , StandaloneBuildSubtarget subTarget = StandaloneBuildSubtarget . Default , string sharedSetting = null )
217
217
{
218
218
if ( ShouldReturnActiveProfile ( target , subTarget , sharedSetting ) )
219
- return instance . activeProfile ;
219
+ return activeProfile ;
220
220
221
221
// For backwards compatibility, getter will look for
222
222
// the classic platform build profile for the target platform
@@ -331,8 +331,6 @@ void SyncActiveProfileToFallback()
331
331
void OnDisable ( )
332
332
{
333
333
Save ( ) ;
334
- EditorUserBuildSettings . SetBuildProfilePath ( ( m_ActiveProfile != null ) ?
335
- AssetDatabase . GetAssetPath ( m_ActiveProfile ) : string . Empty ) ;
336
334
337
335
// Platform profiles must be manually serialized for changes to persist.
338
336
foreach ( var kvp in m_PlatformIdToClassicPlatformProfile )
@@ -405,13 +403,19 @@ void OnEnable()
405
403
sharedProfile = sharedProfileObj ;
406
404
}
407
405
408
- var buildProfile = activeProfile ?? GetForClassicPlatform ( EditorUserBuildSettings . activeBuildTarget , EditorUserBuildSettings . standaloneBuildSubtarget ) ;
406
+ var buildProfile = activeProfile ;
409
407
410
- // profile can be null if we're in the middle of creating classic profiles
411
408
if ( buildProfile == null )
412
- return ;
409
+ {
410
+ buildProfile = GetForClassicPlatform ( EditorUserBuildSettings . activeBuildTarget , EditorUserBuildSettings . standaloneBuildSubtarget ) ;
413
411
414
- EditorUserBuildSettings . CopyToBuildProfile ( buildProfile ) ;
412
+ // profile can be null if we're in the middle of creating classic profiles
413
+ if ( buildProfile == null )
414
+ return ;
415
+
416
+ // We only copy EditorUserBuildSettings into the build profile for classic platforms as we don't want to modify actual user assets
417
+ EditorUserBuildSettings . CopyToBuildProfile ( buildProfile ) ;
418
+ }
415
419
416
420
string module = BuildTargetDiscovery . GetModuleNameForBuildTarget ( buildProfile . buildTarget ) ;
417
421
var extension = ModuleManager . GetBuildProfileExtension ( module ) ;
@@ -587,13 +591,11 @@ static void CreateOrLoad()
587
591
System . Diagnostics . Debug . Assert ( s_Instance != null ) ;
588
592
s_Instance . CheckInstalledBuildPlatforms ( ) ;
589
593
590
- EditorUserBuildSettings . SetBuildProfilePath ( ( s_Instance . m_ActiveProfile != null ) ?
591
- AssetDatabase . GetAssetPath ( s_Instance . m_ActiveProfile ) : string . Empty ) ;
592
594
s_Instance . cachedEditorScriptingDefines = BuildDefines . GetBuildProfileScriptDefines ( ) ;
593
595
594
596
BuildProfileModuleUtil . DeleteLastRunnableBuildKeyForDeletedProfiles ( ) ;
595
597
596
- s_Instance . OnActiveProfileChangedForSettingExtension ( null , s_Instance . m_ActiveProfile ) ;
598
+ OnActiveProfileChangedForSettingExtension ( null , activeProfile ) ;
597
599
}
598
600
599
601
[ RequiredByNativeCode , UsedImplicitly ]
@@ -620,6 +622,12 @@ static void SetActiveOrClassicProfileRawPlatformSetting(string settingName, stri
620
622
}
621
623
}
622
624
625
+ [ RequiredByNativeCode , UsedImplicitly ]
626
+ static void EnsureInitialized ( )
627
+ {
628
+ GC . KeepAlive ( instance ) ;
629
+ }
630
+
623
631
[ RequiredByNativeCode , UsedImplicitly ]
624
632
static string GetActiveOrClassicProfileRawPlatformSetting ( string settingName , BuildTarget target , StandaloneBuildSubtarget subtarget )
625
633
{
@@ -643,16 +651,17 @@ static string GetActiveOrClassicProfileRawPlatformSetting(string settingName, Bu
643
651
[ RequiredByNativeCode ]
644
652
static string GetActiveBuildProfilePath ( )
645
653
{
646
- if ( instance . activeProfile )
647
- return AssetDatabase . GetAssetPath ( instance . activeProfile ) ;
654
+ var activeProfile = BuildProfileContext . activeProfile ;
655
+ if ( activeProfile )
656
+ return AssetDatabase . GetAssetPath ( activeProfile ) ;
648
657
649
658
return string . Empty ;
650
659
}
651
660
652
661
[ RequiredByNativeCode ]
653
662
static bool HasActiveProfileWithPlayerSettings ( out int instanceID )
654
663
{
655
- var activeProfile = instance . activeProfile ;
664
+ var activeProfile = BuildProfileContext . activeProfile ;
656
665
if ( activeProfile ? . playerSettings != null )
657
666
{
658
667
instanceID = activeProfile . GetInstanceID ( ) ;
@@ -666,15 +675,15 @@ static bool HasActiveProfileWithPlayerSettings(out int instanceID)
666
675
[ RequiredByNativeCode ]
667
676
static void UpdateActiveProfilePlayerSettingsObjectFromYAML ( )
668
677
{
669
- instance . activeProfile ? . UpdatePlayerSettingsObjectFromYAML ( ) ;
678
+ activeProfile ? . UpdatePlayerSettingsObjectFromYAML ( ) ;
670
679
}
671
680
672
681
static bool ShouldReturnActiveProfile ( BuildTarget buildTarget , StandaloneBuildSubtarget subtarget , string sharedSetting = null )
673
682
{
674
683
if ( ! string . IsNullOrEmpty ( sharedSetting ) )
675
684
return IsSharedSettingEnabledInActiveProfile ( sharedSetting ) ;
676
685
677
- var activeProfile = instance . activeProfile ;
686
+ var activeProfile = BuildProfileContext . activeProfile ;
678
687
if ( activeProfile == null || buildTarget == BuildTarget . NoTarget )
679
688
return false ;
680
689
@@ -684,7 +693,7 @@ static bool ShouldReturnActiveProfile(BuildTarget buildTarget, StandaloneBuildSu
684
693
685
694
static bool IsSharedSettingEnabledInActiveProfile ( string settingName )
686
695
{
687
- var activeProfile = instance . activeProfile ;
696
+ var activeProfile = BuildProfileContext . activeProfile ;
688
697
if ( activeProfile == null )
689
698
return false ;
690
699
0 commit comments