private static void AddAppLinkingActivity(XmlDocument doc, XmlNode xmlNode, string ns, List <string> schemes)
        {
            XmlElement element = ManifestMod.CreateActivityElement(doc, ns, AppLinkActivityName, true);

            foreach (var scheme in schemes)
            {
                // We have to create an intent filter for each scheme since an intent filter
                // can have only one data element.
                XmlElement intentFilter = doc.CreateElement("intent-filter");

                var action = doc.CreateElement("action");
                action.SetAttribute("name", ns, "android.intent.action.VIEW");
                intentFilter.AppendChild(action);

                var category = doc.CreateElement("category");
                category.SetAttribute("name", ns, "android.intent.category.DEFAULT");
                intentFilter.AppendChild(category);

                XmlElement dataElement = doc.CreateElement("data");
                dataElement.SetAttribute("scheme", ns, scheme);
                intentFilter.AppendChild(dataElement);
                element.AppendChild(intentFilter);
            }

            ManifestMod.SetOrReplaceXmlElement(xmlNode, element);
        }
        private static XmlElement CreateUnityOverlayElement(XmlDocument doc, string ns, string activityName)
        {
            // <activity android:name="activityName" android:configChanges="all|of|them" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
            // </activity>
            XmlElement activityElement = ManifestMod.CreateActivityElement(doc, ns, activityName);

            activityElement.SetAttribute("configChanges", ns, "fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen");
            activityElement.SetAttribute("theme", ns, "@android:style/Theme.Translucent.NoTitleBar.Fullscreen");
            return(activityElement);
        }
        private void AndroidUtilGUI()
        {
            this.showAndroidUtils = EditorGUILayout.Foldout(this.showAndroidUtils, "Android Build Facebook Settings");
            if (this.showAndroidUtils)
            {
                if (!FacebookAndroidUtil.SetupProperly)
                {
                    var msg = "Your Android setup is not right. Check the documentation.";
                    switch (FacebookAndroidUtil.SetupError)
                    {
                    case FacebookAndroidUtil.ErrorNoSDK:
                        msg = "You don't have the Android SDK setup!  Go to " + (Application.platform == RuntimePlatform.OSXEditor ? "Unity" : "Edit") + "->Preferences... and set your Android SDK Location under External Tools";
                        break;

                    case FacebookAndroidUtil.ErrorNoKeystore:
                        msg = "Your android debug keystore file is missing! You can create new one by creating and building empty Android project in Ecplise.";
                        break;

                    case FacebookAndroidUtil.ErrorNoKeytool:
                        msg = "Keytool not found. Make sure that Java is installed, and that Java tools are in your path.";
                        break;

                    case FacebookAndroidUtil.ErrorNoOpenSSL:
                        msg = "OpenSSL not found. Make sure that OpenSSL is installed, and that it is in your path.";
                        break;

                    case FacebookAndroidUtil.ErrorKeytoolError:
                        msg = "Unkown error while getting Debug Android Key Hash.";
                        break;
                    }

                    EditorGUILayout.HelpBox(msg, MessageType.Warning);
                }

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(this.androidKeystorePathLabel, GUILayout.Width(180), GUILayout.Height(16));
                FacebookSettings.AndroidKeystorePath = EditorGUILayout.TextField(FacebookSettings.AndroidKeystorePath);
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.LabelField(
                    "Copy and Paste these into your \"Native Android App\" Settings on developers.facebook.com/apps");
                this.SelectableLabelField(this.packageNameLabel, Utility.GetApplicationIdentifier());
                this.SelectableLabelField(this.classNameLabel, ManifestMod.DeepLinkingActivityName);
                this.SelectableLabelField(this.debugAndroidKeyLabel, FacebookAndroidUtil.DebugKeyHash);
                if (GUILayout.Button("Regenerate Android Manifest"))
                {
                    ManifestMod.GenerateManifest();
                }
            }

            EditorGUILayout.Space();
        }
