Revmob fullscreen ad preload on Unity? - android

Is there any way to preload fullscreen ad on Unity? Right now when we call it using
revmob.ShowFullscreen();
when we create end game screen. But most of the time it loads after 5/10 secs later which is in-game most probably if you press restart, so it shows a full screen ad during gameplay.
I've found some ways to preload it on native android and tried same function to see if they exists in Unity but no luck.
Thanks.

Yes! You can use the following code:
private RevMobFullscreen fullscreen;
fullscreen = revmob.CreateFullscreen();
fullscreen.show();
If you need more information, you can access RevMob mobile ad network website: https://www.revmobmobileadnetwork.com

It will be better to add this code to the Create statement:
private RevMobFullscreen fullscreen;
fullscreen = revmob.CreateFullscreen();
...and then also this code to the listener:
RevMobAdsListener revmobListener = new RevMobAdsListener() {
// Required
#Override
public void onRevMobSessionIsStarted() {
fullscreen.show();
}
(...)
}
This will show the fullscreen ad.

You can do like this to preload revmob videos in unity. But there are memory leaks in revmob unity videos and they might fix that in 9.2.x...
REVMOB_APP_IDS = new Dictionary<string, string>() {
{ "Android", androidMediaId},
{ "IOS", iosMediaId }
};
revmob = RevMob.Start (REVMOB_APP_IDS, gameObject.name);
public void SessionIsStarted ()
{
CacheVideoInterstitial("Bootup");
}
public void CacheVideoInterstitial(string location) {
DestroyVideo();
StartCoroutine(CacheAfterEndofFrame(location));
}
IEnumerator CacheAfterEndofFrame(string location) {
yield return null;
fullscreenVideo = revmob.CreateVideo(location);
}
void DestroyVideo() {
if( fullscreenVideo != null ) {
fullscreenVideo.Hide();
//fullscreenVideo.Release();
//fullscreenVideo = null;
}
}
// revmob ad closing delegate
public void UserClosedTheAd (string revMobAdType)
{
DestroyVideo();
CacheVideoInterstitial(this.location);
}

Related

Unity GameObject "flash" after SetActive on Android Project

I have a project that use Android to display Unity Player. So I export Untiy project as Android module which implemented by Android Application.
I create buttons in Android Activity which contains UnityPlayer, And when I click button, it send a message to Unity Player to invoke C# function, just like this:
findViewById(R.id.btnChange).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mUnityPlayer.UnitySendMessage("ScriptHolder", "ChangeSkin", "");
}
});
And the function named "ChangeSkin" is just to change some GameObjects' active. Just like this:
void ChangeSkin()
{
int prefab;
if (_currentPrefab == PREFAB_DEFAULT)
{
prefab = PREFAB_PRINCESS;
}
else
{
prefab = PREFAB_DEFAULT;
}
ShowSkin(prefab);
}
private void ShowSkin(int prefab)
{
_currentPrefab = prefab;
foreach (var item in _defaultDressList)
{
item.SetActive(prefab == PREFAB_DEFAULT);
}
foreach (var item in _princessDressList)
{
item.SetActive(prefab == PREFAB_PRINCESS);
}
}
And something weird happening: when I click button to change the person's cloth in Unity, the GameObjects which called SetActive(true) show at the position above the right position for a frame and become normal, it looks like they flash. Here is the gif of the project demo:
It looks like the position offset is equal to the height of status bar. If I create a button on Unity Scene and call "ChangeSkin" function, everything will be OK.
I tried all I can to fix this but not succeed. So I hope you will help me, thx.
You should check your Unity layout. If you have a layout group that has content size fitter component, and the child objects also have it (content size fitter), this could cause these glitches.
I fixed this problem by using a flag (or trigger). Just like this:
// set value true to trigger ChangeSkin() in Update(),
// instead of invoke ChangeSkin() directly
private bool _changingSkinTrigger = false;
void ChangeSkin()
{
if (_currentPrefab == PREFAB_DEFAULT)
{
_currentPrefab = PREFAB_PRINCESS;
}
else
{
_currentPrefab = PREFAB_DEFAULT;
}
_changingSkinTrigger = true;
}
void Update()
{
if (_changingSkinTrigger)
{
_changingSkinTrigger = false;
ShowSkin();
}
}
private void ShowSkin()
{
foreach (var item in _defaultDressList)
{
item.SetActive(_currentPrefab == PREFAB_DEFAULT);
}
foreach (var item in _princessDressList)
{
item.SetActive(_currentPrefab == PREFAB_PRINCESS);
}
}
Thank #AndriyMarshalek for enlightening me.

Xamarin Forms - Take photograph without any user interaction

