Integrating Rewarded Video

Read through the process of requesting and displaying Fyber Rewarded Video for users in your app here. Before following this, you should have already completed Getting started with the Fyber Unity Plugin.

[Requesting a Rewarded Video](#requesting-a-rewarded-video)
[Handling Errors](#handling-errors)
[Showing a Rewarded Video](#showing-a-rewarded-video)
[Rewarded Video Result](#rewarded-video-result)
[Configuring the Rewarded Video](#configuring-the-rewarded-video) ## Requesting a Rewarded Video Before displaying a `Rewarded Video` you'll have to request it. To do so: 1. Make sure you've initialized the SDK previously as described in [Getting started with Fyber SDK](/content/current/unity/integration/getting-started/). 2. Set up a delegate to listen to `Rewarded Video` requests using this snippet: ``` void OnEnable() { // Ad availability FyberCallback.AdAvailable += OnAdAvailable; } void OnDisable() { // Ad availability FyberCallback.AdAvailable -= OnAdAvailable; } // [...] Ad rewardedVideoAd; // [...] private void OnAdAvailable(Ad ad) { switch(ad.AdFormat) { case AdFormat.REWARDED_VIDEO: rewardedVideoAd = ad; break; //handle other ad formats if needed } } ``` 3. Use the following code snippet to request a `Rewarded Video`: ``` RewardedVideoRequester.Create() // optional method chaining //.AddParameter("key", "value") //.AddParameters(dictionary) //.WithPlacementId(placementId) // changing the GUI notification behaviour when the user finishes viewing the video //.NotifyUserOnCompletion(true) // you can add a virtual currency requester to a video requester instead of requesting it separately //.WithVirtualCurrencyRequester(virtualCurrencyRequester) // you don't need to add a callback if you are using delegates //.WithCallback(requestCallback) // requesting the video .Request(); ``` ### Handling Errors You won't always get a successful response. To handle these scenarios you'll have to set up these delegate methods: ``` void OnEnable() { FyberCallback.AdNotAvailable += OnAdNotAvailable; FyberCallback.RequestFail += OnRequestFail; } void Disable() { FyberCallback.AdNotAvailable -= OnAdNotAvailable; FyberCallback.RequestFail -= OnRequestFail; } private void OnAdNotAvailable(AdFormat adFormat) { switch(adFormat) { case AdFormat.REWARDED_VIDEO: rewardedVideoAd = null; break; //handle other ad formats if needed } } private void OnRequestFail(RequestError error) { // process error UnityEngine.Debug.Log("OnRequestError: " + error.Description); } ``` `error` can have one of these values: | RequestError | Description | --- | ---- |DEVICE_NOT_SUPPORTED | Only devices running Android API level 10 and above are supported. |CONNECTION_ERROR | Internet connection error. |UNKNOWN_ERROR | An unknown error occurred. |SDK_NOT_STARTED | You need to start the SDK. |NULL_CONTEXT_REFERENCE | Context reference cannot be null. |SECURITY_TOKEN_NOT_PROVIDED | The security token was not provided when starting the SDK. |ERROR_REQUESTING_ADS | An error happened while trying to retrieve ads |UNABLE_TO_REQUEST_ADS | The SDK is unable to request right now because it is either already performing a request or showing an ad. ## Showing a Rewarded Video Once you've received a `Rewarded Video` `Ad` you show it via: ``` Ad rewardedVideoAd; //[...] public void ShowVideo() { if (rewardedVideoAd != null) { rewardedVideoAd.Start(); rewardedVideoAd = null; } } ``` ### Rewarded Video Result You may want to be notified when the `Rewarded Video` is started and closed. To do so you'll have to add these extra delegates: ``` void OnEnable() { FyberCallback.AdStarted += OnAdStarted; FyberCallback.AdFinished += OnAdFinished; } void OnDisable() { FyberCallback.AdStarted -= OnAdStarted; FyberCallback.AdFinished -= OnAdFinished; } private void OnAdStarted(Ad ad) { switch(ad.adFormat) { case AdFormat.REWARDED_VIDEO: rewardedVideoAd = null; break; //handle other ad formats if needed } } private void OnAdFinished(AdResult result) { switch (result.AdFormat) { case AdFormat.REWARDED_VIDEO: UnityEngine.Debug.Log("rewarded video closed with result: " + result.Status + "and message: " + result.Message); break; //handle other ad formats if needed } } ``` `result.Message` can have one of these values: | Value | Meaning | -------------------| ------------------- |`"CLOSE_FINISHED"` | The video has finished after completing. The user will be rewarded. |`"CLOSE_ABORTED"` | The video has finished before completing. The user might have aborted it, either explicitly (by tapping the close button) or implicitly (by switching to another app) or it was interrupted by an asynchronous event like an incoming phone call. |`"ERROR"` | The video was interrupted or failed to play due to an error. | ## Configuring the Rewarded Video ### User Notifications By default, Fyber SDK will notify your user of successful engagements with the following message: _Thanks! Your reward will be paid out shortly_. If you want to override this behaviour you can chain the `NotifyUserOnCompletion` method to you request: ``` RewardedVideoRequester.Create() .NotifyUserOnCompletion(false) .Request(); ``` ### Rewarding the User If you want to reward the user after he/she has completed an offer you will have to perform a `Virtual Currency` request. You can do this separately ( see [Virtual Currency System](/content/current/unity/integration/rewarding-user/vcs/) ) or you can incorporate it into your `Rewarded Video` request. To do so you'll first have to setup the appropriate delegates: ``` void OnEnable() { FyberCallback.VirtualCurrencySuccess += OnCurrencyResponse; FyberCallback.VirtualCurrencyError += OnCurrencyErrorResponse; } void OnDisable() { FyberCallback.VirtualCurrencySuccess -= OnCurrencyResponse; FyberCallback.VirtualCurrencyError -= OnCurrencyErrorResponse; } public void OnCurrencyResponse(VirtualCurrencyResponse response) { UnityEngine.Debug.Log("Delta of coins: " + response.DeltaOfCoins.ToString() + ". Transaction ID: " + response.LatestTransactionId + ".\nCurreny ID: " + response.CurrencyId + ". Currency Name: " + response.CurrencyName); } public void OnCurrencyErrorResponse(VirtualCurrencyErrorResponse vcsError) { UnityEngine.Debug.Log(String.Format("Delta of coins request failed.\n" + "Error Type: {0}\nError Code: {1}\nError Message: {2}", vcsError.Type, vcsError.Code, vcsError.Message); } ``` Then you will need to create a `VirtualCurrencyRequester` that you will chain to your `RewardedVideo` request: ``` VirtualCurrencyRequester virtualCurrencyRequester = VirtualCurrencyRequester.Create() // optional method chaining //.AddParameter("key", "value") //.AddParameters(dictionary) // Overrideing currency Id //.ForCurrencyId(currencyId) // Changing the GUI notification behaviour for when the user is rewarded //.NotifyUserOnReward(true) // you don't need to add a callback if you are using delegates //.WithCallback(vcsCallback) ; RewardedVideoRequester.Create() .WithVirtualCurrencyRequester(virtualCurrencyRequester) .Request(); ``` You can learn more about the Virtual Currency System [here](/content/current/unity/integration/rewarding-user/vcs/). **Please note: If you’re having problems with your Autorotation in videos or the endcard, please see [here](/content/current/faq/rewarded-video/#i-39-m-having-problems-with-autocorrect-rotation-in-my-videos-i-can-39-t-get-the-video-to-lock-what-do-i-do). ## Mediation Fyber `Rewarded Video` product supports mediation for all of our partner networks. To learn more about integrating mediated partners please read [Ad Network Mediation section](/content/current/unity/mediation).