Exemplo n.º 1
0
    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))
            {
                string manifest0 = Path.Combine(Application.dataPath, "../Temp/StagingArea/AndroidManifest-main.xml");
                if (!File.Exists(manifest0))
                {
                    Debug.LogError("unitywebview: cannot find both Assets/Plugins/Android/AndroidManifest.xml and Temp/StagingArea/AndroidManifest-main.xml. please build the app to generate Assets/Plugins/Android/AndroidManifest.xml and then rebuild it again.");
                    return;
                }
                else
                {
                    File.Copy(manifest0, manifest);
                }
            }
            var changed         = false;
            var androidManifest = new AndroidManifest(manifest);
            changed = (androidManifest.SetHardwareAccelerated(true) || changed);
#if UNITYWEBVIEW_ANDROID_USES_CLEARTEXT_TRAFFIC
            changed = (androidManifest.SetUsesCleartextTraffic(true) || changed);
#endif
#if UNITYWEBVIEW_ANDROID_ENABLE_CAMERA
            changed = (androidManifest.AddCamera() || changed);
#endif
#if UNITYWEBVIEW_ANDROID_ENABLE_MICROPHONE
            changed = (androidManifest.AddMicrophone() || changed);
#endif
#if UNITY_5_6_0 || UNITY_5_6_1
            changed = (androidManifest.SetActivityName("net.gree.unitywebview.CUnityPlayerActivity") || changed);
#endif
            if (changed)
            {
                androidManifest.Save();
                Debug.LogError("unitywebview: adjusted AndroidManifest.xml. Please rebuild the app.");
            }
        }
#endif

#if UNITY_IOS
        if (buildTarget == BuildTarget.iOS)
        {
            string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
            UnityEditor.iOS.Xcode.PBXProject proj = new using UnityEditor.iOS.Xcode.PBXProject();
            proj.ReadFromString(File.ReadAllText(projPath));
#if UNITY_2019_3_OR_NEWER
            proj.AddFrameworkToProject(proj.GetUnityFrameworkTargetGuid(), "WebKit.framework", false);
#else
            proj.AddFrameworkToProject(proj.TargetGuidByName("Unity-iPhone"), "WebKit.framework", false);
#endif
            File.WriteAllText(projPath, proj.WriteToString());
        }
#endif
    }
Exemplo n.º 2
0
    //// 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

#if UNITY_2018_1_OR_NEWER
    public void OnPostGenerateGradleAndroidProject(string basePath)
    {
        var changed         = false;
        var androidManifest = new AndroidManifest(GetManifestPath(basePath));

        changed = (androidManifest.SetHardwareAccelerated(true) || changed);
#if UNITYWEBVIEW_ANDROID_ENABLE_CAMERA
        changed = (androidManifest.AddCamera() || changed);
#endif
        if (changed)
        {
            androidManifest.Save();
            Debug.Log("unitywebview: adjusted AndroidManifest.xml.");
        }
    }
    //// 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

#if UNITY_2018_1_OR_NEWER
    public void OnPostGenerateGradleAndroidProject(string basePath)
    {
        var changed         = false;
        var androidManifest = new AndroidManifest(GetManifestPath(basePath));

        changed = (androidManifest.SetHardwareAccelerated(true) || changed);
        changed = (androidManifest.SetUsesCleartextTraffic(true) || changed);
        changed = (androidManifest.AddCamera() || changed);
        changed = (androidManifest.AddMicrophone() || changed);
        changed = (androidManifest.AddGallery() || changed);

        if (changed)
        {
            androidManifest.Save();
            Debug.Log("unitywebview: adjusted AndroidManifest.xml.");
        }
    }
Exemplo n.º 4
0
    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))
            {
                string manifest0 = Path.Combine(Application.dataPath, "../Temp/StagingArea/AndroidManifest-main.xml");
                if (!File.Exists(manifest0))
                {
                    Debug.LogError("unitywebview: cannot find both Assets/Plugins/Android/AndroidManifest.xml and Temp/StagingArea/AndroidManifest-main.xml. please build the app to generate Assets/Plugins/Android/AndroidManifest.xml and then rebuild it again.");
                    return;
                }
                else
                {
                    File.Copy(manifest0, manifest);
                }
            }
            var changed         = false;
            var androidManifest = new AndroidManifest(manifest);
            changed = (androidManifest.SetHardwareAccelerated(true) || changed);
#if UNITYWEBVIEW_ANDROID_USES_CLEARTEXT_TRAFFIC
            changed = (androidManifest.SetUsesCleartextTraffic(true) || changed);