I have a requirement to take a photograph of a user in Xamarin Forms without them having to press the shutter button. For example, when the app launches it should show a preview and count down from 5 seconds (to give the user chance to get in position) then take a picture automatically.
I have tried the Xamarin Media Plugin library however this stackoverflow post and this GitHub issue state that this feature is not a supported.
I have seen a number of dead discussions such as this with people asking similar questions without resoltion.
I tried the LeadTools AutoCapture sample but this only seems to work for documents/text and not people (unless I am missing something??).
I am now working my way through the Camera2Basic sample which is quite old and only targets Android via android.hardware.camera2.
Are there any samples out there (or 3rd party libraries) that can acheive this requirement? Ideally I would like it to be cross platform (iOS and Android) but currently the main focus is Android.
You can create the Custom View Renderer on Android to achieve that.
And based on this offical sample is more convenient, just modify code as follow can achieve your wants.
This official sample can preview camera view in Xamarin Forms App, we just need to add a Timer to call the Frame from Camera after 5 seconds.The modified Renderer code as follow:
public class CameraPreviewRenderer : ViewRenderer<CustomRenderer.CameraPreview, CustomRenderer.Droid.CameraPreview>, Camera.IPreviewCallback
{
CameraPreview cameraPreview;
byte[] tmpData;
public CameraPreviewRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<CustomRenderer.CameraPreview> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
// Unsubscribe
cameraPreview.Click -= OnCameraPreviewClicked;
}
if (e.NewElement != null)
{
if (Control == null)
{
cameraPreview = new CameraPreview(Context);
SetNativeControl(cameraPreview);
}
Control.Preview = Camera.Open((int)e.NewElement.Camera);
// Subscribe
cameraPreview.Click += OnCameraPreviewClicked;
}
}
protected override void OnAttachedToWindow()
{
base.OnAttachedToWindow();
// call the timer method to get the current frame.
Device.StartTimer(new TimeSpan(0, 0, 5), () =>
{
// do something every 5 seconds
Device.BeginInvokeOnMainThread(() =>
{
Console.WriteLine("get data"+tmpData);
// using MessagingCenter to pass data to forms
MessagingCenter.Send<object, byte[]>(this, "CameraData", tmpData);
cameraPreview.Preview.StopPreview();
cameraPreview.IsPreviewing = false;
// interact with UI elements
});
return false; // runs again, or false to stop
});
}
void OnCameraPreviewClicked(object sender, EventArgs e)
{
if (cameraPreview.IsPreviewing)
{
cameraPreview.Preview.StopPreview();
cameraPreview.IsPreviewing = false;
}
else
{
cameraPreview.Preview.SetPreviewCallback(this);
cameraPreview.Preview.StartPreview();
cameraPreview.IsPreviewing = true;
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
Control.Preview.Release();
}
base.Dispose(disposing);
}
// get frame all the time
public void OnPreviewFrame(byte[] data, Camera camera)
{
tmpData = data;
}
}
Now, Xamarin Forms can receive the data from MessagingCenter:
MessagingCenter.Subscribe<object, byte[]>(this, "CameraData", async (sender, arg) =>
{
MemoryStream stream = new MemoryStream(arg);
if (stream != null)
{
//image is defined in Xaml
image.Source = ImageSource.FromStream(() => stream);
}
});
image is defined in XAML: <Image x:Name="image" WidthRequest="200" HeightRequest="200"/>

How to implement Native Ads without MediaView

I want to implement Native Ads in RecyclerView without MediaView, So referred to this tutorial after following this I successfully implemented native ads in RecyclerView but my need is to load only icon, heading, advertiser and call to action button not the MediaView, etc.
So I removed the MediView and other elements Code from the implementation which I made after which the code looks like this
UnifiedNativeAdViewHolder.java
package com.mishracodes.myapplication;
import android.view.View;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.ads.formats.UnifiedNativeAdView;
public class UnifiedNativeAdViewHolder extends RecyclerView.ViewHolder {
private UnifiedNativeAdView adView;
public UnifiedNativeAdView getAdView() {
return adView;
}
UnifiedNativeAdViewHolder(View view) {
super(view);
adView = view.findViewById(R.id.ad_view);
adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
adView.setIconView(adView.findViewById(R.id.ad_icon));
adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
}
}
and inside the RecyclerViewAdapter.java the part of code which manages NativeAdView
private void populateNativeAdView(UnifiedNativeAd nativeAd,
UnifiedNativeAdView adView) {
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
NativeAd.Image icon = nativeAd.getIcon();
if (icon == null) {
adView.getIconView().setVisibility(View.INVISIBLE);
} else {
((ImageView) adView.getIconView()).setImageDrawable(icon.getDrawable());
adView.getIconView().setVisibility(View.VISIBLE);
}
if (nativeAd.getAdvertiser() == null) {
adView.getAdvertiserView().setVisibility(View.INVISIBLE);
} else {
((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
adView.getAdvertiserView().setVisibility(View.VISIBLE);
}
adView.setNativeAd(nativeAd);
}
loading Native Ad function in MainActivity.java
private void loadNativeAds() {
AdLoader.Builder builder = new AdLoader.Builder(this, getString(R.string.ad_unit_id));
adLoader = builder.forUnifiedNativeAd(
new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
mNativeAds.add(unifiedNativeAd);
if (!adLoader.isLoading()) {
insertAdsInMenuItems();
}
}
}).withAdListener(
new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
Log.d("MainActivity", "The previous native ad failed to load. Attempting to"
+ " load another.");
if (!adLoader.isLoading()) {
insertAdsInMenuItems();
}
}
}).build();
adLoader.loadAds(new AdRequest.Builder().build(), NUMBER_OF_ADS);
}
Now after doing this I used Native Ad Validator to validate if the ads are implemented correctly
Result:
But After testing it for sometime I got this error
Warning in detail
So Is there any way to Call for Loading Native Ad so that only those ads are called which do not have Media View. As I have Seen Many Apps which uses native ads like this without Media View.
We can but it was possible before as Google made it mandatory to add MediaView. Also it's a good idea as your revenue would be increased but it may affect the user experience. Finally, you can't do this because if you upload it to the play store then Google will send you a warning notice saying that you didn't add MediaView. It may affect your earnings.
I'm too late, but it helps someone. I found a way to implement native ads without that MediaView.
The logic is Google has provided some native templates. These are for beginners who want to get started with native ads without implementing them from scratch. There are two templates - gnt_small_template_view and gnt_medium_template_view. gnt_small_template_view doesn't have MediaView. You can happily implement it in recycler view and also you can change the layout as per your requirement (but don't change the view ids). The original layout doesn't look good. You mostly change it.
Here is the documentation.
I have added these templates to my app, I didn't face any issue.