Esempio n. 4
0
        // Exporting the *.unityPackage for Asset store
        public static string ExportPackage()
        {
            Debug.Log("Exporting Facebook Unity Package...");
            string path = OutputPath;

            try
            {
                if (!File.Exists(Path.Combine(Application.dataPath, "Temp")))
                {
                    AssetDatabase.CreateFolder("Assets", "Temp");
                }

                AssetDatabase.MoveAsset(SDKPath + "Resources/FacebookSettings.asset", "Assets/Temp/FacebookSettings.asset");
                AssetDatabase.DeleteAsset(PluginsPath + "Android/AndroidManifest.xml");
                AssetDatabase.DeleteAsset(PluginsPath + "Android/AndroidManifest.xml.meta");

                string[] facebookFiles = (string[])Directory.GetFiles(FacebookPath, "*.*", SearchOption.TopDirectoryOnly);
                string[] sdkFiles      = (string[])Directory.GetFiles(SDKPath, "*.*", SearchOption.AllDirectories);
                string[] exampleFiles  = (string[])Directory.GetFiles(ExamplesPath, "*.*", SearchOption.AllDirectories);
                string[] pluginsFiles  = (string[])Directory.GetFiles(PluginsPath, "*.*", SearchOption.AllDirectories);

                string[] playServicesResolverFiles = (string[])Directory.GetFiles(PlayServicesResolverPath, "*.*",
                                                                                  SearchOption.AllDirectories);
                string[] files = new string[facebookFiles.Length + sdkFiles.Length + exampleFiles.Length +
                                            pluginsFiles.Length + playServicesResolverFiles.Length];
                facebookFiles.CopyTo(files, 0);
                sdkFiles.CopyTo(files, facebookFiles.Length);
                exampleFiles.CopyTo(files, sdkFiles.Length + facebookFiles.Length);
                pluginsFiles.CopyTo(files, sdkFiles.Length + facebookFiles.Length + exampleFiles.Length);
                playServicesResolverFiles.CopyTo(files, sdkFiles.Length + facebookFiles.Length + exampleFiles.Length + pluginsFiles.Length);

                AssetDatabase.ExportPackage(
                    files,
                    path,
                    ExportPackageOptions.IncludeDependencies | ExportPackageOptions.Recurse);
            }
            finally
            {
                // Move files back no matter what
                AssetDatabase.MoveAsset("Assets/Temp/FacebookSettings.asset", SDKPath + "Resources/FacebookSettings.asset");
                AssetDatabase.DeleteAsset("Assets/Temp");

                // regenerate the manifest
                ManifestMod.GenerateManifest();
            }

            Debug.Log("Finished exporting!");

            return(path);
        }
        public static void GenerateManifest()
        {
            var outputFile = Path.Combine(Application.dataPath, ManifestMod.AndroidManifestPath);

            // Create containing directory if it does not exist
            Directory.CreateDirectory(Path.GetDirectoryName(outputFile));

            // only copy over a fresh copy of the AndroidManifest if one does not exist
            if (!File.Exists(outputFile))
            {
                ManifestMod.CreateDefaultAndroidManifest(outputFile);
            }

            UpdateManifest(outputFile);
        }
        public static bool CheckManifest()
        {
            bool result     = true;
            var  outputFile = Path.Combine(Application.dataPath, ManifestMod.AndroidManifestPath);

            if (!File.Exists(outputFile))
            {
                Debug.LogError("An android manifest must be generated for the Facebook SDK to work.  Go to Facebook->Edit Settings and press \"Regenerate Android Manifest\"");
                return(false);
            }

            XmlDocument doc = new XmlDocument();

            doc.Load(outputFile);

            if (doc == null)
            {
                Debug.LogError("Couldn't load " + outputFile);
                return(false);
            }

            XmlNode manNode = FindChildNode(doc, "manifest");
            XmlNode dict    = FindChildNode(manNode, "application");

            if (dict == null)
            {
                Debug.LogError("Error parsing " + outputFile);
                return(false);
            }

            XmlElement loginElement;

            if (!ManifestMod.TryFindElementWithAndroidName(dict, UnityLoginActivityName, out loginElement))
            {
                Debug.LogError(string.Format("{0} is missing from your android manifest.  Go to Facebook->Edit Settings and press \"Regenerate Android Manifest\"", UnityLoginActivityName));
                result = false;
            }

            var        deprecatedMainActivityName = "com.facebook.unity.FBUnityPlayerActivity";
            XmlElement deprecatedElement;

            if (ManifestMod.TryFindElementWithAndroidName(dict, deprecatedMainActivityName, out deprecatedElement))
            {
                Debug.LogWarning(string.Format("{0} is deprecated and no longer needed for the Facebook SDK.  Feel free to use your own main activity or use the default \"com.unity3d.player.UnityPlayerNativeActivity\"", deprecatedMainActivityName));
            }

            return(result);
        }
        private void AppLinksUtilGUI()
        {
            this.showAppLinksSettings = EditorGUILayout.Foldout(this.showAppLinksSettings, "App Links Settings");
            if (this.showAppLinksSettings)
            {
                for (int i = 0; i < FacebookSettings.AppLinkSchemes.Count; ++i)
                {
                    EditorGUILayout.LabelField(string.Format("App Link Schemes for '{0}'", FacebookSettings.AppLabels[i]));
                    List <string> currentAppLinkSchemes = FacebookSettings.AppLinkSchemes[i].Schemes;
                    for (int j = 0; j < currentAppLinkSchemes.Count; ++j)
                    {
                        GUI.changed = false;
                        string scheme = EditorGUILayout.TextField(currentAppLinkSchemes[j]);
                        if (scheme != currentAppLinkSchemes[j])
                        {
                            currentAppLinkSchemes[j] = scheme;
                            this.SettingsChanged();
                        }

                        if (GUI.changed)
                        {
                            ManifestMod.GenerateManifest();
                        }
                    }

                    EditorGUILayout.BeginHorizontal();
                    if (GUILayout.Button("Add a Scheme"))
                    {
                        FacebookSettings.AppLinkSchemes[i].Schemes.Add(string.Empty);
                        this.SettingsChanged();
                    }

                    if (currentAppLinkSchemes.Count > 0)
                    {
                        if (GUILayout.Button("Remove Last Scheme"))
                        {
                            FacebookSettings.AppLinkSchemes[i].Schemes.Pop();
                        }
                    }

                    EditorGUILayout.EndHorizontal();
                }
            }
        }
        private void AndroidUtilGUI()
        {
            showAndroidUtils = EditorGUILayout.Foldout(showAndroidUtils, "Android Build Facebook Settings");
            if (showAndroidUtils)
            {
                if (!FacebookAndroidUtil.IsSetupProperly())
                {
                    var msg = "Your Android setup is not right. Check the documentation.";
                    switch (FacebookAndroidUtil.SetupError)
                    {
                    case FacebookAndroidUtil.ERROR_NO_SDK:
                        msg = "You don't have the Android SDK setup!  Go to " + (Application.platform == RuntimePlatform.OSXEditor ? "Unity" : "Edit") + "->Preferences... and set your Android SDK Location under External Tools";
                        break;

                    case FacebookAndroidUtil.ERROR_NO_KEYSTORE:
                        msg = "Your android debug keystore file is missing! You can create new one by creating and building empty Android project in Ecplise.";
                        break;

                    case FacebookAndroidUtil.ERROR_NO_KEYTOOL:
                        msg = "Keytool not found. Make sure that Java is installed, and that Java tools are in your path.";
                        break;

                    case FacebookAndroidUtil.ERROR_NO_OPENSSL:
                        msg = "OpenSSL not found. Make sure that OpenSSL is installed, and that it is in your path.";
                        break;

                    case FacebookAndroidUtil.ERROR_KEYTOOL_ERROR:
                        msg = "Unkown error while getting Debug Android Key Hash.";
                        break;
                    }
                    EditorGUILayout.HelpBox(msg, MessageType.Warning);
                }
                EditorGUILayout.HelpBox("Copy and Paste these into your \"Native Android App\" Settings on developers.facebook.com/apps", MessageType.None);
                SelectableLabelField(packageNameLabel, PlayerSettings.bundleIdentifier);
                SelectableLabelField(classNameLabel, ManifestMod.DeepLinkingActivityName);
                SelectableLabelField(debugAndroidKeyLabel, FacebookAndroidUtil.DebugKeyHash);
                if (GUILayout.Button("Regenerate Android Manifest"))
                {
                    ManifestMod.GenerateManifest();
                }
            }
            EditorGUILayout.Space();
        }
