Android Question Unity Ads?

SSDM71

Active Member
Licensed User
Longtime User
Is possible have unity ads for B4A? Have someone already used Unity Ads?

I would use Unity Ads for the videos, but in their documentation there isn't the support to B4A but only the SDK and I don't be able to create a B4A library from SDK, also I don't know if it is possible or not

Thanks,
 

Informatix

Expert
Licensed User
Longtime User
I won't be surprised that UnityAds works only with Unity, which is a stand-alone solution.
 
Upvote 0

SSDM71

Active Member
Licensed User
Longtime User

This is the reply from Unity Tecnologies for my question. Therefore I understand that is possible use the SDK of Unity Ads, but I don't be able to build a library from SDK.I used once Eclipse but I did't so be able to use. Can this video tutorial util for me to build a library?

Thanks,
 
Upvote 0

cambopad

Active Member
Licensed User
Longtime User
I heard many people had great eCPM from UnityAds! I wish expert here could create a wrapper(possibly a chargeable one) for this ad!
 
Upvote 0

SSDM71

Active Member
Licensed User
Longtime User
It can be a START.... Yes.

But you need to learn java (including knowledge about android) to write a wrapper with eclipse...

Know that I can create this library is already a good start. I can always learn from the person more expert than me. I hope to find someone that is able to do a wrapper.

I heard many people had great eCPM from UnityAds! I wish expert here could create a wrapper(possibly a chargeable one) for this ad!

I think that Unity Ads are the best for the games, with good eCPM and the possibility to advertise your game with your earning.
 
Upvote 0

SSDM71

Active Member
Licensed User
Longtime User
I can use this wrapper for B4A? I resolve the problem to obtain an Activity from B4AActivity using BA.activity()
I use the following code:
B4X:
package com.unity3d.ads.android.unity3d;

import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BA;
import java.lang.reflect.Method;
import java.util.HashMap;
import android.app.Activity;
import com.unity3d.ads.android.UnityAds;
import com.unity3d.ads.android.UnityAdsDeviceLog;
import com.unity3d.ads.android.IUnityAdsListener;
import com.unity3d.ads.android.UnityAdsUtils;
import com.unity3d.ads.android.properties.UnityAdsProperties;
import com.unity3d.ads.android.webapp.UnityAdsWebData;
import com.unity3d.ads.android.zone.UnityAdsZoneManager;
@ShortName("UnityAdsWrapper")
public class UnityAdsWrapper implements IUnityAdsListener {
    private Activity _startupActivity = null;
    private String _gameObject = null;
    private String _gameId = null;
    private Method _sendMessageMethod = null;
    private boolean _testMode = false;
    private static Boolean _constructed = false;
    private static Boolean _initialized = false;

