Merge branch 'snowballmaster'

# Conflicts:
#	README.md
This commit is contained in:
Kris Pypen 2019-08-02 10:27:47 +02:00
commit f4f3c597bb
4 changed files with 126 additions and 12 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 MiB

View File

@ -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)
<br />
@ -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/<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" />
@ -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 ```<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 {
@ -305,7 +300,7 @@ class _UnityDemoScreenState extends State<UnityDemoScreen>{
[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

BIN
preview_android.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 MiB

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

@ -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, @"<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 (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;
}
}