#endif
#if UNITYWEBVIEW_ANDROID_ENABLE_CAMERA
            changed = (androidManifest.AddCamera() || changed);
#endif
#if UNITYWEBVIEW_ANDROID_ENABLE_MICROPHONE
            changed = (androidManifest.AddMicrophone() || changed);
#endif
#if UNITY_5_6_0 || UNITY_5_6_1
            changed = (androidManifest.SetActivityName("net.gree.unitywebview.CUnityPlayerActivity") || changed);
#endif
            if (changed)
            {
                androidManifest.Save();
                Debug.LogError("unitywebview: adjusted AndroidManifest.xml. Please rebuild the app.");
            }
        }
#endif
    }
Exemplo n.º 5
0
    //// 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

#if UNITY_2018_1_OR_NEWER
    public void OnPostGenerateGradleAndroidProject(string basePath)
    {
        var changed         = false;
        var androidManifest = new AndroidManifest(GetManifestPath(basePath));

        changed = (androidManifest.SetHardwareAccelerated(true) || changed);
#if UNITYWEBVIEW_ANDROID_USES_CLEARTEXT_TRAFFIC
        changed = (androidManifest.SetUsesCleartextTraffic(true) || changed);
#endif
#if UNITYWEBVIEW_ANDROID_ENABLE_CAMERA
        changed = (androidManifest.AddCamera() || changed);
#endif
#if UNITYWEBVIEW_ANDROID_ENABLE_MICROPHONE
        changed = (androidManifest.AddMicrophone() || changed);
#endif
        if (changed)
        {
            androidManifest.Save();
            Debug.Log("unitywebview: adjusted AndroidManifest.xml.");
        }
    }
    //// 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

#if UNITY_2018_1_OR_NEWER
    public void OnPostGenerateGradleAndroidProject(string basePath)
    {
        var changed         = false;
        var androidManifest = new AndroidManifest(GetManifestPath(basePath));

        if (!nofragment)
        {
            changed = (androidManifest.AddFileProvider(basePath) || changed);
            {
                var path   = GetBuildGradlePath(basePath);
                var lines0 = File.ReadAllText(path).Replace("\r\n", "\n").Replace("\r", "\n").Split(new[] { '\n' });
                {
                    var lines          = new List <string>();
                    var independencies = false;
                    foreach (var line in lines0)
                    {
                        if (line == "dependencies {")
                        {
                            independencies = true;
                        }
                        else if (independencies && line == "}")
                        {
                            independencies = false;
                            lines.Add("    implementation 'androidx.core:core:1.6.0'");
                        }
                        else if (independencies)
                        {
                            if (line.Contains("implementation(name: 'core") ||
                                line.Contains("implementation(name: 'androidx.core.core") ||
                                line.Contains("implementation 'androidx.core:core"))
                            {
                                break;
                            }
                        }
                        lines.Add(line);
                    }
                    if (lines.Count > lines0.Length)
                    {
                        File.WriteAllText(path, string.Join("\n", lines) + "\n");
                    }
                }
            }
            {
                var path   = GetGradlePropertiesPath(basePath);
                var lines0 = "";
                var lines  = "";
                if (File.Exists(path))
                {
                    lines0 = File.ReadAllText(path).Replace("\r\n", "\n").Replace("\r", "\n") + "\n";
                    lines  = lines0;
                }
                if (!lines.Contains("android.useAndroidX=true"))
                {
                    lines += "android.useAndroidX=true\n";
                }
                if (!lines.Contains("android.enableJetifier=true"))
                {
                    lines += "android.enableJetifier=true\n";
                }
                if (lines != lines0)
                {
                    File.WriteAllText(path, lines);
                }
            }
        }
        changed = (androidManifest.SetHardwareAccelerated(true) || changed);
#if UNITYWEBVIEW_ANDROID_USES_CLEARTEXT_TRAFFIC
        changed = (androidManifest.SetUsesCleartextTraffic(true) || changed);
#endif
#if UNITYWEBVIEW_ANDROID_ENABLE_CAMERA
        changed = (androidManifest.AddCamera() || changed);
#endif
#if UNITYWEBVIEW_ANDROID_ENABLE_MICROPHONE
        changed = (androidManifest.AddMicrophone() || changed);
#endif
        if (changed)
        {
            androidManifest.Save();
            Debug.Log("unitywebview: adjusted AndroidManifest.xml.");
        }
    }
    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))
            {
                string manifest0 = Path.Combine(Application.dataPath, "../Temp/StagingArea/AndroidManifest-main.xml");
                if (!File.Exists(manifest0))
                {
                    Debug.LogError("unitywebview: cannot find both Assets/Plugins/Android/AndroidManifest.xml and Temp/StagingArea/AndroidManifest-main.xml. please build the app to generate Assets/Plugins/Android/AndroidManifest.xml and then rebuild it again.");
                    return;
                }
                else
                {
                    File.Copy(manifest0, manifest);
                }
            }
            var changed         = false;
            var androidManifest = new AndroidManifest(manifest);
            if (!nofragment)
            {
                changed = (androidManifest.AddFileProvider("Assets/Plugins/Android") || changed);
                var files = Directory.GetFiles("Assets/Plugins/Android/");
                var found = false;
                foreach (var file in files)
                {
                    if (Regex.IsMatch(file, @"^Assets/Plugins/Android/(androidx\.core\.)?core-.*.aar$"))
                    {
                        Debug.LogError("XXX");
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    foreach (var file in files)
                    {
                        var match = Regex.Match(file, @"^Assets/Plugins/Android/(core.*.aar).tmpl$");
                        if (match.Success)
                        {
                            var name = match.Groups[1].Value;
                            File.Copy(file, "Assets/Plugins/Android/" + name);
                            break;
                        }
                    }
                }
            }
            changed = (androidManifest.SetHardwareAccelerated(true) || changed);
#if UNITYWEBVIEW_ANDROID_USES_CLEARTEXT_TRAFFIC
            changed = (androidManifest.SetUsesCleartextTraffic(true) || changed);
#endif
#if UNITYWEBVIEW_ANDROID_ENABLE_CAMERA
            changed = (androidManifest.AddCamera() || changed);
#endif
#if UNITYWEBVIEW_ANDROID_ENABLE_MICROPHONE
            changed = (androidManifest.AddMicrophone() || changed);
#endif
#if UNITY_5_6_0 || UNITY_5_6_1
            changed = (androidManifest.SetActivityName("net.gree.unitywebview.CUnityPlayerActivity") || changed);
#endif
            if (changed)
            {
                androidManifest.Save();
                Debug.LogError("unitywebview: adjusted AndroidManifest.xml. Please rebuild the app.");
            }
        }