    public UnityAdsWrapper () {
        if (!_constructed) {
            _constructed = true;
            try {
                    Class<?> unityClass = Class.forName("com.unity3d.player.UnityPlayer");
                    Class<?> paramTypes[] = new Class[3];
                    paramTypes[0] = String.class;
                    paramTypes[1] = String.class;
                    paramTypes[2] = String.class;
                    _sendMessageMethod = unityClass.getDeclaredMethod("UnitySendMessage", paramTypes);
            }
            catch (Exception e) {
                UnityAdsDeviceLog.error("Error getting class or method of com.unity3d.player.UnityPlayer, method UnitySendMessage(string, string, string). " + e.getLocalizedMessage());
            }
        }
    }
    public boolean isSupported () {
        return UnityAds.isSupported();
    }
    public String getSDKVersion () {
        return UnityAds.getSDKVersion();
    }
    public void Initialize(final String gameId,BA ba, boolean testMode, String gameObject, final String unityVersion) {
        if (!_initialized) {
            _initialized = true;
            _gameId = gameId;
            _gameObject = gameObject;
            _testMode = testMode;
            if (_startupActivity == null)
                _startupActivity = ba.activity;
            final UnityAdsWrapper listener = this;

            try {
                UnityAdsUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        UnityAds.setTestMode(_testMode);
            if(unityVersion.length() > 0) {
              UnityAdsProperties.UNITY_VERSION = unityVersion;
            }
                        UnityAds.init(_startupActivity, _gameId, listener);
                    }
                });
            }
            catch (Exception e) {
                UnityAdsDeviceLog.error("Error occured while initializing Unity Ads");
            }
        }
    }
    public boolean show (final String zoneId, final String rewardItemKey, final String optionsString) {
        if(canShowZone(zoneId)) {
            HashMap<String, Object> options = null;

            if(optionsString.length() > 0) {
                options = new HashMap<String, Object>();
                for(String rawOptionPair : optionsString.split(",")) {
                    String[] optionPair = rawOptionPair.split(":");
                    options.put(optionPair[0], optionPair[1]);
                }
            }

            if(rewardItemKey.length() > 0) {
                if(zoneId != null && zoneId.length() > 0) {
                    UnityAds.setZone(zoneId, rewardItemKey);
                }
            } else {
                if(zoneId != null && zoneId.length() > 0) {
                    UnityAds.setZone(zoneId);
                }
            }

            return UnityAds.show(options);
        }

        return false;
    }

    public void hide () {
        UnityAds.hide();
    }

    public boolean canShow () {
        return UnityAds.canShow();
    }

    public boolean canShowZone(String zone) {
        if(zone != null && zone.length() > 0) {
            UnityAdsZoneManager zoneManager = UnityAdsWebData.getZoneManager();
            if(zoneManager != null) {
                if(zoneManager.getZone(zone) != null) {
                    return UnityAds.canShow();
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

        return UnityAds.canShow();
    }

    public boolean hasMultipleRewardItems () {
        return UnityAds.hasMultipleRewardItems();
    }

    public String getRewardItemKeys () {
        if (UnityAds.getRewardItemKeys() == null) return null;
        if (UnityAds.getRewardItemKeys().size() > 0) {
            String keys = "";
            String key="";
            for (int i=0; i<UnityAds.getRewardItemKeys().size(); i++) {
                UnityAds.getRewardItemKeys().set(i, key);
                if (UnityAds.getRewardItemKeys().indexOf(key) > 0) {
                    keys += ";";
                }
                keys += key;
            }
            return keys;
        }

        return null;
    }

    public String getDefaultRewardItemKey () {
        return UnityAds.getDefaultRewardItemKey();
    }

    public String getCurrentRewardItemKey () {
        return UnityAds.getCurrentRewardItemKey();
    }

    public boolean setRewardItemKey (String rewardItemKey) {
        return UnityAds.setRewardItemKey(rewardItemKey);
    }

    public void setDefaultRewardItemAsRewardItem () {
        UnityAds.setDefaultRewardItemAsRewardItem();
    }

    public String getRewardItemDetailsWithKey (String rewardItemKey) {
        String retString = "";

        if (UnityAds.getRewardItemDetailsWithKey(rewardItemKey) != null) {
            UnityAdsDeviceLog.debug("Fetching reward data");

            @SuppressWarnings({ "unchecked", "rawtypes" })
            HashMap<String, String> rewardMap = (HashMap)UnityAds.getRewardItemDetailsWithKey(rewardItemKey);

            if (rewardMap != null) {
                retString = rewardMap.get(UnityAds.UNITY_ADS_REWARDITEM_NAME_KEY);
                retString += ";" + rewardMap.get(UnityAds.UNITY_ADS_REWARDITEM_PICTURE_KEY);
                return retString;
            }
            else {
                UnityAdsDeviceLog.debug("Problems getting reward item details");
            }
        }
        else {
            UnityAdsDeviceLog.debug("Could not find reward item details");
        }
        return "";
    }

    public String getRewardItemDetailsKeys () {
        return String.format("%s;%s", UnityAds.UNITY_ADS_REWARDITEM_NAME_KEY, UnityAds.UNITY_ADS_REWARDITEM_PICTURE_KEY);
    }

    public void setLogLevel(int logLevel) {
        UnityAdsDeviceLog.setLogLevel(logLevel);
    }

    public void enableUnityDeveloperInternalTestMode() {
        UnityAds.enableUnityDeveloperInternalTestMode();
    }

    public void setCampaignDataURL(String campaignDataURL) {
        UnityAds.setCampaignDataURL(campaignDataURL);
    }

    // IUnityAdsListener

    @Override
    public void onHide() {
        sendMessageToUnity3D("onHide", null);
    }

    @Override
    public void onShow() {
        sendMessageToUnity3D("onShow", null);
    }

    @Override
    public void onVideoStarted() {
        sendMessageToUnity3D("onVideoStarted", null);
    }

    @Override
    public void onVideoCompleted(String rewardItemKey, boolean skipped) {
        sendMessageToUnity3D("onVideoCompleted", rewardItemKey + ";" + (skipped ? "true" : "false"));
    }

    @Override
    public void onFetchCompleted() {
        sendMessageToUnity3D("onFetchCompleted", null);
    }

    @Override
    public void onFetchFailed() {
        sendMessageToUnity3D("onFetchFailed", null);
    }

    public void sendMessageToUnity3D(String methodName, String parameter) {
        // Unity Development build crashes if parameter is NULL
        if (parameter == null)
                parameter = "";

        if (_sendMessageMethod == null) {
            UnityAdsDeviceLog.error("Cannot send message to Unity3D. Method is null");
            return;
        }
        try {
            UnityAdsDeviceLog.debug("Sending message (" + methodName + ", " + parameter + ") to Unity3D");
            _sendMessageMethod.invoke(null, _gameObject, methodName, parameter);
        }
        catch (Exception e) {
            UnityAdsDeviceLog.error("Can't invoke UnitySendMessage method. Error = "  + e.getLocalizedMessage());
        }
    }

}

But when I compile in B4A it give me this error:
B4X:
B4A version: 5.02 (1)
Parsing code.    (0.01s)
Compiling code.    (0.01s)
Compiling layouts code.    (0.00s)
Generating R file.    (0.04s)
Compiling generated Java code.    Error
B4A line: 26
ua.Initialize(\
javac 1.8.0_25
src\b4a\example\main.java:343: error: cannot access IUnityAdsListener
mostCurrent._ua.Initialize("59593",processBA,anywheresoftware.b4a.keywords.Common.False,"","1.4.7");
               ^
  class file for com.unity3d.ads.android.IUnityAdsListener not found
1 error

I load the library and i have imported IUnityAdsListener in the wrapper.
Which is my mistake?

Thanks,
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
You need to initialize the object, writing a public void Initialize sub is not enough to initialize the UnityAds object, you need to do something like this:
B4X:
setObject(UnityAds);
But you need to extends the ViewWrapper<UnityAds>

I haven't wrapped an ads library in a while, but this should give you a hint.
You should do a search for the Google Admob library sourcecode, i remember Erel posting it somewhere.

Good Luck!
Walter
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…