diff --git a/2019_03_28_19_23_37.gif b/2019_03_28_19_23_37.gif deleted file mode 100644 index daf95e7..0000000 Binary files a/2019_03_28_19_23_37.gif and /dev/null differ diff --git a/README.md b/README.md index 9b53763..0e90ebc 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,9 @@ import 'package:flutter_unity_widget/flutter_unity_widget.dart'; ## Preview -![gif](https://github.com/snowballdigital/flutter-unity-view-widget/blob/master/2019_03_28_19_23_37.gif?raw=true) +Android (30 fps gif, showcasing communication between Flutter and Unity): + +![gif](https://github.com/snowballdigital/flutter-unity-view-widget/blob/master/preview_android.gif?raw=true)
@@ -90,7 +92,7 @@ Be sure you have at least one scene added to your build. Copy [`Build.cs`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/Editor/Build.cs) and [`XCodePostBuild.cs`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/Editor/XCodePostBuild.cs) to `unity//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. @@ -103,20 +105,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 ```/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 { @@ -305,7 +300,7 @@ class _UnityDemoScreenState extends State{ [version-badge]: https://img.shields.io/pub/v/flutter_unity_widget.svg?style=flat-square -[package]: https://pub.dartlang.org/packages/flutter_unity_widget/versions/0.1.2 +[package]: https://pub.dartlang.org/packages/flutter_unity_widget/ [license-badge]: https://img.shields.io/github/license/snowballdigital/flutter-unity-view-widget.svg?style=flat-square [license]: https://github.com/snowballdigital/flutter-unity-view-widget/blob/master/LICENSE [prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square diff --git a/preview_android.gif b/preview_android.gif new file mode 100644 index 0000000..36db7e1 Binary files /dev/null and b/preview_android.gif differ diff --git a/scripts/Editor/Build.cs b/scripts/Editor/Build.cs new file mode 100644 index 0000000..281d5c8 --- /dev/null +++ b/scripts/Editor/Build.cs @@ -0,0 +1,119 @@ +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 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() + { + 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("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(androidExportPath, "src/main/AndroidManifest.xml"); + var manifest_text = File.ReadAllText(manifest_file); + manifest_text = Regex.Replace(manifest_text, @"", ""); + Regex regex = new Regex(@"(\s|\S)+?", RegexOptions.Multiline); + manifest_text = regex.Replace(manifest_text, ""); + File.WriteAllText(manifest_file, manifest_text); + } + + [MenuItem("Flutter/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; + } +} \ No newline at end of file