@@ -1304,6 +1304,101 @@ public static void OpenAppdataSpecialFolder(string subfolder)
13041304 }
13051305 }
13061306
1307+ // NOTE android only at the moment
1308+ public static void BuildProject ( Project proj , Platform platform )
1309+ {
1310+ Console . WriteLine ( "Building " + proj . Title + " for " + platform ) ;
1311+ if ( string . IsNullOrEmpty ( proj . Path ) ) return ;
1312+
1313+ // create builder script template (with template string, that can be replaced with project related paths or names?)
1314+ // copy editor build script to Assets/Editor/ folder (if already exists then what? Use UnityLauncherBuildSomething.cs name, so can overwrite..)
1315+ var editorScriptFolder = Path . Combine ( proj . Path , "Assets" , "Editor" ) ;
1316+ if ( Directory . Exists ( editorScriptFolder ) == false ) Directory . CreateDirectory ( editorScriptFolder ) ;
1317+ // TODO check if creation failed
1318+
1319+ // create output file for editor script
1320+ var editorScriptFile = Path . Combine ( editorScriptFolder , "UnityLauncherProBuilder.cs" ) ;
1321+
1322+ // check build folder and create if missing
1323+ var outputFolder = Path . Combine ( proj . Path , "Builds/" + platform + "/" ) ;
1324+ outputFolder = outputFolder . Replace ( '\\ ' , '/' ) ; // fix backslashes
1325+ Console . WriteLine ( "outputFolder= " + outputFolder ) ;
1326+ if ( Directory . Exists ( outputFolder ) == false ) Directory . CreateDirectory ( outputFolder ) ;
1327+ // TODO check if creation failed
1328+
1329+ // cleanup filename from project name
1330+ var invalidChars = Path . GetInvalidFileNameChars ( ) ;
1331+ var outputFile = String . Join ( "_" , proj . Title . Split ( invalidChars , StringSplitOptions . RemoveEmptyEntries ) ) . TrimEnd ( '.' ) ;
1332+ // replace spaces also, for old time(r)s
1333+ outputFile = outputFile . Replace ( ' ' , '_' ) ;
1334+ outputFile = Path . Combine ( outputFolder , outputFile + ".apk" ) ;
1335+ Console . WriteLine ( "outputFile= " + outputFile ) ;
1336+
1337+ // TODO move to txt resource? and later load from local custom file if exists, and later open window or add settings for build options
1338+ // TODO different unity versions? wont work in older unitys right now
1339+ var builderScript = @"using System.Linq;
1340+ using UnityEditor;
1341+ using UnityEngine;
1342+ public static class UnityLauncherProTools
1343+ {
1344+ public static void BuildAndroid()
1345+ {
1346+ EditorUserBuildSettings.buildAppBundle = false;
1347+ EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Gradle;
1348+ PlayerSettings.SetScriptingBackend(BuildTargetGroup.Android, ScriptingImplementation.IL2CPP);
1349+ PlayerSettings.Android.targetArchitectures = AndroidArchitecture.ARM64;
1350+ var settings = new BuildPlayerOptions();
1351+ settings.scenes = GetScenes();
1352+ settings.locationPathName = ""###OUTPUTFILE###"";
1353+ settings.target = BuildTarget.Android;
1354+ settings.options = BuildOptions.None;
1355+ var report = BuildPipeline.BuildPlayer(settings);
1356+ }
1357+ public static void BuildiOS() // Note need to match platform name
1358+ {
1359+ PlayerSettings.iOS.targetDevice = iOSTargetDevice.iPhoneAndiPad;
1360+ var settings = new BuildPlayerOptions();
1361+ settings.scenes = GetScenes();
1362+ settings.locationPathName = ""###OUTPUTFOLDER###"";
1363+ settings.target = BuildTarget.iOS;
1364+ settings.options = BuildOptions.None;
1365+ var report = BuildPipeline.BuildPlayer(settings);
1366+ }
1367+ static string[] GetScenes()
1368+ {
1369+ return EditorBuildSettings.scenes.Where(scene => scene.enabled).Select(scene => scene.path).ToArray();
1370+ }
1371+ }" ;
1372+
1373+ // fill in project specific data
1374+ builderScript = builderScript . Replace ( "###OUTPUTFILE###" , outputFile ) ; // android
1375+ builderScript = builderScript . Replace ( "###OUTPUTFOLDER###" , outputFolder ) ; // ios
1376+ Console . WriteLine ( "builderScript=" + builderScript ) ;
1377+
1378+ File . WriteAllText ( editorScriptFile , builderScript ) ;
1379+ // TODO check if write failed
1380+
1381+ // get selected project unity exe path
1382+ var unityExePath = Tools . GetUnityExePath ( proj . Version ) ;
1383+ if ( unityExePath == null ) return ;
1384+
1385+ // create commandline string for building and launch it
1386+ //var buildcmd = $"\"{unityExePath}\" -quit -batchmode -nographics -projectPath \"{proj.Path}\" -executeMethod \"Builder.BuildAndroid\" -buildTarget android -logFile -";
1387+ var buildParams = $ " -quit -batchmode -nographics -projectPath \" { proj . Path } \" -executeMethod \" UnityLauncherProTools.Build{ platform } \" -buildTarget { platform } -logFile -";
1388+ Console . WriteLine ( "buildcmd= " + buildParams ) ;
1389+
1390+ // launch build
1391+ var proc = Tools . LaunchExe ( unityExePath , buildParams ) ;
1392+
1393+ // wait for process exit then open output folder
1394+ proc . Exited += ( o , i ) =>
1395+ {
1396+ Console . WriteLine ( "Build process exited: " + outputFolder ) ;
1397+ Tools . ExploreFolder ( outputFolder ) ;
1398+ } ;
1399+
1400+ }
1401+
13071402 } // class
13081403} // namespace
13091404
0 commit comments