How to change Vuforia AR camera focus mode?

I am using Vuforia 6.2 AR SDK for in Unity. But while I test the application in Android phone the camera seems like blurry. I searched in Vuforia's developer website and found some camera focus mode but I can't implement because that guideline was for older Vuforia SDK, I can't find the script they mentioned in their website. Here is their code sample but it's not working. I created different script and run this line on Start() function, but still not working.
CameraDevice.Instance.SetFocusMode(
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
try this
void Start ()
{
VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
VuforiaARController.Instance.RegisterOnPauseCallback(OnPaused);
}
private void OnVuforiaStarted()
{
CameraDevice.Instance.SetFocusMode(
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}
private void OnPaused(bool paused)
{
if (!paused) // resumed
{
// Set again autofocus mode when app is resumed
CameraDevice.Instance.SetFocusMode(
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}
}
This code is the right code.
bool cameramode = false;
public void OnCameraChangeMode()
{
Vuforia.CameraDevice.CameraDirection currentDir = Vuforia.CameraDevice.Instance.GetCameraDirection();
if (!cameramode) {
RestartCamera(Vuforia.CameraDevice.CameraDirection.CAMERA_FRONT);
camBtnTxt.text = "Back Camera";
} else {
RestartCamera(Vuforia.CameraDevice.CameraDirection.CAMERA_BACK);
camBtnTxt.text = "Front Camera";
}
}
private void RestartCamera(Vuforia.CameraDevice.CameraDirection newDir)
{
Vuforia.CameraDevice.Instance.Stop();
Vuforia.CameraDevice.Instance.Deinit();
Vuforia.CameraDevice.Instance.Init(newDir);
Vuforia.CameraDevice.Instance.Start();
}

Applovin AdclickListener not working in android

I am integrating AppLovin sdk to integrate ads in my app.My app is a game app and I want my user to reward coins when they click on the ads.But adclicklistener of AppLovin seems not working in my case.
The Code:
private AppLovinAdView adView;
// Create AppLovin Ad View
final AppLovinSdk sdk = AppLovinSdk.getInstance(SceneActivity.this);
adView = new AppLovinAdView(sdk, AppLovinAdSize.INTERSTITIAL, SceneActivity.this);
//Show ad after 4 levels
if (currentLevel % 4 == 0) {
// An ad is available to display. It's safe to call show.
AppLovinInterstitialAd.show(SceneActivity.this);
adView.loadNextAd();
}
adView.setAdClickListener(new AppLovinAdClickListener() {
#SuppressLint("SimpleDateFormat")
#Override
public void adClicked(AppLovinAd arg0)
{
System.out.println("Adclicked");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String today = sdf.format(new Date());
if (!today.equalsIgnoreCase(getLastDownloadDate())) {
// give coins once per day
modifyMoney(MONEY_DOWNLOAD_GAME);
// put last Download date
setLastDownloadDate(today);
}
}
});
The INTERSTITIAL ad is displaying but the click listener is not working.
The problem is that you are mixing AppLovinInterstitialAd (which internally owns its own instance of AppLovinAdView) and your own standalone AppLovinAdView. When you call AppLovinInterstitialAd.show(), you're bypassing your ad view which had the listener attached to it.
So you don't need to use AppLovinAdView at all... give this a try:
private void showInterstitial() {
final AppLovinSdk sdk = AppLovinSdk.getInstance(mActivity);
final AppLovinInterstitialAdDialog adDialog = AppLovinInterstitialAd.create(sdk, mActivity);
adDialog.setAdClickListener(new AppLovinAdClickListener() {
#Override
public void adClicked(AppLovinAd appLovinAd) {
// Ad clicked, add your on-click logic here
}
});
adDialog.show(); // Display a pre-cached interstitial
}

Categories

Resources