101 lines
5.8 KiB
C#
101 lines
5.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using UnityEditor;
|
|
using UnityEditor.Build.Reporting;
|
|
using UnityEditor.SceneManagement;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
|
|
namespace MagnetismHeroes.Editor
|
|
{
|
|
public static class ProjectBootstrap
|
|
{
|
|
public const string MainScenePath = "Assets/MagnetismHeroes/Scenes/Main.unity";
|
|
|
|
public static void Prepare()
|
|
{
|
|
foreach (string path in new[] {
|
|
"Assets/MagnetismHeroes/UnityRuntime/Interaction", "Assets/MagnetismHeroes/UnityRuntime/Presentation",
|
|
"Assets/MagnetismHeroes/UnityRuntime/StageAssets", "Assets/MagnetismHeroes/UnityRuntime/Persistence",
|
|
"Assets/MagnetismHeroes/UnityRuntime/Bootstrap", "Assets/MagnetismHeroes/Scenes",
|
|
"Assets/MagnetismHeroes/Tests/EditMode/Core" }) Directory.CreateDirectory(path);
|
|
AssetDatabase.Refresh();
|
|
PlayerSettings.companyName = "MagnetismHeroes";
|
|
PlayerSettings.productName = "MagnetismHeroes";
|
|
PlayerSettings.SetApplicationIdentifier(UnityEditor.Build.NamedBuildTarget.Android, "com.magnetismheroes.game");
|
|
PlayerSettings.defaultInterfaceOrientation = UIOrientation.LandscapeLeft;
|
|
PlayerSettings.Android.minSdkVersion = AndroidSdkVersions.AndroidApiLevel26;
|
|
PlayerSettings.Android.targetSdkVersion = AndroidSdkVersions.AndroidApiLevelAuto;
|
|
PlayerSettings.Android.forceInternetPermission = false;
|
|
PlayerSettings.runInBackground = false;
|
|
PlayerSettings.colorSpace = ColorSpace.Linear;
|
|
var mobilePipeline = AssetDatabase.LoadAssetAtPath<RenderPipelineAsset>("Assets/Settings/Mobile_RPAsset.asset");
|
|
if (mobilePipeline == null) throw new InvalidOperationException("Mobile URP asset missing.");
|
|
GraphicsSettings.defaultRenderPipeline = mobilePipeline;
|
|
QualitySettings.renderPipeline = mobilePipeline;
|
|
if (!File.Exists(MainScenePath))
|
|
{
|
|
var scene = EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single);
|
|
EditorSceneManager.SaveScene(scene, MainScenePath);
|
|
}
|
|
EditorBuildSettings.scenes = new[] { new EditorBuildSettingsScene(MainScenePath, true) };
|
|
AssetDatabase.DeleteAsset("Assets/TutorialInfo");
|
|
AssetDatabase.DeleteAsset("Assets/Readme.asset");
|
|
AssetDatabase.DeleteAsset("Assets/Scenes");
|
|
AssetDatabase.SaveAssets();
|
|
Debug.Log("[Scaffold] Unity project prepared.");
|
|
}
|
|
|
|
public static void ConnectApp()
|
|
{
|
|
const string prefabPath = "Assets/MagnetismHeroes/UnityRuntime/Presentation/Prefabs/GameApp.prefab";
|
|
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
|
|
if (prefab == null) throw new InvalidOperationException("GameApp prefab missing.");
|
|
var scene = EditorSceneManager.OpenScene(MainScenePath, OpenSceneMode.Single);
|
|
var instances = scene.GetRootGameObjects().Where(root =>
|
|
PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(root) == prefabPath).ToArray();
|
|
if (instances.Length > 1) throw new InvalidOperationException("Main contains duplicate GameApp instances.");
|
|
foreach (var root in scene.GetRootGameObjects())
|
|
{
|
|
if ((root.name == "Main Camera" && root.GetComponent<Camera>() != null)
|
|
|| (root.name == "Directional Light" && root.GetComponent<Light>() != null))
|
|
UnityEngine.Object.DestroyImmediate(root);
|
|
}
|
|
if (instances.Length == 0) PrefabUtility.InstantiatePrefab(prefab, scene);
|
|
EditorSceneManager.SaveScene(scene, MainScenePath);
|
|
Debug.Log("[Integration] Main connected to GameApp.");
|
|
}
|
|
|
|
public static void ConfigureAndroid()
|
|
{
|
|
PlayerSettings.SetScriptingBackend(UnityEditor.Build.NamedBuildTarget.Android, ScriptingImplementation.IL2CPP);
|
|
PlayerSettings.Android.targetArchitectures = AndroidArchitecture.ARM64;
|
|
PlayerSettings.Android.buildApkPerCpuArchitecture = false;
|
|
PlayerSettings.defaultInterfaceOrientation = UIOrientation.LandscapeLeft;
|
|
PlayerSettings.Android.forceInternetPermission = false;
|
|
AssetDatabase.SaveAssets();
|
|
Debug.Log("[AndroidConfiguration] ARM64 IL2CPP, landscape, no forced Internet permission.");
|
|
}
|
|
|
|
public static void BuildAndroid()
|
|
{
|
|
string apkPath = Environment.GetEnvironmentVariable("MAGNETISM_APK_PATH");
|
|
if (string.IsNullOrEmpty(apkPath)) throw new InvalidOperationException("MAGNETISM_APK_PATH is required.");
|
|
EditorUserBuildSettings.buildAppBundle = false;
|
|
EditorUserBuildSettings.development = false;
|
|
BuildReport report = BuildPipeline.BuildPlayer(new BuildPlayerOptions {
|
|
scenes = new[] { MainScenePath }, locationPathName = apkPath,
|
|
target = BuildTarget.Android, options = BuildOptions.None
|
|
});
|
|
Debug.Log($"[AndroidBuild] {report.summary.result}: {report.summary.totalSize} bytes, {report.summary.totalErrors} errors.");
|
|
if (report.summary.result != BuildResult.Succeeded) throw new InvalidOperationException("Android build did not succeed.");
|
|
var packedPaths = report.packedAssets.SelectMany(asset => asset.contents)
|
|
.Select(asset => asset.sourceAssetPath).Distinct().OrderBy(path => path).ToArray();
|
|
File.WriteAllLines(apkPath + ".packed-assets.txt", packedPaths);
|
|
if (packedPaths.Any(path => path.Contains("PerformanceTestRun")))
|
|
throw new InvalidOperationException("Generated performance-test metadata entered the release APK.");
|
|
}
|
|
}
|
|
}
|