@@ -28,9 +28,6 @@ internal sealed class BuildProfileContext : ScriptableObject
2828 const string k_SharedProfilePath = $ "{ k_BuildProfilePath } /SharedProfile.asset";
2929 static BuildProfileContext s_Instance ;
3030
31- [ SerializeField ]
32- BuildProfile m_ActiveProfile ;
33-
3431 [ SerializeField ]
3532 string [ ] m_CachedEditorScriptingDefines = Array . Empty < string > ( ) ;
3633
@@ -64,54 +61,57 @@ internal string[] cachedEditorScriptingDefines
6461 /// perspective Classic Platform and Build Profiles are different concepts.
6562 /// </remarks>
6663 [ VisibleToOtherModules ]
67- internal BuildProfile activeProfile
64+ internal static BuildProfile activeProfile
6865 {
6966 get
7067 {
7168 // 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 ;
7472
75- m_ActiveProfile = null ;
7673 return null ;
7774 }
7875
7976 set
8077 {
81- if ( m_ActiveProfile == value )
82- return ;
78+ var prev = EditorUserBuildSettings . activeBuildProfile ;
8379
84- var prev = m_ActiveProfile ;
8580 if ( value == null || value . platformBuildProfile == null )
8681 {
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 ) ;
9286 OnActiveProfileChangedForSettingExtension ( prev , null ) ;
9387 BuildProfileModuleUtil . RequestScriptCompilation ( null ) ;
9488 return ;
9589 }
9690
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 (
9899 value . platformId , out var entry ) && entry == value )
99100 {
100101 Debug . LogWarning ( "[BuildProfile] Classic Platforms cannot be set as the active build profile." ) ;
101102 return ;
102103 }
103104
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 ) ;
111111 }
112112 }
113113
114- void OnActiveProfileChangedForSettingExtension ( BuildProfile previous , BuildProfile newProfile )
114+ static void OnActiveProfileChangedForSettingExtension ( BuildProfile previous , BuildProfile newProfile )
115115 {
116116 BuildTargetDiscovery . TryGetBuildTarget ( EditorUserBuildSettings . activeBuildTarget , out IBuildTarget iBuildTarget ) ;
117117 if ( iBuildTarget == null )
@@ -216,7 +216,7 @@ internal static BuildProfile GetActiveOrClassicBuildProfile(
216216 BuildTarget target , StandaloneBuildSubtarget subTarget = StandaloneBuildSubtarget . Default , string sharedSetting = null )
217217 {
218218 if ( ShouldReturnActiveProfile ( target , subTarget , sharedSetting ) )
219- return instance . activeProfile ;
219+ return activeProfile ;
220220
221221 // For backwards compatibility, getter will look for
222222 // the classic platform build profile for the target platform
@@ -331,8 +331,6 @@ void SyncActiveProfileToFallback()
331331 void OnDisable ( )
332332 {
333333 Save ( ) ;
334- EditorUserBuildSettings . SetBuildProfilePath ( ( m_ActiveProfile != null ) ?
335- AssetDatabase . GetAssetPath ( m_ActiveProfile ) : string . Empty ) ;
336334
337335 // Platform profiles must be manually serialized for changes to persist.
338336 foreach ( var kvp in m_PlatformIdToClassicPlatformProfile )
@@ -405,13 +403,19 @@ void OnEnable()
405403 sharedProfile = sharedProfileObj ;
406404 }
407405
408- var buildProfile = activeProfile ?? GetForClassicPlatform ( EditorUserBuildSettings . activeBuildTarget , EditorUserBuildSettings . standaloneBuildSubtarget ) ;
406+ var buildProfile = activeProfile ;
409407
410- // profile can be null if we're in the middle of creating classic profiles
411408 if ( buildProfile == null )
412- return ;
409+ {
410+ buildProfile = GetForClassicPlatform ( EditorUserBuildSettings . activeBuildTarget , EditorUserBuildSettings . standaloneBuildSubtarget ) ;
413411
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+ }
415419
416420 string module = BuildTargetDiscovery . GetModuleNameForBuildTarget ( buildProfile . buildTarget ) ;
417421 var extension = ModuleManager . GetBuildProfileExtension ( module ) ;
@@ -587,13 +591,11 @@ static void CreateOrLoad()
587591 System . Diagnostics . Debug . Assert ( s_Instance != null ) ;
588592 s_Instance . CheckInstalledBuildPlatforms ( ) ;
589593
590- EditorUserBuildSettings . SetBuildProfilePath ( ( s_Instance . m_ActiveProfile != null ) ?
591- AssetDatabase . GetAssetPath ( s_Instance . m_ActiveProfile ) : string . Empty ) ;
592594 s_Instance . cachedEditorScriptingDefines = BuildDefines . GetBuildProfileScriptDefines ( ) ;
593595
594596 BuildProfileModuleUtil . DeleteLastRunnableBuildKeyForDeletedProfiles ( ) ;
595597
596- s_Instance . OnActiveProfileChangedForSettingExtension ( null , s_Instance . m_ActiveProfile ) ;
598+ OnActiveProfileChangedForSettingExtension ( null , activeProfile ) ;
597599 }
598600
599601 [ RequiredByNativeCode , UsedImplicitly ]
@@ -620,6 +622,12 @@ static void SetActiveOrClassicProfileRawPlatformSetting(string settingName, stri
620622 }
621623 }
622624
625+ [ RequiredByNativeCode , UsedImplicitly ]
626+ static void EnsureInitialized ( )
627+ {
628+ GC . KeepAlive ( instance ) ;
629+ }
630+
623631 [ RequiredByNativeCode , UsedImplicitly ]
624632 static string GetActiveOrClassicProfileRawPlatformSetting ( string settingName , BuildTarget target , StandaloneBuildSubtarget subtarget )
625633 {
@@ -643,16 +651,17 @@ static string GetActiveOrClassicProfileRawPlatformSetting(string settingName, Bu
643651 [ RequiredByNativeCode ]
644652 static string GetActiveBuildProfilePath ( )
645653 {
646- if ( instance . activeProfile )
647- return AssetDatabase . GetAssetPath ( instance . activeProfile ) ;
654+ var activeProfile = BuildProfileContext . activeProfile ;
655+ if ( activeProfile )
656+ return AssetDatabase . GetAssetPath ( activeProfile ) ;
648657
649658 return string . Empty ;
650659 }
651660
652661 [ RequiredByNativeCode ]
653662 static bool HasActiveProfileWithPlayerSettings ( out int instanceID )
654663 {
655- var activeProfile = instance . activeProfile ;
664+ var activeProfile = BuildProfileContext . activeProfile ;
656665 if ( activeProfile ? . playerSettings != null )
657666 {
658667 instanceID = activeProfile . GetInstanceID ( ) ;
@@ -666,15 +675,15 @@ static bool HasActiveProfileWithPlayerSettings(out int instanceID)
666675 [ RequiredByNativeCode ]
667676 static void UpdateActiveProfilePlayerSettingsObjectFromYAML ( )
668677 {
669- instance . activeProfile ? . UpdatePlayerSettingsObjectFromYAML ( ) ;
678+ activeProfile ? . UpdatePlayerSettingsObjectFromYAML ( ) ;
670679 }
671680
672681 static bool ShouldReturnActiveProfile ( BuildTarget buildTarget , StandaloneBuildSubtarget subtarget , string sharedSetting = null )
673682 {
674683 if ( ! string . IsNullOrEmpty ( sharedSetting ) )
675684 return IsSharedSettingEnabledInActiveProfile ( sharedSetting ) ;
676685
677- var activeProfile = instance . activeProfile ;
686+ var activeProfile = BuildProfileContext . activeProfile ;
678687 if ( activeProfile == null || buildTarget == BuildTarget . NoTarget )
679688 return false ;
680689
@@ -684,7 +693,7 @@ static bool ShouldReturnActiveProfile(BuildTarget buildTarget, StandaloneBuildSu
684693
685694 static bool IsSharedSettingEnabledInActiveProfile ( string settingName )
686695 {
687- var activeProfile = instance . activeProfile ;
696+ var activeProfile = BuildProfileContext . activeProfile ;
688697 if ( activeProfile == null )
689698 return false ;
690699
0 commit comments