-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from Pedro2712/AdsRespawn
Respawn player after Ads.
- Loading branch information
Showing
17 changed files
with
309 additions
and
228 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
public class ActivateAds : MonoBehaviour | ||
{ | ||
[Header("Ads Screen")] | ||
public GameObject AdsScreen; | ||
|
||
private bool respawnInThisRoomBefore; | ||
|
||
[Header("Player")] | ||
public Player player; | ||
|
||
[Header("Init Ads")] | ||
public AdsInitializer initAds; | ||
|
||
void Start() | ||
{ | ||
// Toda vez que isso recarregar estou em outra sala entao poderia reviver novamente. | ||
respawnInThisRoomBefore = false; | ||
} | ||
|
||
public void loadAds() | ||
{ | ||
// Não tenho a possibilidade de reviver após morrer uma segunda vez na mesma sala. | ||
if(!respawnInThisRoomBefore){ | ||
// Enable Ads Screen apenas se o player não já tiver respawnado antes | ||
// Pause Game | ||
// Carrega Ads: | ||
respawnInThisRoomBefore = true; | ||
StartCoroutine(DelayedAdsScreen()); | ||
AdsScreen.SetActive(true); | ||
|
||
initAds.startAds(); | ||
|
||
//Time.timeScale = 0f; | ||
|
||
} else if (respawnInThisRoomBefore){ | ||
//Carrega GameOver direto | ||
SceneManager.LoadScene("GameOver"); | ||
} | ||
} | ||
|
||
private IEnumerator DelayedAdsScreen(){ | ||
yield return new WaitForSeconds(3f); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
Assets/Scripts/Interstitial.cs.meta → Assets/Scripts/Ads/ActivateAds.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using UnityEngine.Advertisements; | ||
|
||
public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener | ||
{ | ||
[SerializeField] string _androidGameId; | ||
[SerializeField] string _iOSGameId; | ||
[SerializeField] bool _testMode = true; | ||
private string _gameId; | ||
|
||
[SerializeField] RewardedAdsButton rewardedAdsButton; | ||
|
||
public void startAds() | ||
{ | ||
InitializeAds(); | ||
} | ||
|
||
public void InitializeAds() | ||
{ | ||
#if UNITY_IOS | ||
_gameId = _iOSGameId; | ||
#elif UNITY_ANDROID | ||
_gameId = _androidGameId; | ||
#elif UNITY_EDITOR | ||
_gameId = _androidGameId; //Only for testing the functionality in the Editor | ||
#endif | ||
|
||
if (!Advertisement.isInitialized && Advertisement.isSupported) | ||
{ | ||
Advertisement.Initialize(_gameId, _testMode, this); | ||
} | ||
|
||
if (Advertisement.isInitialized){ | ||
Debug.LogWarning("Unity Ads initialization complete."); | ||
rewardedAdsButton.LoadAd(); | ||
}else{ | ||
Debug.LogWarning($"Unity Ads Initialization Failed."); | ||
} | ||
} | ||
|
||
|
||
public void OnInitializationComplete() | ||
{ | ||
Debug.LogWarning("Unity Ads initialization complete."); | ||
rewardedAdsButton.LoadAd(); | ||
} | ||
|
||
public void OnInitializationFailed(UnityAdsInitializationError error, string message) | ||
{ | ||
Debug.LogWarning($"Unity Ads Initialization Failed: {error.ToString()} - {message}"); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
public class GameOverButton : MonoBehaviour | ||
{ | ||
public void GotToGameOverScreen(){ | ||
//Time.timeScale = 1f; | ||
SceneManager.LoadScene("GameOver"); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
Assets/Scripts/Banner.cs.meta → Assets/Scripts/Ads/GameOverButton.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
using UnityEngine.Advertisements; | ||
|
||
public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener | ||
{ | ||
[SerializeField] Button _showAdButton; | ||
[SerializeField] string _androidAdUnitId = "Rewarded_Android"; | ||
[SerializeField] string _iOSAdUnitId = "Rewarded_iOS"; | ||
string _adUnitId = null; // This will remain null for unsupported platforms | ||
|
||
[Header("Player")] | ||
public Player player; | ||
public Transform respawnLocation; | ||
public Animator animator; | ||
|
||
[Header("AdsScreen")] | ||
public GameObject AdsScreen; | ||
|
||
void Awake() | ||
{ | ||
// Get the Ad Unit ID for the current platform: | ||
#if UNITY_IOS | ||
_adUnitId = _iOSAdUnitId; | ||
#elif UNITY_ANDROID | ||
_adUnitId = _androidAdUnitId; | ||
#endif | ||
|
||
// Disable the button until the ad is ready to show: | ||
_showAdButton.interactable = false; | ||
} | ||
|
||
// Call this public method when you want to get an ad ready to show. | ||
public void LoadAd() | ||
{ | ||
// IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script). | ||
Debug.LogWarning("Loading Ad: " + _adUnitId); | ||
Advertisement.Load(_adUnitId, this); | ||
} | ||
|
||
// If the ad successfully loads, add a listener to the button and enable it: | ||
public void OnUnityAdsAdLoaded(string adUnitId) | ||
{ | ||
Debug.LogWarning("Ad Loaded: " + adUnitId); | ||
|
||
if (adUnitId.Equals(_adUnitId)) | ||
{ | ||
// Configure the button to call the ShowAd() method when clicked: | ||
_showAdButton.onClick.AddListener(ShowAd); | ||
// Enable the button for users to click: | ||
_showAdButton.interactable = true; | ||
} | ||
} | ||
|
||
// Implement a method to execute when the user clicks the button: | ||
public void ShowAd() | ||
{ | ||
// Disable the button: | ||
_showAdButton.interactable = false; | ||
// Then show the ad: | ||
Advertisement.Show(_adUnitId, this); | ||
} | ||
|
||
// Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward: | ||
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState) | ||
{ | ||
if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED)) | ||
{ | ||
Debug.LogWarning("Unity Ads Rewarded Ad Completed"); | ||
|
||
// Grant a reward. | ||
Debug.LogWarning("Back To Game"); | ||
AdsScreen.SetActive(false); | ||
//Time.timeScale = 1f; | ||
|
||
player.entity.currentHealth = player.entity.maxHealth; | ||
player.entity.currentStamina = player.entity.maxStamina; | ||
|
||
// Atualizando Global Variables | ||
GlobalVariables.instance.life = player.entity.maxHealth; | ||
GlobalVariables.instance.stamina = player.entity.maxStamina; | ||
|
||
// Manda o Player para a posição de respawn | ||
player.transform.position = respawnLocation.position; | ||
player.entity.dead = false; | ||
animator.SetBool("isDead", false); | ||
animator.SetTrigger("respawn"); | ||
|
||
} | ||
} | ||
|
||
// Implement Load and Show Listener error callbacks: | ||
public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message) | ||
{ | ||
Debug.LogWarning($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}"); | ||
// Use the error details to determine whether to try to load another ad. | ||
} | ||
|
||
public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message) | ||
{ | ||
Debug.LogWarning($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}"); | ||
// Use the error details to determine whether to try to load another ad. | ||
} | ||
|
||
public void OnUnityAdsShowStart(string adUnitId) { } | ||
public void OnUnityAdsShowClick(string adUnitId) { } | ||
|
||
void OnDestroy() | ||
{ | ||
// Clean up the button listeners: | ||
_showAdButton.onClick.RemoveAllListeners(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.