77using AltTester . AltTesterUnitySDK . Editor ;
88using System ;
99using System . Linq ;
10+ using System . Collections . Generic ;
1011using AltTester . AltTesterUnitySDK . Commands ;
1112using AltTester . AltTesterSDK . Driver ;
1213using TMPro ;
@@ -60,6 +61,30 @@ private static void BuildPlayer(string defaultBuildPath, bool setupForAltTester
6061
6162 Debug . Log ( $ "Using Build Profile: { buildProfile . name } ") ;
6263
64+ // Unity 6 Build Profiles can override scripting defines. If we only mutate
65+ // PlayerSettings, AltTester runtime scripts may be excluded from the build
66+ // (leading to missing-script errors and no AltTester server listening).
67+ // For AltTester builds, temporarily mirror the current Standalone define
68+ // symbols onto the BuildProfile's scripting defines.
69+ string [ ] originalProfileDefines = Array . Empty < string > ( ) ;
70+ if ( setupForAltTester )
71+ {
72+ originalProfileDefines = GetBuildProfileDefines ( buildProfile ) ;
73+
74+ // Capture the current Standalone define symbols after SetupAltTester()
75+ // has added AltTester + IMMUTABLE_E2E_TESTING.
76+ string defineSymbols = PlayerSettings . GetScriptingDefineSymbolsForGroup ( BuildTargetGroup . Standalone ) ;
77+ var desired = defineSymbols
78+ . Split ( ';' )
79+ . Select ( s => s . Trim ( ) )
80+ . Where ( s => ! string . IsNullOrEmpty ( s ) )
81+ . Distinct ( )
82+ . ToArray ( ) ;
83+
84+ SetBuildProfileDefines ( buildProfile , MergeDefines ( originalProfileDefines , desired ) ) ;
85+ Debug . Log ( $ "Applied { desired . Length } define symbols to BuildProfile for AltTester build.") ;
86+ }
87+
6388 BuildPlayerWithProfileOptions options = new BuildPlayerWithProfileOptions
6489 {
6590 buildProfile = buildProfile ,
@@ -82,6 +107,11 @@ private static void BuildPlayer(string defaultBuildPath, bool setupForAltTester
82107 EditorApplication . Exit ( 1 ) ;
83108 }
84109
110+ if ( setupForAltTester )
111+ {
112+ // Restore original profile defines to avoid dirtying the repo locally.
113+ SetBuildProfileDefines ( buildProfile , originalProfileDefines ) ;
114+ }
85115
86116 if ( setupForAltTester )
87117 {
@@ -171,6 +201,59 @@ private static void SetupAltTester(string[] scenes)
171201 AltBuilder . InsertAltInScene ( scenes [ 0 ] , instrumentationSettings ) ;
172202 }
173203
204+ private static string [ ] GetBuildProfileDefines ( BuildProfile buildProfile )
205+ {
206+ var so = new SerializedObject ( buildProfile ) ;
207+ var prop = so . FindProperty ( "m_ScriptingDefines" ) ;
208+ if ( prop == null || ! prop . isArray ) return Array . Empty < string > ( ) ;
209+ var list = new List < string > ( prop . arraySize ) ;
210+ for ( int i = 0 ; i < prop . arraySize ; i ++ )
211+ {
212+ var el = prop . GetArrayElementAtIndex ( i ) ;
213+ if ( el != null && ! string . IsNullOrEmpty ( el . stringValue ) )
214+ {
215+ list . Add ( el . stringValue ) ;
216+ }
217+ }
218+ return list . ToArray ( ) ;
219+ }
220+
221+ private static string [ ] MergeDefines ( IEnumerable < string > a , IEnumerable < string > b )
222+ {
223+ var set = new HashSet < string > ( StringComparer . Ordinal ) ;
224+ foreach ( var s in a ?? Array . Empty < string > ( ) ) if ( ! string . IsNullOrEmpty ( s ) ) set . Add ( s ) ;
225+ foreach ( var s in b ?? Array . Empty < string > ( ) ) if ( ! string . IsNullOrEmpty ( s ) ) set . Add ( s ) ;
226+ return set . ToArray ( ) ;
227+ }
228+
229+ private static void SetBuildProfileDefines ( BuildProfile buildProfile , IEnumerable < string > defines )
230+ {
231+ var so = new SerializedObject ( buildProfile ) ;
232+ var prop = so . FindProperty ( "m_ScriptingDefines" ) ;
233+ if ( prop == null || ! prop . isArray )
234+ {
235+ Debug . LogWarning ( "BuildProfile does not expose m_ScriptingDefines; cannot apply defines." ) ;
236+ return ;
237+ }
238+
239+ prop . ClearArray ( ) ;
240+ int idx = 0 ;
241+ if ( defines != null )
242+ {
243+ foreach ( var d in defines )
244+ {
245+ if ( string . IsNullOrEmpty ( d ) ) continue ;
246+ prop . InsertArrayElementAtIndex ( idx ) ;
247+ prop . GetArrayElementAtIndex ( idx ) . stringValue = d ;
248+ idx ++ ;
249+ }
250+ }
251+
252+ so . ApplyModifiedPropertiesWithoutUndo ( ) ;
253+ EditorUtility . SetDirty ( buildProfile ) ;
254+ AssetDatabase . SaveAssets ( ) ;
255+ }
256+
174257 public static void RemoveAltFromScene ( string scene )
175258 {
176259 Debug . Log ( "Removing AltTesterPrefab from the [" + scene + "] scene." ) ;
0 commit comments