Skip to content

Commit

Permalink
Merge pull request #42 from lorant-csonka-planorama/fix-example-project
Browse files Browse the repository at this point in the history
Update example unity project's Editor scripts
  • Loading branch information
juicycleff authored Sep 9, 2019
2 parents 7b008c3 + f973f62 commit 25bf165
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 507 deletions.
44 changes: 28 additions & 16 deletions example/unity/DemoApp/Assets/Editor/Build.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
Expand All @@ -14,17 +13,31 @@ public class Build : MonoBehaviour

static readonly string apkPath = Path.Combine(ProjectPath, "Builds/" + Application.productName + ".apk");

[MenuItem("Flutter/Export Android %&a", false, 1)]
public static void DoBuildAndroid()
static readonly string androidExportPath = Path.GetFullPath(Path.Combine(ProjectPath, "../../android/UnityExport"));
static readonly string iosExportPath = Path.GetFullPath(Path.Combine(ProjectPath, "../../ios/UnityExport"));

[MenuItem("Flutter/Export Android (Unity 2019.3.*) %&n", false, 1)]
public static void DoBuildAndroidLibrary()
{
DoBuildAndroid(Path.Combine(apkPath, "unityLibrary"));

// Copy over resources from the launcher module that are used by the library
Copy(Path.Combine(apkPath + "/launcher/src/main/res"), Path.Combine(androidExportPath, "src/main/res"));
}

[MenuItem("Flutter/Export Android %&a", false, 2)]
public static void DoBuildAndroidLegacy()
{
string buildPath = Path.Combine(apkPath, Application.productName);
string exportPath = Path.GetFullPath(Path.Combine(ProjectPath, "../../android/UnityExport"));
DoBuildAndroid(Path.Combine(apkPath, Application.productName));
}

public static void DoBuildAndroid(String buildPath)
{
if (Directory.Exists(apkPath))
Directory.Delete(apkPath, true);

if (Directory.Exists(exportPath))
Directory.Delete(exportPath, true);
if (Directory.Exists(androidExportPath))
Directory.Delete(androidExportPath, true);

EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Gradle;

Expand All @@ -39,38 +52,37 @@ public static void DoBuildAndroid()
if (report.summary.result != BuildResult.Succeeded)
throw new Exception("Build failed");

Copy(buildPath, exportPath);
Copy(buildPath, androidExportPath);

// Modify build.gradle
var build_file = Path.Combine(exportPath, "build.gradle");
var build_file = Path.Combine(androidExportPath, "build.gradle");
var build_text = File.ReadAllText(build_file);
build_text = build_text.Replace("com.android.application", "com.android.library");
build_text = build_text.Replace("implementation fileTree(dir: 'libs', include: ['*.jar'])", "implementation project(':unity-classes')");
build_text = Regex.Replace(build_text, @"\n.*applicationId '.+'.*\n", "\n");
File.WriteAllText(build_file, build_text);

// Modify AndroidManifest.xml
var manifest_file = Path.Combine(exportPath, "src/main/AndroidManifest.xml");
var manifest_file = Path.Combine(androidExportPath, "src/main/AndroidManifest.xml");
var manifest_text = File.ReadAllText(manifest_file);
manifest_text = Regex.Replace(manifest_text, @"<application .*>", "<application>");
Regex regex = new Regex(@"<activity.*>(\s|\S)+?</activity>", RegexOptions.Multiline);
manifest_text = regex.Replace(manifest_text, "");
File.WriteAllText(manifest_file, manifest_text);
}

[MenuItem("Flutter/Export IOS %&i", false, 2)]
[MenuItem("Flutter/Export IOS (Unity 2019.3.*) %&i", false, 3)]
public static void DoBuildIOS()
{
string exportPath = Path.GetFullPath(Path.Combine(ProjectPath, "../../ios/UnityExport"));

if (Directory.Exists(exportPath))
Directory.Delete(exportPath, true);
if (Directory.Exists(iosExportPath))
Directory.Delete(iosExportPath, true);

EditorUserBuildSettings.iOSBuildConfigType = iOSBuildType.Release;

var options = BuildOptions.AcceptExternalModificationsToPlayer;
var report = BuildPipeline.BuildPlayer(
GetEnabledScenes(),
exportPath,
iosExportPath,
BuildTarget.iOS,
options
);
Expand Down
Loading

0 comments on commit 25bf165

Please sign in to comment.