updated readme

This commit is contained in:
Rex Isaac Raphael 2020-03-20 12:36:14 +01:00
parent 243032e438
commit 6834239141
31 changed files with 24772 additions and 6 deletions

View File

@ -93,7 +93,7 @@ Be sure you have at least one scene added to your build.
### Add Unity Build Scripts and Export
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/`
Copy [`Build.cs`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/unity_2019-3-5*/Editor/Build.cs) and [`XCodePostBuild.cs`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/unity_2019-3-5*/Editor/XCodePostBuild.cs) to `unity/<Your Unity Project>/Assets/Scripts/Editor/`
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.
@ -180,13 +180,13 @@ If you want to use Unity for integrating Augmented Reality in your Flutter app,
### Add UnityMessageManager Support
Copy [`UnityMessageManager.cs`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/UnityMessageManager.cs) to your unity project.
Copy [`UnityMessageManager.cs`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/unity_2019-3-5*/UnityMessageManager.cs) to your unity project.
Copy this folder [`JsonDotNet`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/JsonDotNet) to your unity project.
Copy this folder [`JsonDotNet`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/unity_2019-3-5*/JsonDotNet) to your unity project.
Copy [`link.xml`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/link.xml) to your unity project.
Copy [`link.xml`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/unity_2019-3-5*/link.xml) to your unity project.
Copy this folder [`Plugins`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/Plugins) to your unity project.
(2019.3.5* only) Copy this folder [`Plugins`](https://github.com/snowballdigital/flutter-unity-view-widget/tree/master/scripts/unity_2019-3-5*/Plugins) to your unity project.
<br />

View File

@ -0,0 +1,175 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
public class MessageHandler
{
public int id;
public string seq;
public String name;
private JToken data;
public static MessageHandler Deserialize(string message)
{
JObject m = JObject.Parse(message);
MessageHandler handler = new MessageHandler(
m.GetValue("id").Value<int>(),
m.GetValue("seq").Value<string>(),
m.GetValue("name").Value<string>(),
m.GetValue("data")
);
return handler;
}
public T getData<T>()
{
return data.Value<T>();
}
public MessageHandler(int id, string seq, string name, JToken data)
{
this.id = id;
this.seq = seq;
this.name = name;
this.data = data;
}
public void send(object data)
{
JObject o = JObject.FromObject(new
{
id = id,
seq = "end",
name = name,
data = data
});
UnityMessageManager.Instance.SendMessageToFlutter(UnityMessageManager.MessagePrefix + o.ToString());
}
}
public class UnityMessage
{
public String name;
public JObject data;
public Action<object> callBack;
}
public class UnityMessageManager : MonoBehaviour
{
#if UNITY_IOS && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern void onUnityMessage(string message);
#endif
public const string MessagePrefix = "@UnityMessage@";
private static int ID = 0;
private static int generateId()
{
ID = ID + 1;
return ID;
}
public static UnityMessageManager Instance { get; private set; }
public delegate void MessageDelegate(string message);
public event MessageDelegate OnMessage;
public delegate void MessageHandlerDelegate(MessageHandler handler);
public event MessageHandlerDelegate OnFlutterMessage;
private Dictionary<int, UnityMessage> waitCallbackMessageMap = new Dictionary<int, UnityMessage>();
static UnityMessageManager()
{
GameObject go = new GameObject("UnityMessageManager");
DontDestroyOnLoad(go);
Instance = go.AddComponent<UnityMessageManager>();
}
void Awake()
{
}
public void SendMessageToFlutter(string message)
{
if (Application.platform == RuntimePlatform.Android)
{
using (AndroidJavaClass jc = new AndroidJavaClass("com.rexraphael.flutterunitywidget.UnityUtils"))
{
jc.CallStatic("onUnityMessage", message);
}
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
#if UNITY_IOS && !UNITY_EDITOR
onUnityMessage(message);
#endif
}
}
public void SendMessageToFlutter(UnityMessage message)
{
int id = generateId();
if (message.callBack != null)
{
waitCallbackMessageMap.Add(id, message);
}
JObject o = JObject.FromObject(new
{
id = id,
seq = message.callBack != null ? "start" : "",
name = message.name,
data = message.data
});
UnityMessageManager.Instance.SendMessageToFlutter(MessagePrefix + o.ToString());
}
void onMessage(string message)
{
if (OnMessage != null)
{
OnMessage(message);
}
}
void onFlutterMessage(string message)
{
if (message.StartsWith(MessagePrefix))
{
message = message.Replace(MessagePrefix, "");
}
else
{
return;
}
MessageHandler handler = MessageHandler.Deserialize(message);
if ("end".Equals(handler.seq))
{
// handle callback message
UnityMessage m;
if (waitCallbackMessageMap.TryGetValue(handler.id, out m))
{
waitCallbackMessageMap.Remove(handler.id);
if (m.callBack != null)
{
m.callBack(handler.getData<object>()); // todo
}
}
return;
}
if (OnFlutterMessage != null)
{
OnFlutterMessage(handler);
}
}
}

View File

@ -0,0 +1,122 @@
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("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 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;
}
}

View File

@ -0,0 +1,399 @@
/*
MIT License
Copyright (c) 2017 Jiulong Wang
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#if UNITY_IOS
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
/// <summary>
/// Adding this post build script to Unity project enables the flutter-unity-widget to access it
/// </summary>
public static class XcodePostBuild
{
/// <summary>
/// The identifier added to touched file to avoid double edits when building to existing directory without
/// replace existing content.
/// </summary>
private const string TouchedMarker = "https://github.com/snowballdigital/flutter-unity-view-widget";
[PostProcessBuild]
public static void OnPostBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS)
{
return;
}
PatchUnityNativeCode(pathToBuiltProject);
UpdateUnityProjectFiles(pathToBuiltProject);
}
/// <summary>
/// We need to add the Data folder to the UnityFramework framework
/// </summary>
private static void UpdateUnityProjectFiles(string pathToBuiltProject)
{
var pbx = new PBXProject();
var pbxPath = Path.Combine(pathToBuiltProject, "Unity-iPhone.xcodeproj/project.pbxproj");
pbx.ReadFromFile(pbxPath);
// Add UnityExport/Data
var targetGuid = pbx.TargetGuidByName("UnityFramework");
var fileGuid = pbx.AddFolderReference(Path.Combine(pathToBuiltProject, "Data"), "Data");
pbx.AddFileToBuild(targetGuid, fileGuid);
pbx.WriteToFile(pbxPath);
}
/// <summary>
/// Make necessary changes to Unity build output that enables it to be embedded into existing Xcode project.
/// </summary>
private static void PatchUnityNativeCode(string pathToBuiltProject)
{
EditUnityFrameworkH(Path.Combine(pathToBuiltProject, "UnityFramework/UnityFramework.h"));
EditUnityAppControllerH(Path.Combine(pathToBuiltProject, "Classes/UnityAppController.h"));
EditUnityAppControllerMM(Path.Combine(pathToBuiltProject, "Classes/UnityAppController.mm"));
EditUnityViewMM(Path.Combine(pathToBuiltProject, "Classes/UI/UnityView.mm"));
}
/// <summary>
/// Edit 'UnityFramework.h': add 'frameworkWarmup'
/// </summary>
private static void EditUnityFrameworkH(string path)
{
var inScope = false;
// Add frameworkWarmup method
EditCodeFile(path, line =>
{
inScope |= line.Contains("- (void)runUIApplicationMainWithArgc:");
if (inScope)
{
if (line.Trim() == "")
{
inScope = false;
return new string[]
{
"",
"// Added by " + TouchedMarker,
"- (void)frameworkWarmup:(int)argc argv:(char*[])argv;",
""
};
}
}
return new string[] { line };
});
}
/// <summary>
/// Edit 'UnityAppController.h': returns 'UnityAppController' from 'AppDelegate' class.
/// </summary>
private static void EditUnityAppControllerH(string path)
{
var inScope = false;
var markerDetected = false;
// Modify inline GetAppController
EditCodeFile(path, line =>
{
inScope |= line.Contains("include \"RenderPluginDelegate.h\"");
if (inScope && !markerDetected)
{
if (line.Trim() == "")
{
inScope = false;
markerDetected = true;
return new string[]
{
"",
"// Added by " + TouchedMarker,
"@protocol UnityEventListener <NSObject>",
"- (void)onMessage:(NSString *)message;",
"@end",
"",
};
}
return new string[] { line };
}
return new string[] { line };
});
inScope = false;
markerDetected = false;
// Add static GetAppController
EditCodeFile(path, line =>
{
inScope |= line.Contains("- (void)startUnity:");
if (inScope)
{
if (line.Trim() == "")
{
inScope = false;
return new string[]
{
"",
"// Added by " + TouchedMarker,
"+ (UnityAppController*)GetAppController;",
"+ (void)addUnityEventListenerInternal:(id<UnityEventListener>)listener;",
"+ (void)removeUnityEventListenerInternal:(id<UnityEventListener>)listener;",
""
};
}
}
return new string[] { line };
});
inScope = false;
markerDetected = false;
// Modify inline GetAppController
EditCodeFile(path, line =>
{
inScope |= line.Contains("extern UnityAppController* GetAppController");
if (inScope && !markerDetected)
{
if (line.Trim() == "")
{
inScope = false;
markerDetected = true;
return new string[]
{
"// }",
"",
"// Added by " + TouchedMarker,
"static inline UnityAppController* GetAppController()",
"{",
" return [UnityAppController GetAppController];",
"}",
"",
"// Added by " + TouchedMarker,
"static inline void addUnityEventListenerInternal(id<UnityEventListener> listener)",
"{",
" [UnityAppController addUnityEventListenerInternal: listener];",
"}",
"",
"// Added by " + TouchedMarker,
"static inline void removeUnityEventListenerInternal(id<UnityEventListener> listener)",
"{",
" [UnityAppController removeUnityEventListenerInternal:listener];",
"}"
};
}
return new string[] { "// " + line };
}
return new string[] { line };
});
}
/// <summary>
/// Edit 'UnityAppController.mm': triggers 'UnityReady' notification after Unity is actually started.
/// </summary>
private static void EditUnityAppControllerMM(string path)
{
var inScope = false;
var markerDetected = false;
EditCodeFile(path, line =>
{
if (line.Trim() == "@end")
{
return new string[]
{
"",
"// Added by " + TouchedMarker,
"static UnityAppController *unityAppController = nil;",
"",
@"+ (UnityAppController*)GetAppController",
"{",
" static dispatch_once_t onceToken;",
" dispatch_once(&onceToken, ^{",
" unityAppController = [[self alloc] init];",
" });",
" return unityAppController;",
"}",
"",
"// Added by " + TouchedMarker,
"static NSHashTable* mUnityEventListeners = [NSHashTable weakObjectsHashTable];",
"+ (void)addUnityEventListener2:(id<UnityEventListener>)listener",
"{",
" [mUnityEventListeners addObject: listener];",
"}",
"",
"// Added by " + TouchedMarker,
"+(void)removeUnityEventListener2:(id<UnityEventListener>)listener",
"{",
" [mUnityEventListeners removeObject: listener];",
"}",
line,
"// Added by " + TouchedMarker,
"extern \"C\" void onUnityMessage(const char* message)",
"{",
" for (id<UnityEventListener> listener in mUnityEventListeners) {",
" [listener onMessage:[NSString stringWithUTF8String:message]];",
" }",
"}",
};
}
inScope |= line.Contains("- (void)startUnity:");
markerDetected |= inScope && line.Contains(TouchedMarker);
if (inScope && line.Trim() == "}")
{
inScope = false;
if (markerDetected)
{
return new string[] { line };
}
else
{
return new string[]
{
" // Modified by " + TouchedMarker,
@" [[NSNotificationCenter defaultCenter] postNotificationName: @""UnityReady"" object:self];",
"}",
};
}
}
return new string[] { line };
});
inScope = false;
markerDetected = false;
// Modify inline GetAppController
EditCodeFile(path, line =>
{
inScope |= line.Contains("UnityAppController* GetAppController()");
if (inScope && !markerDetected)
{
if (line.Trim() == "}")
{
inScope = false;
markerDetected = true;
return new string[]
{
"",
};
}
return new string[] { "// " + line };
}
return new string[] { line };
});
}
/// <summary>
/// Edit 'UnityView.mm': fix the width and height needed for the Metal renderer
/// </summary>
private static void EditUnityViewMM(string path)
{
var inScope = false;
// Add frameworkWarmup method
EditCodeFile(path, line =>
{
inScope |= line.Contains("UnityGetRenderingResolution(&requestedW, &requestedH)");
if (inScope)
{
if (line.Trim() == "")
{
inScope = false;
return new string[]
{
"",
"// Added by " + TouchedMarker,
" if (requestedW == 0) {",
" requestedW = _surfaceSize.width;",
" }",
" if (requestedH == 0) {",
" requestedH = _surfaceSize.height;",
" }",
""
};
}
}
return new string[] { line };
});
}
private static void EditCodeFile(string path, Func<string, IEnumerable<string>> lineHandler)
{
var bakPath = path + ".bak";
if (File.Exists(bakPath))
{
File.Delete(bakPath);
}
File.Move(path, bakPath);
using (var reader = File.OpenText(bakPath))
using (var stream = File.Create(path))
using (var writer = new StreamWriter(stream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
var outputs = lineHandler(line);
foreach (var o in outputs)
{
writer.WriteLine(o);
}
}
}
}
}
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<linker>
<assembly fullname="System">
<type fullname="System.ComponentModel.TypeConverter" preserve="all" />
<!-- <namespace fullname="System.ComponentModel" preserve="all" /> -->
</assembly>
</linker>

View File

@ -1,4 +1,3 @@
package scripts.Plugins.Android;
import android.os.Bundle;
import android.widget.FrameLayout;

View File

@ -0,0 +1,32 @@
<linker>
<!-- The following assemblies contain namespaces that should be fully preserved
even when assembly stripping is used. Not excluding the assemblies below from
stripping can result in crashes or various exceptions. -->
<assembly fullname="System">
<type fullname="System.ComponentModel.TypeConverter" preserve="all"/>
<type fullname="System.ComponentModel.ArrayConverter" preserve="all"/>
<type fullname="System.ComponentModel.BaseNumberConverter" preserve="all"/>
<type fullname="System.ComponentModel.BooleanConverter" preserve="all"/>
<type fullname="System.ComponentModel.ByteConverter" preserve="all"/>
<type fullname="System.ComponentModel.CharConverter" preserve="all"/>
<type fullname="System.ComponentModel.CollectionConverter" preserve="all"/>
<type fullname="System.ComponentModel.ComponentConverter" preserve="all"/>
<type fullname="System.ComponentModel.CultureInfoConverter" preserve="all"/>
<type fullname="System.ComponentModel.DateTimeConverter" preserve="all"/>
<type fullname="System.ComponentModel.DecimalConverter" preserve="all"/>
<type fullname="System.ComponentModel.DoubleConverter" preserve="all"/>
<type fullname="System.ComponentModel.EnumConverter" preserve="all"/>
<type fullname="System.ComponentModel.ExpandableObjectConverter" preserve="all"/>
<type fullname="System.ComponentModel.Int16Converter" preserve="all"/>
<type fullname="System.ComponentModel.Int32Converter" preserve="all"/>
<type fullname="System.ComponentModel.Int64Converter" preserve="all"/>
<type fullname="System.ComponentModel.NullableConverter" preserve="all"/>
<type fullname="System.ComponentModel.SByteConverter" preserve="all"/>
<type fullname="System.ComponentModel.SingleConverter" preserve="all"/>
<type fullname="System.ComponentModel.StringConverter" preserve="all"/>
<type fullname="System.ComponentModel.TimeSpanConverter" preserve="all"/>
<type fullname="System.ComponentModel.UInt16Converter" preserve="all"/>
<type fullname="System.ComponentModel.UInt32Converter" preserve="all"/>
<type fullname="System.ComponentModel.UInt64Converter" preserve="all"/>
</assembly>
</linker>