Thomas Stockx 2019-08-01 11:57:56 +02:00
parent a702f59e1a
commit 3fe912f5e0
2 changed files with 122 additions and 11 deletions

View File

@ -84,9 +84,9 @@ Now your project files should look like this.
### Add Unity Build Scripts and Export
Copy [`Build.cs`](https://github.com/f111fei/react-native-unity-demo/blob/master/unity/Cube/Assets/Scripts/Editor/Build.cs) and [`XCodePostBuild.cs`](https://github.com/f111fei/react-native-unity-demo/blob/master/unity/Cube/Assets/Scripts/Editor/XCodePostBuild.cs) to `unity/<Your Unity Project>/Assets/Scripts/Editor/`
Copy [`Build.cs`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/Editor/Build.cs) and [`XCodePostBuild.cs`](https://github.com/f111fei/react-native-unity-demo/blob/master/unity/Cube/Assets/Scripts/Editor/XCodePostBuild.cs) to `unity/<Your Unity Project>/Assets/Scripts/Editor/`
Open your unity project in Unity Editor. Now you can export unity project with `Flutter/Export Android` or `Flutter/Export IOS` menu.
Open your unity project in Unity Editor. Now you can export the Unity project with `Flutter/Export Android` (for Unity versions up to 2019.2), `Flutter/Export Android (Unity 2019.3.*)` (for Unity versions 2019.3 and up, which uses the new [Unity as a Library](https://blogs.unity3d.com/2019/06/17/add-features-powered-by-unity-to-native-mobile-apps/) export format), or `Flutter/Export IOS` menu.
<img src="https://github.com/snowballdigital/flutter-unity-view-widget/blob/master/Screenshot%202019-03-27%2008.13.08.png?raw=true" width="400" />
@ -99,20 +99,13 @@ IOS will export unity project to `ios/UnityExport`.
**Android Platform Only**
1. After exporting the unity game, open Android Studio and and add the `Unity Classes` Java `.jar` file as a module to the unity project. You just need to do this once if you are exporting from the same version of Unity everytime. The `.jar` file is located in the ```<Your Flutter Project>/android/UnityExport/lib``` folder
2. Next open `build.gradle` of `flutter_unity_widget` module and replace the dependencies with
```gradle
dependencies {
implementation project(':UnityExport') // The exported unity project
implementation project(':unity-classes') // the unity classes module you added from step 1
}
```
3. Next open `build.gradle` of `UnityExport` module and replace the dependencies with
2. If using Unity 2019.2 or older, open `build.gradle` of `UnityExport` module and replace the dependencies with
```gradle
dependencies {
implementation project(':unity-classes') // the unity classes module you added from step 1
}
```
4. Next open `build.gradle` of `UnityExport` module and remove these
3. If using Unity 2019.2 or older, open `build.gradle` of `UnityExport` module and remove these
```gradle
bundle {
language {

118
scripts/Editor/Build.cs Normal file
View File

@ -0,0 +1,118 @@
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");
static readonly string exportPath = Path.GetFullPath(Path.Combine(ProjectPath, "../../android/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(exportPath, "src/main/res"));
}
[MenuItem("Flutter/Export Android %&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(exportPath))
Directory.Delete(exportPath, 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, exportPath);
// Modify build.gradle
var build_file = Path.Combine(exportPath, "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_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, 3)]
public static void DoBuildIOS()
{
if (Directory.Exists(exportPath))
Directory.Delete(exportPath, true);
EditorUserBuildSettings.iOSBuildConfigType = iOSBuildType.Release;
var options = BuildOptions.AcceptExternalModificationsToPlayer;
var report = BuildPipeline.BuildPlayer(
GetEnabledScenes(),
exportPath,
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;
}
}