forked from rakirox/react-native-unity-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build.cs
121 lines (97 loc) · 4.63 KB
/
Build.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using Application = UnityEngine.Application;
using BuildResult = UnityEditor.Build.Reporting.BuildResult;
public class Build : MonoBehaviour {
static readonly string ProjectPath = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
static readonly string apkPath = Path.Combine(ProjectPath, "Builds/" + Application.productName + ".apk");
private static readonly string androidExportPath =
Path.GetFullPath(Path.Combine(ProjectPath, "../../android/UnityExport"));
private static readonly string iosExportPath =
Path.GetFullPath(Path.Combine(ProjectPath, "../../ios/UnityExport"));
[MenuItem("ReactNative/Export Android (Unity 2019.3.*) %&n", false, 1)]
public static void DoBuildAndroidLibrary() {
DoBuildAndroid(Path.Combine(apkPath, "unityLibrary"));
Copy(Path.Combine(apkPath, "launcher/src/main/res"), Path.Combine(androidExportPath, "src/main/res"));
}
[MenuItem("ReactNative/Export Android legacy %&a", false, 2)]
public static void DoBuildAndroidLegacy() {
DoBuildAndroid(Path.Combine(apkPath, Application.productName));
}
public static void DoBuildAndroid(String buildPath) {
if (Directory.Exists(apkPath)) {
Directory.Delete(apkPath, true);
}
if (Directory.Exists(androidExportPath)) {
Directory.Delete(androidExportPath, true);
}
EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Gradle;
var options = BuildOptions.AcceptExternalModificationsToPlayer;
var report = BuildPipeline.BuildPlayer(
GetEnabledScenes(),
apkPath,
BuildTarget.Android,
options
);
if (report.summary.result != BuildResult.Succeeded) {
throw new Exception("Build failed");
}
Copy(buildPath, androidExportPath);
// Modify 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("bundle {", "splits {");
build_text = build_text.Replace("enableSplit = false", "enable false");
build_text = build_text.Replace("enableSplit = true", "enable true");
build_text = build_text.Replace("implementation fileTree(dir: 'libs', include: ['*.jar'])", "implementation ':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(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("ReactNative/Export IOS (Unity 2019.3.*) %&i", false, 3)]
public static void DoBuildIOS() {
if (Directory.Exists(iosExportPath)) {
Directory.Delete(iosExportPath, true);
}
EditorUserBuildSettings.iOSBuildConfigType = iOSBuildType.Release;
var options = BuildOptions.AcceptExternalModificationsToPlayer;
var report = BuildPipeline.BuildPlayer(
GetEnabledScenes(),
iosExportPath,
BuildTarget.iOS,
options
);
if (report.summary.result != BuildResult.Succeeded) {
throw new Exception("Build failed");
}
}
static void Copy(string source, string destinationPath) {
if (Directory.Exists(destinationPath))
Directory.Delete(destinationPath, true);
Directory.CreateDirectory(destinationPath);
foreach (string dirPath in Directory.GetDirectories(source, "*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(source, destinationPath));
foreach (string newPath in Directory.GetFiles(source, "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(source, destinationPath), true);
}
static string[] GetEnabledScenes() {
var scenes = EditorBuildSettings.scenes
.Where(s => s.enabled)
.Select(s => s.path)
.ToArray();
return scenes;
}
}