Esempio n. 9
0
        public static void OnPostProcessBuild(BuildTarget target, string path)
        {
            // If integrating with facebook on any platform, throw a warning if the app id is invalid
            if (!FacebookSettings.IsValidAppId)
            {
                Debug.LogWarning("You didn't specify a Facebook app ID.  Please add one using the Facebook menu in the main Unity editor.");
            }

            // Unity renamed build target from iPhone to iOS in Unity 5, this keeps both versions happy
            if (target.ToString() == "iOS" || target.ToString() == "iPhone")
            {
                UpdatePlist(path);
                FixupFiles.FixSimulator(path);
                FixupFiles.AddVersionDefine(path);
                FixupFiles.FixColdStart(path);
                FixupFiles.AddBuildFlag(path);
            }

            if (target == BuildTarget.Android)
            {
                // The default Bundle Identifier for Unity does magical things that causes bad stuff to happen
                var defaultIdentifier = "com.Company.ProductName";

                if (Utility.GetApplicationIdentifier() == defaultIdentifier)
                {
                    Debug.LogError("The default Unity Bundle Identifier (com.Company.ProductName) will not work correctly.");
                }

                if (!FacebookAndroidUtil.SetupProperly)
                {
                    Debug.LogError("Your Android setup is not correct. See Settings in Facebook menu.");
                }

                if (!ManifestMod.CheckManifest())
                {
                    // If something is wrong with the Android Manifest, try to regenerate it to fix it for the next build.
                    ManifestMod.GenerateManifest();
                }
            }
        }
        private void AppIdGUI()
        {
            EditorGUILayout.LabelField("Add the Facebook App Id(s) associated with this game");
            if (FacebookSettings.AppIds.Count == 0 || FacebookSettings.AppId == "0")
            {
                EditorGUILayout.HelpBox("Invalid App Id", MessageType.Error);
            }

            for (int i = 0; i < FacebookSettings.AppIds.Count; ++i)
            {
                EditorGUILayout.BeginVertical();

                EditorGUILayout.LabelField(string.Format("App #{0}", i + 1));

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(this.appNameLabel);
                FacebookSettings.AppLabels[i] = EditorGUILayout.TextField(FacebookSettings.AppLabels[i]);
                EditorGUILayout.EndHorizontal();

                GUI.changed = false;
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(this.appIdLabel);
                FacebookSettings.AppIds[i] = EditorGUILayout.TextField(FacebookSettings.AppIds[i]);
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(this.clientTokenLabel);
                FacebookSettings.ClientTokens[i] = EditorGUILayout.TextField(FacebookSettings.ClientTokens[i]);
                EditorGUILayout.EndHorizontal();

                if (GUI.changed)
                {
                    this.SettingsChanged();
                    ManifestMod.GenerateManifest();
                }

                EditorGUILayout.EndVertical();
            }

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Add Another App Id"))
            {
                FacebookSettings.AppLabels.Add("New App");
                FacebookSettings.AppIds.Add("0");
                FacebookSettings.ClientTokens.Add(string.Empty);
                FacebookSettings.AppLinkSchemes.Add(new FacebookSettings.UrlSchemes());
                this.SettingsChanged();
            }

            if (FacebookSettings.AppLabels.Count > 1)
            {
                if (GUILayout.Button("Remove Last App Id"))
                {
                    FacebookSettings.AppLabels.Pop();
                    FacebookSettings.AppIds.Pop();
                    FacebookSettings.ClientTokens.Pop();
                    FacebookSettings.AppLinkSchemes.Pop();
                    this.SettingsChanged();
                }
            }

            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();
            if (FacebookSettings.AppIds.Count > 1)
            {
                EditorGUILayout.HelpBox("2) Select Facebook App Id to be compiled with this game", MessageType.None);
                GUI.changed = false;
                FacebookSettings.SelectedAppIndex = EditorGUILayout.Popup(
                    "Selected App Id",
                    FacebookSettings.SelectedAppIndex,
                    FacebookSettings.AppIds.ToArray());
                if (GUI.changed)
                {
                    ManifestMod.GenerateManifest();
                }

                EditorGUILayout.Space();
            }
            else
            {
                FacebookSettings.SelectedAppIndex = 0;
            }
        }
        private static void AddSimpleActivity(XmlDocument doc, XmlNode xmlNode, string ns, string className, bool export = false)
        {
            XmlElement element = CreateActivityElement(doc, ns, className, export);

            ManifestMod.SetOrReplaceXmlElement(xmlNode, element);
        }
        public static void UpdateManifest(string fullPath)
        {
            string appId = FacebookSettings.AppId;

            if (!FacebookSettings.IsValidAppId)
            {
                Debug.LogError("You didn't specify a Facebook app ID.  Please add one using the Facebook menu in the main Unity editor.");
                return;
            }

            XmlDocument doc = new XmlDocument();

            doc.Load(fullPath);

            if (doc == null)
            {
                Debug.LogError("Couldn't load " + fullPath);
                return;
            }

            XmlNode manNode = FindChildNode(doc, "manifest");
            XmlNode dict    = FindChildNode(manNode, "application");

            if (dict == null)
            {
                Debug.LogError("Error parsing " + fullPath);
                return;
            }

            string ns = dict.GetNamespaceOfPrefix("android");

            // add the unity login activity
            XmlElement unityLoginElement = CreateUnityOverlayElement(doc, ns, UnityLoginActivityName);

            ManifestMod.SetOrReplaceXmlElement(dict, unityLoginElement);

            // add the unity dialogs activity
            XmlElement unityDialogsElement = CreateUnityOverlayElement(doc, ns, UnityDialogsActivityName);

            ManifestMod.SetOrReplaceXmlElement(dict, unityDialogsElement);

            ManifestMod.AddAppLinkingActivity(doc, dict, ns, FacebookSettings.AppLinkSchemes[FacebookSettings.SelectedAppIndex].Schemes);

            ManifestMod.AddSimpleActivity(doc, dict, ns, DeepLinkingActivityName, true);
            ManifestMod.AddSimpleActivity(doc, dict, ns, UnityGameRequestActivityName);
            ManifestMod.AddSimpleActivity(doc, dict, ns, UnityGameGroupCreateActivityName);
            ManifestMod.AddSimpleActivity(doc, dict, ns, UnityGameGroupJoinActivityName);

            // add the app id
            // <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="\ fb<APPID>" />
            XmlElement appIdElement = doc.CreateElement("meta-data");

            appIdElement.SetAttribute("name", ns, ApplicationIdMetaDataName);
            appIdElement.SetAttribute("value", ns, "fb" + appId);
            ManifestMod.SetOrReplaceXmlElement(dict, appIdElement);

            // enable AutoLogAppEventsEnabled by default
            // <meta-data android:name="com.facebook.sdk.AutoLogAppEventsEnabled" android:value="true"/>
            string     autoLogAppEventsEnabled        = FacebookSettings.AutoLogAppEventsEnabled.ToString().ToLower();
            XmlElement autoLogAppEventsEnabledElement = doc.CreateElement("meta-data");

            autoLogAppEventsEnabledElement.SetAttribute("name", ns, AutoLogAppEventsEnabled);
            autoLogAppEventsEnabledElement.SetAttribute("value", ns, autoLogAppEventsEnabled);
            ManifestMod.SetOrReplaceXmlElement(dict, autoLogAppEventsEnabledElement);

            // enable AdvertiserIDCollectionEnabled by default
            // <meta-data android:name="com.facebook.sdk.AdvertiserIDCollectionEnabled" android:value="true"/>
            string     advertiserIDCollectionEnabled        = FacebookSettings.AdvertiserIDCollectionEnabled.ToString().ToLower();
            XmlElement advertiserIDCollectionEnabledElement = doc.CreateElement("meta-data");

            advertiserIDCollectionEnabledElement.SetAttribute("name", ns, AdvertiserIDCollectionEnabled);
            advertiserIDCollectionEnabledElement.SetAttribute("value", ns, advertiserIDCollectionEnabled);
            ManifestMod.SetOrReplaceXmlElement(dict, advertiserIDCollectionEnabledElement);

            // Add the facebook content provider
            // <provider
            //   android:name="com.facebook.FacebookContentProvider"
            //   android:authorities="com.facebook.app.FacebookContentProvider<APPID>"
            //   android:exported="true" />
            XmlElement contentProviderElement = CreateContentProviderElement(doc, ns, appId);

            ManifestMod.SetOrReplaceXmlElement(dict, contentProviderElement);

            // Remove the FacebookActivity since we can rely on it in the androidsdk aar as of v4.12
            // (otherwise unity manifest merge likes fail if there's any difference at all)
            XmlElement facebookElement;

            if (TryFindElementWithAndroidName(dict, FacebookActivityName, out facebookElement))
            {
                dict.RemoveChild(facebookElement);
            }

            // Save the document formatted
            XmlWriterSettings settings = new XmlWriterSettings
            {
                Indent          = true,
                IndentChars     = "  ",
                NewLineChars    = "\r\n",
                NewLineHandling = NewLineHandling.Replace
            };

            using (XmlWriter xmlWriter = XmlWriter.Create(fullPath, settings))
            {
                doc.Save(xmlWriter);
            }
        }
        private void AppIdGUI()
        {
            EditorGUILayout.HelpBox("1) Add the Facebook App Id(s) associated with this game", MessageType.None);
            if (instance.AppIds.Length == 0 || instance.AppIds[instance.SelectedAppIndex] == "0")
            {
                EditorGUILayout.HelpBox("Invalid App Id", MessageType.Error);
            }
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField(appNameLabel);
            EditorGUILayout.LabelField(appIdLabel);
            EditorGUILayout.EndHorizontal();
            for (int i = 0; i < instance.AppIds.Length; ++i)
            {
                EditorGUILayout.BeginHorizontal();
                instance.SetAppLabel(i, EditorGUILayout.TextField(instance.AppLabels[i]));
                GUI.changed = false;
                instance.SetAppId(i, EditorGUILayout.TextField(instance.AppIds[i]));
                if (GUI.changed)
                {
                    ManifestMod.GenerateManifest();
                }
                EditorGUILayout.EndHorizontal();
            }
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Add Another App Id"))
            {
                var appLabels = new List <string>(instance.AppLabels);
                appLabels.Add("New App");
                instance.AppLabels = appLabels.ToArray();

                var appIds = new List <string>(instance.AppIds);
                appIds.Add("0");
                instance.AppIds = appIds.ToArray();
            }
            if (instance.AppLabels.Length > 1)
            {
                if (GUILayout.Button("Remove Last App Id"))
                {
                    var appLabels = new List <string>(instance.AppLabels);
                    appLabels.RemoveAt(appLabels.Count - 1);
                    instance.AppLabels = appLabels.ToArray();

                    var appIds = new List <string>(instance.AppIds);
                    appIds.RemoveAt(appIds.Count - 1);
                    instance.AppIds = appIds.ToArray();
                }
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();
            if (instance.AppIds.Length > 1)
            {
                EditorGUILayout.HelpBox("2) Select Facebook App Id to be compiled with this game", MessageType.None);
                GUI.changed = false;
                instance.SetAppIndex(EditorGUILayout.Popup("Selected App Id", instance.SelectedAppIndex, instance.AppLabels));
                if (GUI.changed)
                {
                    ManifestMod.GenerateManifest();
                }
                EditorGUILayout.Space();
            }
            else
            {
                instance.SetAppIndex(0);
            }
        }