Skip to content

Commit

Permalink
Merge pull request gree#346 from gree/fix/unified_post_proces_script
Browse files Browse the repository at this point in the history
Fix/unified post process script
  • Loading branch information
KojiNakamaru authored Aug 23, 2018
2 parents d9ac7d8 + d58e293 commit e8db8b1
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 138 deletions.
10 changes: 1 addition & 9 deletions build/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ DSTDIR[0]="Packager/Assets/Plugins"
SRCS[0]=%W|
WebViewObject.cs
iOS/WebView.mm
iOS/Editor/UnityWebViewPostprocessBuild.cs
Android/Editor/UnityWebViewPostprocessBuild.cs
Android/Editor/UnityWebViewPostprocessBuild2018_1.cs
Editor/UnityWebViewPostprocessBuild.cs
|

# NOTE: WebPlayerTemplates is not inclued for now because the sample app becomes complicated.
Expand Down Expand Up @@ -70,12 +68,6 @@ task :build do
sh "./install.sh"
end
end
# the following is for Unity 2018 which seems not to process Editor/*.cs under Plugins/{Android,iOS}.
FileUtils.mkdir_p("#{DSTDIR[0]}/Editor")
FileUtils.mkdir_p("#{DSTDIR[0]}/Editor")
sh "cat #{DSTDIR[0]}/Android/Editor/*.cs #{DSTDIR[0]}/iOS/Editor/*.cs > #{DSTDIR[0]}/Editor/UnityWebViewPostprocessBuild.cs"
FileUtils.rm_rf("#{DSTDIR[0]}/Android/Editor")
FileUtils.rm_rf("#{DSTDIR[0]}/iOS/Editor")
end

desc "pack"
Expand Down
102 changes: 0 additions & 102 deletions plugins/Android/Editor/UnityWebViewPostprocessBuild2018_1.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
#if UNITY_ANDROID
#if !UNITY_2018_1_OR_NEWER
using System.Collections;
using System.IO;
using System.Text;
using System.Xml;
using UnityEditor.Android;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEditor;
using UnityEngine;

public class UnityWebViewPostprocessBuild {
#if UNITY_2018_1_OR_NEWER
public class UnityWebViewPostprocessBuild : IPostGenerateGradleAndroidProject
#else
public class UnityWebViewPostprocessBuild
#endif
{
//// for android/unity 2018.1 or newer
//// cf. https://forum.unity.com/threads/android-hardwareaccelerated-is-forced-false-in-all-activities.532786/
//// cf. https://github.com/Over17/UnityAndroidManifestCallback

public void OnPostGenerateGradleAndroidProject(string basePath) {
Debug.Log("adjusted AndroidManifest.xml.");
var androidManifest = new AndroidManifest(GetManifestPath(basePath));
androidManifest.SetHardwareAccelerated(true);
androidManifest.Save();
}

public int callbackOrder {
get {
return 1;
}
}

private string GetManifestPath(string basePath) {
var pathBuilder = new StringBuilder(basePath);
pathBuilder.Append(Path.DirectorySeparatorChar).Append("src");
pathBuilder.Append(Path.DirectorySeparatorChar).Append("main");
pathBuilder.Append(Path.DirectorySeparatorChar).Append("AndroidManifest.xml");
return pathBuilder.ToString();
}

//// for others

#if UNITYWEBVIEW_ANDROID_USE_ACTIVITY_NAME
// please modify ACTIVITY_NAME if you set UNITYWEBVIEW_ANDROID_USE_ACTIVITY_NAME and utilize any custom activty.
private const string ACTIVITY_NAME = "com.unity3d.player.UnityPlayerActivity";
#endif

[PostProcessBuild(100)]
public static void OnPostprocessBuild(BuildTarget buildTarget, string path) {
#if !UNITY_2018_1_OR_NEWER
if (buildTarget == BuildTarget.Android) {
string manifest = Path.Combine(Application.dataPath, "Plugins/Android/AndroidManifest.xml");
if (!File.Exists(manifest)) {
Expand Down Expand Up @@ -44,6 +78,15 @@ public static void OnPostprocessBuild(BuildTarget buildTarget, string path) {
}
#endif
}
#endif
if (buildTarget == BuildTarget.iOS) {
string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
string target = proj.TargetGuidByName("Unity-iPhone");
proj.AddFrameworkToProject(target, "WebKit.framework", false);
File.WriteAllText(projPath, proj.WriteToString());
}
}

private static XmlElement SearchActivity(XmlDocument doc) {
Expand Down Expand Up @@ -83,5 +126,59 @@ private static XmlElement SearchActivity(XmlDocument doc) {
return null;
}
}
#endif
#endif

internal class AndroidXmlDocument : XmlDocument {
private string m_Path;
protected XmlNamespaceManager nsMgr;
public readonly string AndroidXmlNamespace = "http://schemas.android.com/apk/res/android";

public AndroidXmlDocument(string path) {
m_Path = path;
using (var reader = new XmlTextReader(m_Path)) {
reader.Read();
Load(reader);
}
nsMgr = new XmlNamespaceManager(NameTable);
nsMgr.AddNamespace("android", AndroidXmlNamespace);
}

public string Save() {
return SaveAs(m_Path);
}

public string SaveAs(string path) {
using (var writer = new XmlTextWriter(path, new UTF8Encoding(false))) {
writer.Formatting = Formatting.Indented;
Save(writer);
}
return path;
}
}

internal class AndroidManifest : AndroidXmlDocument {
private readonly XmlElement ApplicationElement;

public AndroidManifest(string path) : base(path) {
ApplicationElement = SelectSingleNode("/manifest/application") as XmlElement;
}

private XmlAttribute CreateAndroidAttribute(string key, string value) {
XmlAttribute attr = CreateAttribute("android", key, AndroidXmlNamespace);
attr.Value = value;
return attr;
}

internal XmlNode GetActivityWithLaunchIntent() {
return
SelectSingleNode(
"/manifest/application/activity[intent-filter/action/@android:name='android.intent.action.MAIN' and "
+ "intent-filter/category/@android:name='android.intent.category.LAUNCHER']",
nsMgr);
}

internal void SetHardwareAccelerated(bool enabled) {
var activity = GetActivityWithLaunchIntent() as XmlElement;
activity.SetAttribute("android:hardwareAccelerated", (enabled) ? "true" : "false");
}
}

22 changes: 0 additions & 22 deletions plugins/iOS/Editor/UnityWebViewPostprocessBuild.cs

This file was deleted.

0 comments on commit e8db8b1

Please sign in to comment.