#endif
        if (buildTarget == BuildTarget.iOS)
        {
            string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
            var    type     = Type.GetType("UnityEditor.iOS.Xcode.PBXProject, UnityEditor.iOS.Extensions.Xcode");
            if (type == null)
            {
                Debug.LogError("unitywebview: failed to get PBXProject. please install iOS build support.");
                return;
            }
            var src = File.ReadAllText(projPath);
            //dynamic proj = type.GetConstructor(Type.EmptyTypes).Invoke(null);
            var proj = type.GetConstructor(Type.EmptyTypes).Invoke(null);
            //proj.ReadFromString(src);
            {
                var method = type.GetMethod("ReadFromString");
                method.Invoke(proj, new object[] { src });
            }
            var target = "";
#if UNITY_2019_3_OR_NEWER
            //target = proj.GetUnityFrameworkTargetGuid();
            {
                var method = type.GetMethod("GetUnityFrameworkTargetGuid");
                target = (string)method.Invoke(proj, null);
            }
#else
            //target = proj.TargetGuidByName("Unity-iPhone");
            {
                var method = type.GetMethod("TargetGuidByName");
                target = (string)method.Invoke(proj, new object[] { "Unity-iPhone" });
            }
#endif
            //proj.AddFrameworkToProject(target, "WebKit.framework", false);
            {
                var method = type.GetMethod("AddFrameworkToProject");
                method.Invoke(proj, new object[] { target, "WebKit.framework", false });
            }
#if UNITYWEBVIEW_IOS_ALLOW_FILE_URLS
            // proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-DUNITYWEBVIEW_IOS_ALLOW_FILE_URLS");
            {
                var method = type.GetMethod("AddBuildProperty", new Type[] { typeof(string), typeof(string), typeof(string) });
                method.Invoke(proj, new object[] { target, "OTHER_CFLAGS", "-DUNITYWEBVIEW_IOS_ALLOW_FILE_URLS" });
            }
#endif
            var dst = "";
            //dst = proj.WriteToString();
            {
                var method = type.GetMethod("WriteToString");
                dst = (string)method.Invoke(proj, null);
            }
            File.WriteAllText(projPath, dst);
        }
    }