Search code examples
facebookunity-game-enginenetwork-programmingsdkaudience

Facebook Audience Network SDK for Unity


I import the Unity Audience Network SDK 5.4.1 package to unity, any version and it gives me this error:

Assets\AudienceNetwork\Editor\AudienceNetworkPostprocess.cs(25,23): error CS0234: The type or namespace name 'iOS' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?)

if i remove the ios part and try to work on android only - nothing works, i try to play a reward scene and i get this error:

NullReferenceException: Object reference not set to an instance of an object AudienceNetwork.Utility.AdUtility.IsInitialized () (at Assets/AudienceNetwork/Library/AdUtility.cs:50) RewardedVideoAdScene.Awake () (at Assets/AudienceNetwork/Scenes/RewardedVideo/RewardedVideoAdScene.cs:21)

Is there a way to make this work? am i doing something wrong or missing something? does the previous plugin works? any links to it? thanks.


Solution

  • Without knowing what you removed the second issue is hard to tackle ...

    All we can say is that it refers to the line 51

     AndroidJavaObject context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
    

    where most probably currentActivity is null if executed on a PC since the line 50 right before

    AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    

    might not work in this case.


    The first one sounds like a "bug".

    You can use #if pre-processors with UNITY_IOS as a hotfix for at least making the Compiler error go away like

    /**
     * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
     *
     * You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
     * copy, modify, and distribute this software in source code or binary form for use
     * in connection with the web services and APIs provided by Facebook.
     *
     * As with any software that integrates with the Facebook platform, your use of
     * this software is subject to the Facebook Developer Principles and Policies
     * [http://developers.facebook.com/policy/]. This copyright 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.
     */
    namespace AudienceNetwork.Editor
    {
        using System.IO;
        using UnityEditor;
        using UnityEditor.Callbacks;
    #if UNITY_IOS
        using UnityEditor.iOS.Xcode;
    #endif
        using UnityEngine;
    
        public static class XCodePostProcess
        {
            public static string AudienceNetworkFramework = "FBAudienceNetwork.framework";
            public static string AudienceNetworkAAR = "AudienceNetwork.aar";
            public static string FrameworkDependenciesKey = "FrameworkDependencies";
            public static string RequiredFrameworks = "AdSupport;StoreKit;WebKit";
    
            [PostProcessBuild(100)]
            public static void OnPostProcessBuild(BuildTarget target, string path)
            {
    #if UNITY_IOS
                if (target == BuildTarget.iOS) {
                    string projectPath = PBXProject.GetPBXProjectPath(path);
                    PBXProject project = new PBXProject();
                    project.ReadFromString(File.ReadAllText(projectPath));
                    string targetName = PBXProject.GetUnityTargetName();
                    string targetGUID = project.TargetGuidByName(targetName);
                    project.AddFrameworkToProject(targetGUID, "AdSupport.framework", false);
                    project.AddFrameworkToProject(targetGUID, "StoreKit.framework", false);
                    project.AddFrameworkToProject(targetGUID, "WebKit.framework", false);
    
                    File.WriteAllText(projectPath, project.WriteToString());
                }
    #endif
    
                PluginImporter[] importers = PluginImporter.GetAllImporters();
                PluginImporter iOSPlugin = null;
                PluginImporter androidPlugin = null;
                foreach (PluginImporter importer in importers)
                {
                    if (importer.assetPath.Contains(AudienceNetworkFramework))
                    {
                        iOSPlugin = importer;
                        Debug.Log("Audience Network iOS plugin found at " + importer.assetPath + ".");
                    }
                    else if (importer.assetPath.Contains(AudienceNetworkAAR))
                    {
                        androidPlugin = importer;
                        Debug.Log("Audience Network Android plugin found at " + importer.assetPath + ".");
                    }
                }
                if (iOSPlugin != null)
                {
                    iOSPlugin.SetCompatibleWithAnyPlatform(false);
                    iOSPlugin.SetCompatibleWithEditor(false);
                    iOSPlugin.SetCompatibleWithPlatform(BuildTarget.iOS, true);
                    iOSPlugin.SetPlatformData(BuildTarget.iOS, FrameworkDependenciesKey, RequiredFrameworks);
                    iOSPlugin.SaveAndReimport();
                }
                if (androidPlugin != null)
                {
                    androidPlugin.SetCompatibleWithAnyPlatform(false);
                    androidPlugin.SetCompatibleWithEditor(false);
                    androidPlugin.SetCompatibleWithPlatform(BuildTarget.Android, true);
                    androidPlugin.SaveAndReimport();
                }
            }
        }
    }
    

    No warranty that this fixes all your issues, though.


    Google Ads had a similar error and used kind of the same hotfix.


    Otherwise you might want to consider installing the IOS build support which I guess would also make the error go away (but fills unnecessarily disk space of course in case you only want to build for Android)